如何解决电容器定位和背景定位,清除不工作
我正在尝试在 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 举报,一经查实,本站将立刻删除。