嗨,大家好,我需要一些有关如何在启用时获取用户坐标的帮助
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地理位置中的代码
navigator.geolocation.getCurrentPosition(
(position) => {
var initialPosition = JSON.stringify(position);
this.setState({initialPosition});
},
(error) => alert(JSON.stringify(error)),
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
每隔1秒钟循环遍历一次,如果坐标发生变化,则更新坐标状态(纬度和经度)。到目前为止,它工作顺利。:)
本文收集自互联网,转载请注明来源。
如有侵权,请联系[email protected] 删除。
我来说两句