【发布时间】:2017-06-28 16:00:58
【问题描述】:
我想获取用户的当前位置并计算该位置与其他位置之间的距离。我正在使用本教程:Create a Nearby Places List with Google Maps in Ionic 2。 这是代码:
let usersLocation;
Geolocation.getCurrentPosition().then((position) => {
usersLocation = { lat :position.coords.latitude , lng : position.coords.longitude }
locations.map((location) => {
let placeLocation = {
lat: location.latitude,
lng: location.longitude
};
location.distance = this.getDistanceBetweenPoints(
usersLocation,
placeLocation,
'miles'
).toFixed(2);
});
});
问题是:usersLocation变量没有设置当前用户所在位置的经纬度。你能帮助我吗!!
【问题讨论】:
-
您的 .then 函数仅在调用 getCurrentPosition 的成功结果时使用。你应该有一个很好的方法来捕捉错误,使用错误函数:developers.google.com/web/fundamentals/getting-started/primers/…。错误函数将帮助您确定调用 getCurrentPosition() 时可能发生的故障。我敢打赌,您调用 getCurrentPosition() 时会出错并且正在吃掉它。
-
我使用了上面链接中指示的方法,但是在成功或错误两种情况下都没有输入。我在另一个提供程序中调用函数 getCurrentPosition() 并且它工作正常。
-
您的 config.xml 是否具有允许 gps 权限的功能
-
没有。它没有那个功能。
-
确保您已关注cordova.apache.org/docs/en/latest/reference/… 以启用使用gps。
标签: typescript google-maps-api-3 ionic-framework ionic2