React Native Maps followUserLocation 坐标

React Native Maps followUserLocation coordinates

大家好,我需要一些帮助来了解如何在启用

时获取用户的坐标

followUserLocation

MapView

。有没有办法做到这一点,或者是否有任何可能的替代方案?

我已经做过这样的事情了:

import {DeviceEventEmitter} from 'react-native';
import RNALocation from 'react-native-android-location';
import MapView from 'react-native-maps';

componentDidMount() {
    DeviceEventEmitter.addListener('updateLocation', function(e: Event) {
      console.log(e);
      this.setState({lng: e.Longitude, lat: e.Latitude });
    }.bind(this));
    RNALocation.getLocation();
 }

已经使用 React Native Geolocation https://facebook.github.io/react-native/docs/geolocation.html 但似乎没有任何效果。它只给我用户的初始位置,当用户位置发生变化时它不会给我一些更新。

我什至使用 setInterval 继续调用 API 所以它可以给我关于用户位置的新更新,但坐标仍然没有改变。

我使用假 GPS 来移动我的位置或模拟我的位置。 .请帮忙,非常感谢。

不管怎样,我已经把它全部整理好了。由于我无法使用移动设备的 GPS 来收听用户位置。我所做的是使用 React Native geolocation

中的代码
navigator.geolocation.getCurrentPosition(
  (position) => {
    var initialPosition = JSON.stringify(position);
    this.setState({initialPosition});
  },
  (error) => alert(JSON.stringify(error)),
  {enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);

每 1 秒循环一次,然后在坐标发生变化时更新坐标状态(纬度和经度)。到目前为止它工作顺利。 :)

您可以使用 navigator.geolocation.watchPosition (https://developer.mozilla.org/en-US/docs/Web/API/Geolocation/watchPosition)

const options = options = {
  enableHighAccuracy: false,
  timeout: 5000,
  maximumAge: 0
};
navigator.geolocation.watchPosition((lastPosition) => {
    this.setState({lng: lastPosition.coords.longitude, lat: lastPosition.coords.latitude });
}, (error) => {
  console.log('Error', error)
}, options);