无法立即在openlayers 5中获取地理位置坐标

时间:2018-08-05 18:24:01

标签: angular geolocation openlayers angular6 openlayers-5

我正在使用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>

将自动选中较小屏幕的复选框。

无论如何,即使我没有遇到任何错误,我也必须亲自几次手动取消选中该复选框,以查看地理位置坐标并使整个代码正常工作。第一次打开它时它不起作用,没有任何坐标,因此我将其关闭然后再打开。之后,它应该可以正常工作。

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

谢谢

1 个答案:

答案 0 :(得分:1)

仅当您使用toggleGeolocation调用true时才打开跟踪,在此阶段仅发生这种情况,然后选中该复选框。如果您希望它立即开始,则应在ngOnInit方法的结尾处​​调用它:

this.toggleGeolocation(this.isChecked);