如何通过代码获取Autodesk Forge中自定义点的经纬高坐标?
Hey there! I totally get the frustration when you can see data in the properties panel but can't seem to pull it via code—let's fix that. Since you have basic JS experience and are new to React, I'll stick to plain Viewer JavaScript examples that you can drop into your Node.js/JS setup.
There are two common scenarios for these coordinates, so let's cover both:
Scenario 1: Coordinates are stored as custom properties (what you see in the properties panel)
If the lat/lon/z values show up directly in the Viewer's properties panel for your rectangle, you can fetch them using the viewer.getProperties() method. Here's how:
First, get the
dbIdof your rectangle. You can either:- Use
viewer.getSelection()to get the dbId of a selected object - Or traverse the model's object tree to find the right element (if you know its name or other identifiers)
- Use
Fetch the properties and extract the coordinates:
// Get the first selected object's dbId (adjust if you need a specific element) const targetDbId = viewer.getSelection()[0]; if (targetDbId) { viewer.getProperties(targetDbId, (props) => { // Loop through all properties to find your lat/lon/z entries props.properties.forEach(prop => { // Match the display names exactly as they appear in the properties panel switch(prop.displayName) { case '纬度': // or 'Latitude', 'lat'—use whatever you see in the panel console.log('Latitude:', prop.displayValue); break; case '经度': // or 'Longitude', 'lon' console.log('Longitude:', prop.displayValue); break; case '高程': // or 'Elevation', 'z' console.log('Elevation:', prop.displayValue); break; } }); }); }
Scenario 2: Coordinates are derived from the model's georeferencing
If the model is georeferenced (aligned to real-world coordinates), you'll need to convert the rectangle's local 3D coordinates to lat/lon/z using the Viewer's georeferencing API. Here's how:
// Get the dbId of your rectangle (same as above) const targetDbId = viewer.getSelection()[0]; if (targetDbId) { // Get the bounding box of the rectangle to access its corner points viewer.getBoundingBox(targetDbId, (bbox) => { if (!bbox) return; // Define all four corner points of the 2D rectangle (adjust for 3D if needed) const corners = [ new THREE.Vector3(bbox.min.x, bbox.min.y, bbox.min.z), new THREE.Vector3(bbox.max.x, bbox.min.y, bbox.min.z), new THREE.Vector3(bbox.max.x, bbox.max.y, bbox.min.z), new THREE.Vector3(bbox.min.x, bbox.max.y, bbox.min.z) ]; // Get the model's georeferencing transformation const geoRef = viewer.model.getGeoreferencing(); if (geoRef) { corners.forEach((corner, index) => { // Convert local model coordinates to real-world lat/lon/z const worldCoords = geoRef.modelToWorld(corner); // Note: Depending on the model's CRS, worldCoords.x is usually longitude, y is latitude, z is elevation console.log(`Corner ${index + 1} real-world coords:`, { lon: worldCoords.x, lat: worldCoords.y, z: worldCoords.z }); }); } else { console.log('This model does not have georeferencing data.'); } }); }
Quick Tips for Your Setup
- Since you're new to React, you don't need to wrap this in React components right now—you can run this code after the Viewer finishes initializing (hook into the
viewer.initialize()promise or theGEOMETRY_LOADED_EVENT). - Double-check the property names in the properties panel to make sure your
switchcase matches exactly (capitalization, spaces, and language matter!).
Let me know if you run into issues—start with verifying which scenario applies to your model, and you'll be pulling those coordinates in no time!
内容的提问来源于stack exchange,提问作者Fred Coud

