Ответ 1
Я испробовал каждое решение, предоставленное всеми вами и другими людьми, также в Интернете. Наконец-то я нашел решение. Вы можете попробовать этот плагин cordova-plugin-advanced-geolocation (https://github.com/Esri/cordova-plugin-advanced-geolocation) из ESRI. Но этот плагин будет работать для Android, а не IOS. Для IOS вы можете пойти с тем же старым подходом. то есть - используя this.geolocation.getCurrentPosition(...)
или this.geolocation.watchPosition(..)
.
Добавить cordova-plugin-advanced-geolocation Plugin Вот так: -
cordova plugin add https://github.com/esri/cordova-plugin-advanced-geolocation.git
затем
declare var AdvancedGeolocation:any;
//наверху класса
//**For Android**
if (this.platform.is('android')) {
this.platform.ready().then(() => {
AdvancedGeolocation.start((success) => {
//loading.dismiss();
// this.refreshCurrentUserLocation();
try {
var jsonObject = JSON.parse(success);
console.log("Provider " + JSON.stringify(jsonObject));
switch (jsonObject.provider) {
case "gps":
console.log("setting gps ====<<>>" + jsonObject.latitude);
this.currentLat = jsonObject.latitude;
this.currentLng = jsonObject.longitude;
break;
case "network":
console.log("setting network ====<<>>" + jsonObject.latitude);
this.currentLat = jsonObject.latitude;
this.currentLng = jsonObject.longitude;
break;
case "satellite":
//TODO
break;
case "cell_info":
//TODO
break;
case "cell_location":
//TODO
break;
case "signal_strength":
//TODO
break;
}
}
catch (exc) {
console.log("Invalid JSON: " + exc);
}
},
function (error) {
console.log("ERROR! " + JSON.stringify(error));
},
{
"minTime": 500, // Min time interval between updates (ms)
"minDistance": 1, // Min distance between updates (meters)
"noWarn": true, // Native location provider warnings
"providers": "all", // Return GPS, NETWORK and CELL locations
"useCache": true, // Return GPS and NETWORK cached locations
"satelliteData": false, // Return of GPS satellite info
"buffer": false, // Buffer location data
"bufferSize": 0, // Max elements in buffer
"signalStrength": false // Return cell signal strength data
});
});
} else {
// **For IOS**
let options = {
frequency: 1000,
enableHighAccuracy: false
};
this.watch = this.geolocation.watchPosition(options).filter((p: any) => p.code === undefined).subscribe((position: Geoposition) => {
// loading.dismiss();
console.log("current location at login" + JSON.stringify(position));
// Run update inside of Angular zone
this.zone.run(() => {
this.currentLat = position.coords.latitude;
this.currentLng = position.coords.longitude;
});
});
}
РЕДАКТИРОВАТЬ: Первая установка всегда идет хорошо. Но иногда вы можете получить ошибки без причины в последующих установках. Чтобы сделать эту ошибку (любую ошибку с этим плагином), уйдите. Выполните следующие действия:
1. Удалите этот плагин из вашего проекта (включая config.xml
и package.json
).
2. Удалить/удалить платформу android
.
3. Удалить папку plugins
.
4. Теперь переустановите этот плагин снова, следуя инструкциям выше.