【问题标题】:React Native Location Tracker反应本机位置跟踪器
【发布时间】:2022-09-29 20:05:44
【问题描述】:

我正在实现此代码,它显示错误,例如“未定义不是对象(评估'navigator.geolocation.clearWatch')“

watchLocation = () => {
const { coordinate } = this.state;

this.watchID = navigator.geolocation.watchPosition(
  position => {
    const { latitude, longitude } = position.coords;

    const newCoordinate = {
      latitude,
      longitude,
    };

    if (Platform.OS === 'android') {
      if (this.marker) {
        this.marker._component.animateMarkerToCoordinate(newCoordinate, 500); // 500 is the duration to animate the marker
      }
    } else {
      coordinate.timing(newCoordinate).start();
    }

    this.setState({
      latitude,
      longitude,
    });
  },
  error => console.log(error),
  {
    enableHighAccuracy: true,
    timeout: 20000,
    maximumAge: 1000,
    distanceFilter: 30,
  }
);

};

谁能让我知道如何解决这个问题?我指的是本教程。 Link

【问题讨论】:

    标签: react-native location mobile-application pubnub


    【解决方案1】:

    首先安装:

    npm i @react-native-community/geolocation
    

    然后 -

    import Geolocation from '@react-native-community/geolocation';
    
    watchLocation = () => {
    const { coordinate } = this.state;
    
    this.watchID = Geolocation.watchPosition(
        //Your Code 
     );
    }
    

    供参考结帐 - https://www.npmjs.com/package/@react-native-community/geolocation

    我已经浏览了您正在关注的博客,因此您也必须进行更改:

    componentWillUnmount() {
      Geolocation.clearWatch(this.watchID);
    }
    

    【讨论】:

    • 谢谢您的帮助!但是我们可以在世博管理流程中使用它吗?
    猜你喜欢
    • 2019-03-31
    • 2020-07-05
    • 1970-01-01
    • 2011-04-05
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多