小编use*_*806的帖子

获取ionic2中的当前位置

我是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;
    }
Run Code Online (Sandbox Code Playgroud)

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

let usersLocation = {


            lat: 40.713744, 
            lng: -74.009056
        };
Run Code Online (Sandbox Code Playgroud)

我想到达真正的用户位置.

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

谢谢

EDITED

 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 
        };
Run Code Online (Sandbox Code Playgroud)

ionic2 angular

1
推荐指数
1
解决办法
7519
查看次数

标签 统计

angular ×1

ionic2 ×1