【问题标题】:Ionic geolocation - how to update position?离子地理定位 - 如何更新位置?
【发布时间】:2018-05-09 16:18:11
【问题描述】:

我是 Ionic 的新手,只是在我的应用程序中修复了地理位置。我现在可以在地图上看到我的位置,但我遇到了位置没有在地图上更新的问题。我尝试了所有可能的方法并在 Google 上搜索了很多内容,并在 Stackoverflow 上阅读了此处,但仍然没有任何效果。位置没有更新。你能看看我的代码并告诉我我做错了什么吗?

 loadMap() {

      this.geolocation.getCurrentPosition().then((position) => {
      let latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
      let mapOptions = {
        center: latLng,
        zoom: 15,
        mapTypeId: google.maps.MapTypeId.ROADMAP
      }
      this.map = new google.maps.Map(this.mapElement.nativeElement, mapOptions);


      //Red pin shows users position on the map
      var myPositionMarker = new google.maps.Marker({
        map: this.map,
        position: latLng, 
        icon: 'assets/imgs/pins/redpin.png',
        enableHighAccuracy:true
      })


      //For position update(this is a code which is not working)
        let watch = this.geolocation.watchPosition();
        watch.subscribe((data) => {
       // data can be a set of coordinates, or an error (if an error occurred).
       // data.coords.latitude
       // data.coords.longitude
       });

【问题讨论】:

    标签: ionic-framework geolocation


    【解决方案1】:

    您可以尝试使用 watchPosition 方法而不是 getCurrentPosition 类似:

    loadMap() {
    
      this.geolocation.watchPosition().subscribe((position) => {
          let latLng = new google.maps.LatLng(position.coords.latitude,position.coords.longitude);
          let marker = new google.maps.Marker({
            map: this.map,
            icon:  'assets/imgs/pins/redpin.png',
            position: latLng,
            enableHighAccuracy:true
           });
          let message = "Current Position";
          this.addInfoWindow(marker, content);
      }, (err) => {console.log(err);} );
    }
    

    【讨论】:

      猜你喜欢
      • 2016-03-17
      • 2018-05-03
      • 1970-01-01
      • 2016-12-23
      • 2014-08-16
      • 2015-12-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多