【发布时间】:2020-01-02 22:41:57
【问题描述】:
每次我使用Geolocation 来自动定位设备,它总是返回第一次使用Geolocation 时定位的旧位置。这似乎是一个缓存问题。
当我在browser or android 上运行时,同样的问题。我调用下面的函数来获取坐标。
import { Plugins, Capacitor } from '@capacitor/core';
options = {
enableHighAccuracy: true,
timeout: 5000,
maximumAge: 0
};
public locateUser() {
Plugins.Geolocation.getCurrentPosition(this.options)
.then(geoPosition => {
const coordinates: Coordinates = {
lat: geoPosition.coords.latitude,
lng: geoPosition.coords.longitude
};
console.log(coordinates.lat , coordinates.lng); // Always prints the old coordinate even if the device is in a different location
})
.catch(err => {
this.isLoading = false;
this.showErrorAlert();
});
}
// I also tried this function below but the same result:
async getCurrentPosition() {
const coordinates = await Plugins.Geolocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 10000,
maximumAge: 0
});
alert( coordinates.coords.latitude);
alert( coordinates.coords.longitude);
}
【问题讨论】:
标签: angular ionic-framework geolocation capacitor