【问题标题】:Get current position in ionic2获取 ionic2 中的当前位置
【发布时间】:2017-05-03 13:33:09
【问题描述】:

我是 Ionic 2 的新手,正在学习一些教程。

在这种情况下,我需要更改以下方法:

 applyHaversine(locations){

        let usersLocation = {


            lat: 40.713744, 
            lng: -74.009056
        };

        locations.map((location) => {

            let placeLocation = {
                lat: location.latitude,
                lng: location.longitude
            };

            location.distance = this.getDistanceBetweenPoints(
                usersLocation,
                placeLocation,
                'miles'
            ).toFixed(2);
        });

        return locations;
    }

可以看到变量 usersLocation 是硬编码的:

let usersLocation = {


            lat: 40.713744, 
            lng: -74.009056
        };

我想获得真实的用户位置。

我见过 Geolocation.getCurrentPosition() 方法,但我不知道在这种情况下如何实现它。

谢谢

已编辑

 applyHaversine(locations){
        Geolocation.getCurrentPosition().then((resp) => {

        let  latitud = resp.coords.latitude
        let longitud = resp.coords.longitude
        }).catch((error) => {
          console.log('Error getting location', error);
        });

  console.log(this.latitud);
        let usersLocation = {

           lat:  this.latitud, 
           lng:  this.longitud 
        };

【问题讨论】:

    标签: angular ionic2


    【解决方案1】:

    我会使用 Geolocation cordova 插件。您可以使用ionic-native 访问它。首先你需要安装插件:

    $ ionic plugin add cordova-plugin-geolocation --save
    

    然后你可以像这样使用它:

    import { Geolocation } from 'ionic-native';
    
    Geolocation.getCurrentPosition().then(res => {
      // res.coords.latitude
      // res.coords.longitude
    }).catch((error) => {
      console.log('Error getting location', error);
    });
    

    https://ionicframework.com/docs/v2/native/geolocation/

    编辑:

    您更新的代码几乎是正确的。您在代码中犯了 2 个小错误:

    1. 您定义了 2 个局部变量(let latitudlet longitud),但随后在您的代码中使用 this.latitudthis.longitud 访问它们。 this 总是指在你的类中定义的变量,所以这些变量是未定义的。您必须使用局部变量或类范围变量,但这取决于您的体系结构。两者都有效。

    2. Geolocation.getCurrentPosition() 返回一个承诺。这意味着.then(() => {}) 中的代码将在稍后执行(只要插件具有您所在位置的结果)。但是您的其余代码在then 之外,这意味着它将在您获得位置之前执行。因此,您需要将整个代码复制到 then 中,如下所示:

      applyHaversine(locations) {
        Geolocation.getCurrentPosition().then(res => {
          let usersLocation = {
            lat: res.coords.latitude, 
            lng: res.coords.longitude
          };
      
          locations.map((location) => {
      
            let placeLocation = {
              lat: location.latitude,
              lng: location.longitude
            };
      
            location.distance = this.getDistanceBetweenPoints(
              usersLocation,
              placeLocation,
              'miles'
            ).toFixed(2);
          });
      
        console.log(locations); // This will now have your updated distances
      
        }).catch((error) => {
          console.log('Error getting location', error);
        });
      
        return locations; // this will not work because the code above is asynchronous
      }
      

    编辑 2:

    一个可行的例子是:

    applyHaversine(locations) {
      return new Promise((resolve, reject) => {
        Geolocation.getCurrentPosition().then(res => {
          let usersLocation = {
            lat: res.coords.latitude, 
            lng: res.coords.longitude
          };
    
          locations.map((location) => {
    
            let placeLocation = {
              lat: location.latitude,
              lng: location.longitude
            };
    
            location.distance = this.getDistanceBetweenPoints(
              usersLocation,
              placeLocation,
              'miles'
            ).toFixed(2);
          });
    
        resolve(locations); // As soon as this is called, the "then" in will be executed in the function below.
    
        }).catch(reject);
      });
    }
    

    以及你在哪里使用这个功能:

    doSomething(locations) {
      this.applyHaversine(locations).then(res => {
        // The logic after you have your new results goes here.
        console.log(res); // res will be the value that you passed into the resolve function above, in this case your updated locations array
      }
    }
    

    【讨论】:

    • 谢谢 Andreas,但是如何从 let usersLocation {} 变量定义中获取坐标?
    • 我不确定我是否明白你的意思。您可以通过编写usersLocation.latusersLocation.lng 访问坐标
    • 我的意思是,我应该在哪里包含您的提案,以及如何从 let usersLocation {}... 内部访问坐标:res.coords.latitude 和 res.coords.longitude?我在我的问题中包含了一个已编辑的提案,但不起作用。
    • 正如我在上面的示例中所指出的,最后的 return locations; 现在无法正常工作(它将与您传递给函数的数组相同)。完成计算后,您想对数据做什么?有两种方法可以解决这个问题: 1. 将其保存在类属性中 2. 返回另一个 Promise 您可能应该阅读有关 Promises 和 Javascript 中的异步代码以了解这些问题。
    • 就像现在一样,我正在获取显示在谷歌地图上的位置列表。
    猜你喜欢
    • 1970-01-01
    • 2011-07-15
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2014-05-20
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多