如何在 Ionic 中获得 GPS ONLY 位置?
How can I get GPS ONLY location in Ionic?
我正在创建一个应用程序,我工作的公司的用户可以按需注册兴趣点,只需按下按钮并输入名称,然后将它们发送并存储到服务器。
但我对位置的准确性有疑问。我正在使用 @ionic-native/geolocation
,即使我将 enableHighAccuracy
设置为 true
,我也没有得到精确的位置。其中一些比他们拍摄的地方还要远几米。
有没有办法只从 GPS 传感器获取地理定位?
我已经搜索了 google 几个小时,但一无所获。也许我做错了什么。
这是我在客户端获取位置的代码:
obtenirPosicio(): Promise<Geoposicio> {
return new Promise<Geoposicio>((resolve, reject) => {
const OPCIONS_GPS = {} as GeolocationOptions;
OPCIONS_GPS.enableHighAccuracy = true;
OPCIONS_GPS.maximumAge = 0;
OPCIONS_GPS.timeout = 10000; // 10 segons de timeout per obtenir posicio GPS
this.gps.getCurrentPosition(OPCIONS_GPS).then(res => {
let nova_posicio = {} as Geoposicio;
nova_posicio.lat = res.coords.latitude;
nova_posicio.lng = res.coords.longitude;
resolve(nova_posicio);
}).catch(err => {
reject(err);
});
});
}
PS: 我只是瞄准Android
除非用户将定位模式设置为 "Device only"(即 GPS),当定位模式为 "High Accuracy" 时,Android 定位管理器将通过 GPS 和非 GPS 来源,因此有些 Wifi/Bluetooth/cell 三角测量位置会不准确。
但是,您可以通过查看结果的 coords.accuracy
属性 来过滤掉这些,这表明位置的估计精度。
确定您准备接受的最低准确度,然后拒绝任何低于此准确度的准确度。
例如:
obtenirPosicio(): Promise<Geoposicio> {
return new Promise<Geoposicio>((resolve, reject) => {
const MIN_ACCURACY = 20; // metres
const OPCIONS_GPS = {} as GeolocationOptions;
OPCIONS_GPS.enableHighAccuracy = true;
OPCIONS_GPS.maximumAge = 0;
OPCIONS_GPS.timeout = 10000; // 10 segons de timeout per obtenir posicio GPS
this.gps.getCurrentPosition(OPCIONS_GPS).then(res => {
// Reject udpate if accuracy is not sufficient
if(!res.coords.accuracy || res.coords.accuracy > MIN_ACCURACY){
console.warn("Position update rejected because accuracy of"+res.coords.accuracy+"m is less than required "+MIN_ACCURACY+"m");
return; // and reject() if you want
}
let nova_posicio = {} as Geoposicio;
nova_posicio.lat = res.coords.latitude;
nova_posicio.lng = res.coords.longitude;
resolve(nova_posicio);
}).catch(err => {
reject(err);
});
});
}
我正在创建一个应用程序,我工作的公司的用户可以按需注册兴趣点,只需按下按钮并输入名称,然后将它们发送并存储到服务器。
但我对位置的准确性有疑问。我正在使用 @ionic-native/geolocation
,即使我将 enableHighAccuracy
设置为 true
,我也没有得到精确的位置。其中一些比他们拍摄的地方还要远几米。
有没有办法只从 GPS 传感器获取地理定位?
我已经搜索了 google 几个小时,但一无所获。也许我做错了什么。
这是我在客户端获取位置的代码:
obtenirPosicio(): Promise<Geoposicio> {
return new Promise<Geoposicio>((resolve, reject) => {
const OPCIONS_GPS = {} as GeolocationOptions;
OPCIONS_GPS.enableHighAccuracy = true;
OPCIONS_GPS.maximumAge = 0;
OPCIONS_GPS.timeout = 10000; // 10 segons de timeout per obtenir posicio GPS
this.gps.getCurrentPosition(OPCIONS_GPS).then(res => {
let nova_posicio = {} as Geoposicio;
nova_posicio.lat = res.coords.latitude;
nova_posicio.lng = res.coords.longitude;
resolve(nova_posicio);
}).catch(err => {
reject(err);
});
});
}
PS: 我只是瞄准Android
除非用户将定位模式设置为 "Device only"(即 GPS),当定位模式为 "High Accuracy" 时,Android 定位管理器将通过 GPS 和非 GPS 来源,因此有些 Wifi/Bluetooth/cell 三角测量位置会不准确。
但是,您可以通过查看结果的 coords.accuracy
属性 来过滤掉这些,这表明位置的估计精度。
确定您准备接受的最低准确度,然后拒绝任何低于此准确度的准确度。
例如:
obtenirPosicio(): Promise<Geoposicio> {
return new Promise<Geoposicio>((resolve, reject) => {
const MIN_ACCURACY = 20; // metres
const OPCIONS_GPS = {} as GeolocationOptions;
OPCIONS_GPS.enableHighAccuracy = true;
OPCIONS_GPS.maximumAge = 0;
OPCIONS_GPS.timeout = 10000; // 10 segons de timeout per obtenir posicio GPS
this.gps.getCurrentPosition(OPCIONS_GPS).then(res => {
// Reject udpate if accuracy is not sufficient
if(!res.coords.accuracy || res.coords.accuracy > MIN_ACCURACY){
console.warn("Position update rejected because accuracy of"+res.coords.accuracy+"m is less than required "+MIN_ACCURACY+"m");
return; // and reject() if you want
}
let nova_posicio = {} as Geoposicio;
nova_posicio.lat = res.coords.latitude;
nova_posicio.lng = res.coords.longitude;
resolve(nova_posicio);
}).catch(err => {
reject(err);
});
});
}