【问题标题】:Fetching Geolocation data to store in ionic 2 application获取地理位置数据以存储在 ionic 2 应用程序中
【发布时间】:2017-05-15 19:05:14
【问题描述】:

我正在尝试将地理位置数据导入coords 以存储在我的 ionic 应用程序中的 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 值存储正确,我该如何解决这个问题。谢谢

【问题讨论】:

  • 您是否要获取设备的当前位置?
  • @raj 是的,我正在尝试获取设备的当前位置

标签: google-maps typescript geolocation ionic2


【解决方案1】:

ionic 中的本机功能通常返回 PromiseObservable,您只能在 success 回调中访问它们。因此,如果您尝试保存设备的位置,您可以使用 ionic 的 GeoLocation 插件。查看docs

import { 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) => {
    }
  );
})

【讨论】:

  • 我已经更新了返回承诺的代码,您能否修改我在您的问题中的代码,我相信您的答案会起作用,谢谢
  • 我已经用我认为对你有用的解决方案更新了我的答案。如果有帮助,请接受它作为答案。
【解决方案2】:

运行 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;
  }

【讨论】:

    猜你喜欢
    • 2018-03-07
    • 2017-06-29
    • 2017-11-07
    • 1970-01-01
    • 1970-01-01
    • 2017-10-02
    • 2018-01-30
    • 2017-04-09
    • 2012-04-03
    相关资源
    最近更新 更多