首页 > 解决方案 > 电容器地理定位和背景地理定位,清除不工作

问题描述

我正在尝试在 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();
      })
    }
  }

标签: geolocationcapacitorcapacitor-plugin

解决方案


添加 this.watchId = await BackgroundGeolocation.addWatcher({... 缺少定义 await 以等待 promise 完成


推荐阅读