我正在尝试使用地理定位和背景定位(https://github.com/capacitor-community/background-geolocation)在电容器V3中。但是,clearWatch/removeWatcher操作不起作用。尽管成功回调有效,但仓位观察并未停止。该系统与传单相结合。
代码如下;
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();
})
}
}
添加this.watchId=await BackgroundGeolocation.addWatcher({…缺少定义await以等待承诺完成