【问题标题】:Cannot get geolocation coordinates right away in openlayers 5无法在openlayers 5中立即获取地理位置坐标
【发布时间】:2018-08-06 19:25:11
【问题描述】:

我正在使用 openlayers5 和 angular 6 实现地理定位/位置跟踪功能。即使我没有收到任何错误并且这似乎工作正常,我也没有立即获得坐标。这是我的地理位置相关代码。

ngOnInit() {
//initialize everything in the ngOnInit
    //set it
    this.geolocation = new Geolocation({      
      trackingOptions: {
        enableHighAccuracy: true
      },
      projection: 'EPSG:3857'
    });

    //handle errors
    this.geolocation.on('error', (error) => {
      this.geolocationerror=true;
      this.geolocationmsg = error;
    });

    //accuracy
    this.accuracyFeature = new Feature(); 
    this.geolocation.on('change:accuracyGeometry', ()=> {
      this.accuracyFeature.setGeometry(this.geolocation.getAccuracyGeometry());
    });

    //position point
    this.positionFeature = new Feature();
    this.positionFeature.setStyle(new Style({
      image: new CircleStyle({
        radius: 6,
        fill: new Fill({
          color: '#3399CC'
        }),
        stroke: new Stroke({
          color: '#fff',
          width: 2
        })
      })
    }));

    //track position
    this.geolocation.on('change:position', ()=> {
      let coordinates = this.geolocation.getPosition();
      this.positionFeature.setGeometry(coordinates ?
        new Point(coordinates) : null);
    });

    //on smaller screens toggle the geolocation on automatically
    if(window.innerWidth < 600){
      this.isChecked = true;
    }    

    //geolocation has its own vector layer
    this.geolocsource = new VectorSource({});
    this.geoloclayer = new VectorLayer({
      source: this.geolocsource,
      title:'location'
    });

  }//ngOnInit closes here


  //toggle geolocation on/off
  toggleGeolocation(checked){
    //erase any previous errors    
    this.geolocationmsg='';
    this.geolocationerror=false;
    ////toggled on
    if(checked){
      this.geolocation.setTracking(checked); //set on
      this.geolocsource.addFeatures([this.accuracyFeature, this.positionFeature]) ;
      this.geolocOn = true; // to show info in html     
      let center = this.geolocation.getPosition();
      //zoom there
      if (center){
        this.olmap.getView().animate({
          center: center,
          duration: 2000,
          zoom:16
        });        
      }
      //show the geolocation coords in html all the time
      this.geolocLiveCoords = this.geolocation.getPosition();
    }
    else{ //geolocation off
      this.geolocation.setTracking(checked);      
      this.geolocOn = false;
      this.geolocsource.clear();
      this.geolocLiveCoords='';
    }    
  }

这是 HTML 部分

<label for="track">track position<input id="trackpos" type="checkbox" [(ngModel)]="isChecked" (change)="toggleGeolocation(isChecked)"/></label>
<div  *ngIf='geolocationerror'>{{geolocationmsg}}</div>
<div  *ngIf='geolocOn'>{{geolocLiveCoords}}</div>

该复选框会自动检查较小的屏幕。

在任何情况下,即使我没有收到任何错误,我必须自己手动检查复选框几次才能查看地理位置坐标并让整个代码正常工作。我第一次打开它时它不起作用,它没有坐标,所以我将它关闭并再次打开。之后它就可以正常工作了。

这里有什么问题?我该如何解决?请指教。

谢谢

【问题讨论】:

    标签: angular geolocation openlayers angular6 openlayers-5


    【解决方案1】:

    只有在您使用true 调用toggleGeolocation 时,您才会打开跟踪,在这个阶段,只有在您选中复选框时才会发生这种情况。如果您希望它立即开始,您应该在 ngOnInit 方法的末尾调用它:

    this.toggleGeolocation(this.isChecked);
    

    【讨论】:

    • 这是个好主意,但即便如此,我也必须自己手动检查复选框几次才能查看地理位置坐标并让整个代码正常工作。这就是问题所在:无论如何,我必须手动切换几次才能使地理定位工作。好像它不能立即工作或者它在第一次打开时没有坐标。谢谢
    • 你好。我知道是什么导致了错误。与通常情况一样,地理定位需要一些时间才能获得位置。在那一点点时间里,toggleGeolocation 函数的if(center)this.olmap.getView().animate({ 部分是假的,没有进入代码块并且它永远不会缩放到那个点。过了一会儿,有一点,但因为if,地图永远不会在那里缩放。现在,我不知道如何解决这个问题,因为addFeaturesgetPosition 都没有我可以安全使用的回调。关于地理位置实际有一点后何时缩放的任何其他想法?谢谢
    • 我翻阅了他们的文档,但找不到任何从中获取回调的方法。您是否尝试过在调用getPosition 后查看通用geolocation.on('change', (event) =&gt; { ... }) 是否被解雇?或者您可能必须等到它第一次被触发才能使用getPosition?如果他们没有使用该事件,那么我唯一能看到的工作就是使用间隔计时器来检查并等待结果。
    • 由于这个问题改变了上下文,我开始了一个新的、更具体的问题。我还包括一个我尝试过(但失败了)的新解决方案。如果您愿意,可以查看here。感谢您的宝贵时间。
    猜你喜欢
    • 2018-12-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多