在点击点获取 GPS 坐标

Getting GPS coordinate at click point

我正在创建一个 forge 查看器并尝试创建一个扩展,让我可以单击 3D 模型上的一个点并在弹出窗口 window 或类似窗口中显示该点的坐标..我似乎无法使地理定位扩展能够正常工作。有谁知道执行此操作的任何教程或示例代码?

汤姆干杯。

如果您能提供更多有关您遇到的问题的详细信息,这将有助于我们快速了解它。

但是,您可以查看下面的示例代码,演示如何将鼠标点击点转换为与 Autodesk.Geolocation 扩展一起使用的地理定位。另外,参考我的回复 here 配置你的 Revit 模型,使 Autodesk.Geolocation 扩展正常工作。

(function() {
  class GisTool extends Autodesk.Viewing.ToolInterface {
    constructor() {
        super();
        this.names = ['gis-tool'];
 
        // Hack: delete functions defined *on the instance* of the tool.
        // We want the tool controller to call our class methods instead.
        delete this.register;
        delete this.deregister;
        delete this.activate;
        delete this.deactivate;
        delete this.getPriority;
        delete this.handleSingleClick;
    }
 
    register() {      
      console.log('GisTool registered.');
    }
 
    deregister() {
      this.viewer.unloadExtension('Autodesk.Geolocation');
      this.geoTool = null;
      console.log('GisTool unregistered.');
    }
 
    async activate(name, viewer) {
      this.viewer = viewer;
      this.geoTool = await this.viewer.loadExtension('Autodesk.Geolocation');

      if (!this.geoTool.hasGeolocationData())
        alert( 'No GIS data found in current model' );

      console.log('GisTool activated.');
    }
 
    deactivate(name) {
      console.log('GisTool deactivated.');
    }
 
    getPriority() {
      return 1; // Or feel free to use any number higher than 0 (which is the priority of all the default viewer tools)
    }
 
    handleSingleClick(event, button) {
      if (button === 0 ) {
        // const hitPoint = this._intersect(event.clientX, event.clientY);
        // const geolocation = this.geoTool.lmvToLonLat(hitPoint);
        const canvasX = event.canvasX;
        const canvasY = event.canvasY;
        const res = this.viewer.clientToWorld(canvasX, canvasY);
        const geolocation = this.geoTool.lmvToLonLat(res.point);
        console.log(JSON.stringify(res.point));
        console.log(JSON.stringify(geolocation)); //!<<< the geolocation you want
        return true; // Stop the event from going to other tools in the stack
      }
      // Otherwise let another tool handle the event
      return false;
    }

    _intersect(clientX, clientY) {
      return this.viewer.impl.intersectGround(clientX, clientY);
    }
  }

  class GisToolExtension extends Autodesk.Viewing.Extension {
    constructor(viewer, options) {
      super(viewer, options);
      this.tool = new GisTool();
    }
 
    load() {
      this.viewer.toolController.registerTool(this.tool);
      console.log('GisToolExtension loaded.');
      return true;
    }
 
    unload() {
      this.viewer.toolController.deregisterTool(this.tool);
      console.log('GisToolExtension unloaded.');
      return true;
    }
 
    onToolbarCreated(toolbar) {
      const controller = this.viewer.toolController;
      this.button = new Autodesk.Viewing.UI.Button('gis-tool-button');
      this.button.onClick = (ev) => {
          const isActivated = controller.isToolActivated('gis-tool');
          if (isActivated) {
            controller.deactivateTool('gis-tool');
            this.button.setState(Autodesk.Viewing.UI.Button.State.INACTIVE);
          } else {
              controller.activateTool('gis-tool');
              this.button.setState(Autodesk.Viewing.UI.Button.State.ACTIVE);
          }
      };
      this.button.setToolTip('GIS Tool');

      this.group = new Autodesk.Viewing.UI.ControlGroup('gis-tool-group');
      this.group.addControl(this.button);
      toolbar.addControl(this.group);
    }
  }
  Autodesk.Viewing.theExtensionManager.registerExtension('GisToolExtension', GisToolExtension);
})();