我正在使用反应本机地图和componentDidMount()
要求用户启用其位置。
componentDidMount() {
if (Platform.OS === 'android') {
RNAndroidLocationEnabler.promptForEnableLocationIfNeeded({ interval: 20000, fastInterval: 5000 })
.then(data => {
console.log(data);
this.findingLocation();
}).catch(err => {
});
}
else if (Platform.OS === 'ios') {
this.findingLocation();
}
}
And in findingLocation()
我正在尝试像这样获取用户的当前位置
findingLocation = () => {
Geolocation.getCurrentPosition(
position => {
this.setState({
region: {
latitude: position.coords.latitude,
longitude: position.coords.longitude,
latitudeDelta: 0.0043,
longitudeDelta: 0.0034,
}
}, () => {
console.warn(this.state.region);
});
},
error => console.warn('Error', JSON.stringify(error)),
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 10000 },
);
}
在控制台中,我得到了当前位置的经纬度。
但有时它不会反映在 UI 上。
UI code
<MapView
provider={PROVIDER_GOOGLE}
style={styles.map}
region={this.state.region}
onRegionChangeComplete={this.onRegionChange}
zoomEnabled={true}
showsUserLocation={true}
zoomControlEnabled={true}
showsMyLocationButton={true}
/>
我正在使用的库是https://github.com/react-native-community/react-native-maps https://github.com/react-native-community/react-native-maps对于地图和https://www.npmjs.com/package/@react-native-community/geolocation https://www.npmjs.com/package/@react-native-community/geolocation用于地理位置。
请帮帮我。
我哪里出错了。
提前致谢。