我是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)