【发布时间】:2021-01-10 09:23:21
【问题描述】:
我在我的 react 本机代码中使用 Geolocation 来获取当前位置,它在 Android 上运行良好。但不适用于 iOS。
这是我的代码
import Geolocation from "@react-native-community/geolocation";
import Geocoder from "react-native-geocoder";
_getCurrentLocation = () => {
Alert.alert(
"Get Location",
"Do you want to update your location?",
[
{
text: 'Cancel',
onPress: () => console.log('Cancel Pressed'),
style: 'cancel'
},
{
text: 'UPDATE', onPress: () => {
Geolocation.setRNConfiguration({ skipPermissionRequests: true, authorizationLevel: "always" });
if (Platform.OS === 'ios') Geolocation.requestAuthorization();
Geolocation.getCurrentPosition(
info => {
const position = { lat: info.coords.latitude, lng: info.coords.longitude }
Geocoder.geocodePosition(position).then(res => {
console.warn(res);
const { updateUserState } = this.props;
updateUserState({ position : position, country : res[0].countryCode, city : res[0].adminArea });
this.setState({ position : position, country : res[0].countryCode, city : res[0].adminArea });
})
.catch(err => {
console.warn(err)
alert(JSON.stringify(err));
})
},
error => {
// alert("Sorry, we cann't get your location now, please check your permission!")
alert(JSON.stringify(error));
console.log(error)
},
{
enableHighAccuracy: false,
timeout: 3000,
// maximumAge: 1000,
},
)
}
}
],
{ cancelable: false }
)
}
我收到以下错误:
PERMISSION_DENIED:1 POSITION_UNAVAILABLE:2 超时:3 代码:3 消息:“无法在 15.0 秒内获取位置。”
这是我的 info.plist:
<key>NSLocationAlwaysUsageDescription</key>
<string>This app requires access to you location.</string>
<key>NSLocationWhenInUseUsageDescription</key>
<string>This app requires access to you location.</string>
【问题讨论】:
标签: ios react-native mobile geolocation react-native-ios