我想获取用户的当前位置并计算该位置与其他位置之间的距离。我正在使用本教程: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变量未设置当前用户位置的纬度和经度。你能帮助我吗!!
答案 0 :(得分:11)
calculateDistance(lat1:number,lat2:number,long1:number,long2:number){
let p = 0.017453292519943295; // Math.PI / 180
let c = Math.cos;
let a = 0.5 - c((lat1-lat2) * p) / 2 + c(lat2 * p) *c((lat1) * p) * (1 - c(((long1- long2) * p))) / 2;
let dis = (12742 * Math.asin(Math.sqrt(a))); // 2 * R; R = 6371 km
return dis;
}
答案 1 :(得分:0)
尝试google DirectionsService:
https://developers.google.com/maps/documentation/javascript/directions
滚动到页面末尾,最后一个示例计算点之间的距离。