【问题标题】:Getting GPS coordinate at click point在点击点获取 GPS 坐标
【发布时间】:2020-07-31 21:36:43
【问题描述】:

我正在创建一个伪造查看器并尝试创建一个扩展,允许我单击 3D 模型上的一个点并在弹出窗口或类似窗口中显示该点的坐标.. 我似乎无法获得地理定位扩展工作。有谁知道这样做的任何教程或示例代码?

汤姆干杯。

【问题讨论】:

    标签: autodesk-forge


    【解决方案1】:

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

    但是,您可以查看以下示例代码,该示例代码演示了如何将鼠标单击点转换为与 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);
    })();
    

    【讨论】:

      猜你喜欢
      • 2010-09-06
      • 2016-04-14
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2010-11-29
      • 2011-05-09
      • 1970-01-01
      相关资源
      最近更新 更多