获取Geolocation数据以存储在离子2应用程序中

时间:2017-01-01 06:51:01

标签: google-maps typescript geolocation ionic2

我正在尝试将Geolocation数据导入coords以在我的离子应用程序中存储在DynamoDB中,如果我按照以下代码继续收到错误,

bookResource(resourceId, timeslot): Promise<void> {
let promise: Promise<void> = new Promise<void>((resolve, reject) => {
  this.globals.displayLoader("Place...");

  let place : Place = {
    coords: google.maps.LatLng(position.coords.latitude, position.coords.longitude),
    userId: this.globals.getUserId(),
    userFirstName: this.globals.getUserFirstName(),
    userLastName: this.globals.getUserLastName(),
  };
  this.authClient.getClient().placesCreate(this.globals.getUserId(), place).subscribe(
    (data) => {
      place.placeId = data.placeId;
      this.places.push(place);
      resolve();
    },
    (err) => {
      reject(err);
    }
  );
});
return promise;
}

上面代码中的coords值没有存储地理位置坐标,而我的userId值存储正确,我该如何解决这个问题。感谢

2 个答案:

答案 0 :(得分:0)

ionic中的原生功能通常会返回PromiseObservable,您只能在success回调中访问该GeoLocationimport { Geolocation } from 'ionic-native'; getLocation(){ let options = { maximumAge: 3000, timeout: 50000, enableHighAccuracy: true }; return Geolocation.getCurrentPosition(options).then((pos)=>{ let place : Place = { coords: pos.coords, userId: this.globals.getUserId(), userFirstName: this.globals.getUserFirstName(), userLastName: this.globals.getUserLastName(), }; return place; }).catch( (err) => { console.log(err); }); } 。因此,如果您要保存设备的位置,可以使用离子的this.getLocation().then((place)=>{ console.log(place) // you can do whatever you want with ``place`` here this.authClient.getClient().placesCreate(this.globals.getUserId(), place).subscribe( (data) => { place.placeId = data.placeId; this.places.push(place); }, (err) => { } ); }) 插件。查看docs

div

然后您可以访问

这样的位置
<div />

答案 1 :(得分:0)

运行ngOnInit创建一个函数,在该函数中获取latlong值并将其值存储在数据库中

ngOnInit() {
  this.map = this.initMap();
  setInterval(() => {
    this.initMap();
  });
}

以下是要运行OnInit的函数,它返回一个promise。

initMap(): Promise<void> {
    let promise: Promise<void> = new Promise<void>((resolve, reject) => {
      Geolocation.getCurrentPosition().then((position) => {

        let latLng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
        let place: Place = {
          coords: latLng,
          userId: this.globals.getUserId(),
          userFirstName: this.globals.getUserFirstName(),
          userLastName: this.globals.getUserLastName(),
        };
        let GooleMap = new google.maps.Map(document.getElementById('map'), {
          center: latLng,
          zoom: 18
        });
          this.authClient.getClient().placesCreate(this.globals.getUserId(), place).subscribe(
            (data) => {
              place.placeId = data.placeId;
              this.places.push(place);
              resolve();
            },
            (err) => {
              reject(err);
            });
      });
    });
    return promise;
  }
相关问题