观看位置Ionic 3

时间:2018-06-11 13:43:38

标签: javascript ionic3 angular6

大家好我需要帮助,如何用Ionic 3观看Google地图中的位置,我希望在firebase中实时上传位置 在我的家里我有以下代码:

    getMyPosition() {
    this.geolocation.getCurrentPosition().then((result) => {
      this.positionTruck = new google.maps.LatLng(result.coords.latitude, result.coords.longitude);
      const mapOptions = {
        zoom: 18,
        center: this.positionTruck,
        disableDefaultUI: true
      }    
      this.map = new google.maps.Map(document.getElementById('map'), mapOptions);
      this.truckMarker(this.positionTruck);

      let watch = this.geolocation.watchPosition();
      watch.subscribe((data) => {
      let truck = { latitude: data.coords.latitude, longitude: data.coords.longitude };
        this.truckService.updateGeolocation(this.truck.key, truck);
        console.log(data.coords)
      });

    }).catch((error) => {
      console.log('Erro ao tentar pegar sua localização ', error);
    })
  }

现在我已将此代码用于更新truck.service:

    update(key: string, truck: any) {
    return new Promise((resolve, reject) => {
      this.db.list(this.PATH)
            .update(key, (truck))
            .then(() => resolve())
            .catch((e) => reject())
    })
  }

这种方式当我在设备上测试时,主页在页面中刷新,为什么? 此表单是否正确更新位置?求助。

1 个答案:

答案 0 :(得分:1)

导入:import { Geolocation, Geoposition } from '@ionic-native/geolocation';

并声明:

public lat: number = 0;
public lng: number = 0; 

并添加您的代码

let options = {
      frequency: 3000,
      enableHighAccuracy: true
    };

this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {

  console.log(position.coords.latitude + '   ++++++++++ ' + position.coords.longitude);

  // Run update inside of Angular's zone
  this.zone.run(() => {
    this.lat = position.coords.latitude;
    this.lng = position.coords.longitude;
    );

  });

});