问题描述
我正在尝试在 Capacitor V3 中使用地理定位和背景地理定位 (https://github.com/capacitor-community/background-geolocation)。但是, clearWatch/removeWatcher 操作不起作用。尽管成功回调有效,但位置监视并未停止。该系统与Leaflet相结合。
代码如下;
async startTracker() {
if (!this.tracking) {
const position = await Geolocation.getCurrentPosition();
this.trackingCoords.push(new L.LatLng(position.coords.latitude,position.coords.longitude));
this.polyline = L.polyline(this.trackingCoords,{
color: '#000'
});
this.polyline.addTo(this.map);
this.tracking_text = this.translate.instant('tracker.stop_tracking');
this.tracking = true;
if (this.platform.is('hybrid')) {
this.watchId = BackgroundGeolocation.addWatcher({
backgroundMessage: this.translate.instant('tracking.notification_message'),backgroundTitle: this.translate.instant('tracking.notification_title'),requestPermissions: true,stale: false,distanceFilter: 50
},result => {
this.trackingCoords.push(new L.LatLng(result.latitude,result.longitude));
this.polyline.setLatLngs(this.trackingCoords);
});
}
else {
this.watchId = Geolocation.watchPosition(
{
enableHighAccuracy: true,timeout: 5000,maximumAge: 100
},result => {
this.trackingCoords.push(new L.LatLng(result.coords.latitude,result.coords.longitude));
this.polyline.setLatLngs(this.trackingCoords);
});
}
}
else {
this.alertCtrl.create({
header: this.translate.instant('tracker.stop_tracking_title'),message: this.translate.instant('tracking.stop_tracking_message'),buttons: [
{
text: this.translate.instant('cancel'),role: 'cancel',handler: () => {
}
},{
text: this.translate.instant('confirm'),handler: () => {
if (this.platform.is('hybrid')) {
BackgroundGeolocation.removeWatcher({ id: this.watchId });
}
else {
Geolocation.clearWatch({ id: this.watchId });
}
this.tracking = false;
this.tracking_text = this.translate.instant('tracker.start_tracking');
this.map.removeLayer(this.polyline);
if (this.trackingCoords.length > 2) {
this.area = L.polygon(this.trackingCoords,{
color: '#3AC194',});
this.map.addLayer(this.area);
this.area.editing.enable();
this.area.surface = (L.GeometryUtil.geodesicArea(this.trackingCoords) / 1000000);
this.area.latlngs = (this.trackingCoords);
}
this.trackingCoords = [];
}
}
]
}).then((alert) => {
alert.present();
})
}
}
解决方法
暂无找到可以解决该程序问题的有效方法,小编努力寻找整理中!
如果你已经找到好的解决方法,欢迎将解决方案带上本链接一起发送给小编。
小编邮箱:dio#foxmail.com (将#修改为@)