我是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)
我会使用Geolocation cordova插件.你可以用它来访问它ionic-native.首先,您需要安装插件:
$ ionic plugin add cordova-plugin-geolocation --save
Run Code Online (Sandbox Code Playgroud)
然后你可以像这样使用它:
import { Geolocation } from 'ionic-native';
Geolocation.getCurrentPosition().then(res => {
// res.coords.latitude
// res.coords.longitude
}).catch((error) => {
console.log('Error getting location', error);
});
Run Code Online (Sandbox Code Playgroud)
https://ionicframework.com/docs/v2/native/geolocation/
编辑:
您的更新代码几乎是正确的.您在代码中犯了2个小错误:
您定义了2个局部变量(let latitud和let longitud),但稍后在代码中使用this.latitud和访问它们this.longitud.this始终引用您的类中定义的变量,因此这些变量将是未定义的.您必须使用局部变量或类范围变量,但这取决于您的体系结构.两者都有效.
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
}
Run Code Online (Sandbox Code Playgroud)编辑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);
});
}
Run Code Online (Sandbox Code Playgroud)
你在哪里使用这个功能:
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
}
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
7519 次 |
| 最近记录: |