geolocation - 电容器地理定位和背景地理定位,清除不工作
问题描述
我正在尝试在 Capacitor V3中使用 Geolocation 和 Background Gelocation ( https://github.com/capacitor-community/background-geolocation )。但是,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 以等待 promise 完成
推荐阅读
- c - X11/Xlib:虚拟键盘输入和键盘映射同步问题
- javascript - 多个输入循环减慢 each() 和 ajax()
- c++ - 根据 C++17 标准,为什么反向迭代器不是正式的迭代器类别?
- r - 异常记录
- jquery - JQUERY $(this).next issue
- python - 如何在 Python 和 Plotly 中使用下拉菜单进行数据选择
- java - 无法在项目上运行 Gradle 任务
- ios - iOS:退出后 ADAL 自动登录
- flowtype - flowtype 中的自引用泛型类型
- machine-learning - 我无法使用 python 在 sklearn (scikit-learn) 中训练模型