React Native GeoLocation 不适用于 Android
问题描述:
谁能确认 React Native Geolocation 是否真的在 Android 上运行?我在使用 getCurrentPosition()
方法时定位请求超时,并且在使用 watchPosition()
方法时没有错误消息.
Can anyone confirm if React Native Geolocation is actually working on Android?
I am stuck with location request timed out while using getCurrentPosition()
method and no error message while using watchPosition()
method.
我已在 AndroidManifest.xml 上添加了所需的权限 (FINE LOCATION),并且应用程序允许位置权限.
I have added the required permission (FINE LOCATION) on AndroidManifest.xml and location permission is allowed for the app.
这是我的实现:
componentWillMount() {
navigator.geolocation.getCurrentPosition((position) => {
console.log('+++++++');
console.log(position.coords.longitude);
this.setState({
longitude: position.coords.longitude,
error: null,
});
},
(error) => {
console.log(error);
},
);
}
反应原生版本:0.42
React Native Version : 0.42
答
你需要把enableHighAccuracy改为false
you need to change enableHighAccuracy to false
const LATITUDE_DELTA = 0.0922;
const LONGITUDE_DELTA = LATITUDE_DELTA * ASPECT_RATIO;
this.state = {
region:{}}
navigator.geolocation.getCurrentPosition(
(position) => {
console.log(position);
this.setState({
region: {
longitude: position.coords.longitude,
latitude: position.coords.latitude,
latitudeDelta: LATITUDE_DELTA,
longitudeDelta: LONGITUDE_DELTA
}
});
},
(error) => console.log(new Date(), error),
{enableHighAccuracy: false, timeout: 10000, maximumAge: 3000}
);