我在我的應用上使用airbnb's map作爲react-native。這個問題是爲Android應用程序,因爲我還沒有解決iOS應用程序。在React Native AirBnb的MapView上關注用戶位置[Android]
我的問題:
navigator.geolocation正常工作在模擬器,但時間過長在真實設備上,給點有時超時舊設備。
我已經注意到:
如果showsUserLocation
道具設置爲true,我可以看到在地圖上的方式本土「當前位置」標誌之前getCurrentPosition緩解。
我想要什麼:
- 我要永遠對用戶的位置爲中心我所在的地區。
- 我想要一種方法來訪問本地地理位置api(應該更快?),或者有人告訴我我做錯了什麼。以下是 的一個代碼示例。
- 即使位置服務已打開/關閉,我仍然能夠檢測到用戶的位置。這是
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}
);
}
您應該離開辦公室或家中使用設備進行位置測試。在真實設備中必須爲'enableHighAccuracy:true'。它對我來說是完美的。 –
檢查它這個問題http://stackoverflow.com/a/38125402/2955679 –
@pedropeixoto我做了所有安裝指南中要求的,但showsUserLocation不適用於我。你能幫我嗎。? – HelpingHand