【发布时间】:2019-12-25 11:58:46
【问题描述】:
我正在尝试在本机反应中获取设备当前位置。我正在使用下面的库来获取位置:
react-native-geolocation-service
我的代码:
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Device current location permission',
message:
'Allow app to get your current location',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
this.getCurrentLocation();
} else {
console.log('Location permission denied');
}
} catch (err) {
console.warn(err);
}
}
getCurrentLocation(){
Geolocation.requestAuthorization();
Geolocation.getCurrentPosition(
(position) => {
alert(position.coords.latitude);
this.socket.emit('position', {
data: position,
id: this.id,
});
},
(error) => {
alert("map error: "+error.message);
console.log(error.code, error.message);
},
{ enableHighAccuracy: true, timeout: 15000, maximumAge: 10000 }
);
}
Android 工作正常并获得正确的位置,但对于 IOS,它不工作此外,它没有要求允许位置权限。我收到以下错误:
PERMISSION_DENIED: 1
POSITION_UNAVAILABLE: 2
TIMEOUT: 3
code: 3
message: "Unable to fetch location within 15.0s."
这是我的 info.plist:
<key>NSLocationWhenInUseUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location.</string>
<key>NSLocationAlwaysUsageDescription</key>
<string>$(PRODUCT_NAME) would like to access your location.</string>
这是来自 xcode 的背景模式
【问题讨论】:
-
检查IOS配置权限
-
请检查我用 info.plist 和 xcode 背景模式更新的问题。
-
@RahulMishra 我也遇到了同样的问题,你解决了吗?
-
是的,我解决了它会在几分钟内分享代码
-
我在下面分享了我的代码
标签: react-native geolocation react-native-ios