[英]Get current position in 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;
}
您可以看到變量usersLocation是硬編碼的:
let usersLocation = {
lat: 40.713744,
lng: -74.009056
};
我想在那里找到真實的用戶位置。
我已經看到了Geolocation.getCurrentPosition()方法,但是在這種情況下我不知道如何實現。
謝謝
已編輯
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
};
我將使用Geolocation cordova插件。 您可以使用ionic-native
訪問它。 首先,您需要安裝插件:
$ ionic plugin add cordova-plugin-geolocation --save
然后可以像這樣使用它:
import { Geolocation } from 'ionic-native';
Geolocation.getCurrentPosition().then(res => {
// res.coords.latitude
// res.coords.longitude
}).catch((error) => {
console.log('Error getting location', error);
});
https://ionicframework.com/docs/v2/native/geolocation/
編輯:
您更新的代碼幾乎是正確的。 您在代碼中犯了兩個小錯誤:
您定義了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 }
編輯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);
});
}
以及使用此功能的位置:
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
}
}
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.