【发布时间】:2017-04-17 05:12:50
【问题描述】:
我在我的应用程序上使用 airbnb's map 进行 react-native。这个问题是针对 android 应用的,因为我还没有绕过 iOS 应用。
我的问题:
navigator.geolocation 在模拟器上运行良好,但在真实设备上耗时太长,以至于在旧设备上有时会超时。
我注意到的:
如果 showsUserLocation 属性设置为 true,我可以在 getCurrentPosition 解析之前在地图上看到原生的“当前位置”标记。
我想要什么:
- 我希望我的区域始终以用户位置为中心。
- 我想要一种访问本地地理定位 api 的方法(应该是 更快?),或者有人告诉我我做错了什么。下边是 代码示例。
- 我希望即使在打开/关闭位置服务后仍能检测到用户的位置。这是
showsUserLocationprop 的行为,但似乎一旦 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}
);
}
【问题讨论】:
-
您应该带着您的设备离开办公室或家中进行位置测试。在真实设备中必须是
enableHighAccuracy: true。它对我来说很完美。 -
@pedropeixoto 我完全按照安装指南中的要求完成了所有操作,但 showsUserLocation 对我不起作用。你能帮帮我吗?
标签: android google-maps reactjs react-native geolocation