【问题标题】:Expo Locations.watchPositionAsync not being removedExpo Locations.watchPositionAsync 未被删除
【发布时间】:2019-05-03 15:36:56
【问题描述】:

我正在构建一个旅行应用程序。应用程序中的屏幕之一是地图屏幕。我使用 Expo Locations 中的 watchPositionAsync。 watchPositionAsync 效果很好,可以传递用户坐标。一旦地图屏幕/模式关闭,我希望 watchPositionAsync 停止/删除。我将 .remove() 函数附加到 watchPositionAsync,但不幸的是 watchPositionAsync 继续运行。模态关闭后如何让它停止运行?

我尝试过以各种不同的方式使用 .remove(): - componentWillUnmount 等。 - 我什至尝试在按钮内执行它(onPress)

async componentWillMount() {

    const { status } = await Permissions.askAsync(Permissions.LOCATION);

    if (status === 'granted') {
      this._getLocationAsync();
    } else {
      this.setState({ error: 'Locations services needed' });
    }
  }

  componentWillUnmount() {
      this._stopGetLocationAsync().catch(err => console.log('err'));
  }

  _getLocationAsync = async () => {
      const location = await Location.watchPositionAsync(
          {
              enableHighAccuracy: true,
              distanceInterval: 1,
              timeInterval: 10000
          },
          newLocation => {
              let coords = newLocation.coords;
          // this.props.getMyLocation sets my reducer state my_location
          this.props.getMyLocation({
              latitude: parseFloat(coords.latitude),
              longitude: parseFloat(coords.longitude)
          });
        },
        error => console.log(error)
      );
      return location;
  };

  _stopGetLocationAsync = async () => {
    const location = await Location.watchPositionAsync();
    return location.remove();
  };

我希望在组件卸载后删除 watchPositionAsync,但是当我使用 console.log(location.remove()) 时,我得到了未定义。

【问题讨论】:

    标签: reactjs react-native geolocation gps expo


    【解决方案1】:

    在您的_stopGetLocationAsync 中,您正在创建一个新的watchPositionAsync 承诺对象并将其删除。因此_getPositionASync 中的原始版本仍然存在。

    您需要将 const Location 更改为类变量 i.e. this.location=...,然后将 remove 方法传递给该变量。

    这是修改后的代码

    async componentWillMount() {
    
        const { status } = await Permissions.askAsync(Permissions.LOCATION);
    
        if (status === 'granted') {
          this._getLocationAsync();
        } else {
          this.setState({ error: 'Locations services needed' });
        }
      }
    
      componentWillUnmount() {
         this.location.remove(callback...);
      }
    
      _getLocationAsync = async () => {
          this.location = await Location.watchPositionAsync(
              {
                  enableHighAccuracy: true,
                  distanceInterval: 1,
                  timeInterval: 10000
              },
              newLocation => {
                  let coords = newLocation.coords;
              // this.props.getMyLocation sets my reducer state my_location
              this.props.getMyLocation({
                  latitude: parseFloat(coords.latitude),
                  longitude: parseFloat(coords.longitude)
              });
            },
            error => console.log(error)
          );
          return location;
      };
    

    【讨论】:

      猜你喜欢
      • 2011-08-06
      • 1970-01-01
      • 2019-04-17
      • 1970-01-01
      • 1970-01-01
      • 2014-12-09
      • 1970-01-01
      • 2021-02-02
      • 1970-01-01
      相关资源
      最近更新 更多