我有一个使用 Android 5.1.1 并使用 react-native-cli@2.0.1 的真实设备。
我要使用按钮来获得单击按钮的当前位置
navigator.geolocation.getCurrentPosition(
location => this.setState({ msg: location.coords.latitude }),
err => this.setState({ msg: err.code })
);
解析为2(= POSITION_UNAVAILABLE)。
到目前为止我尝试过的事情:
targetSdkVersion
设置为20
PermissionsAndroid.check(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
)
.then(granted => {
this.setState({ location: granted });
})
.catch(err => {
Alert.alert("Err");
});
denied
async requestLocationPermission() {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: "Location Permission",
message: "This app needs access to your location"
}
);
this.setState({ msg: granted });
} catch (err) {
this.setState({ msg: "err" });
}
}
<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
添加到AndroidManifest.xml