ped*_*oto 12 android google-maps geolocation reactjs react-native
我在我的应用程序上使用airbnb的地图作为react-native.这个问题适用于Android应用程序,因为我还没有绕过iOS应用程序.
我的问题:
navigator.geolocation在模拟器上工作正常,但在真实设备上花费的时间太长,以至于它有时会在旧设备上超时.
我注意到了什么:
如果showsUserLocationprop设置为true,我可以在getCurrentPosition结算之前在地图上看到原生的"当前位置"标记.
我想要的是:
showsUserLocation道具的行为,但似乎一旦watchPostion错误,它就完全停止了.这是我到目前为止所做的,它非常简单,因为它是:
componentDidMount() {
navigator.geolocation.getCurrentPosition(
(position) => {
console.log("getCurrentPosition Success");
this.setState({
region: {
...this.state.region,
latitude: position.coords.latitude,
longitude: position.coords.longitude,
}
});
this.props.resetNotifications();
this.watchPosition();
},
(error) => {
this.props.displayError("Error dectecting your location");
alert(JSON.stringify(error))
},
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
}
watchPosition() {
this.watchID = navigator.geolocation.watchPosition(
(position) => {
console.log("watchPosition Success");
if(this.props.followUser){
this.map.animateToRegion(this.newRegion(position.coords.latitude, position.coords.longitude));
}
},
(error) => {
this.props.displayError("Error dectecting your location");
},
{enableHighAccuracy: true, timeout: 20000, maximumAge: 1000}
);
}
Run Code Online (Sandbox Code Playgroud)
我一直觉得watchPosition不同浏览器之间玄乎,一件事之前,我工作是更换getCurrentPosition内部setInterval,使其获得位置每隔几秒钟,如果一旦将重试下一个区间失败,不像watchPosition其停止看,如果一个发生错误.
一般浏览器地理定位不是很可靠,你可以查看这个线程的一些问题https://github.com/facebook/react-native/issues/7495
另一种肯定会更快,也许更可靠的替代方案是使用原生地理定位API.检查此问题是否为暴露本机地理位置的库以反应本机React-native Android地理位置
| 归档时间: |
|
| 查看次数: |
3615 次 |
| 最近记录: |