Suj*_*jay 7 android react-native react-native-maps expo
我刚刚将我的应用程序从 Expo SDK 37.0.0 升级到 38.0.0。该应用程序在 iOS 上运行良好,但在 Android 上我收到以下警告,并且该应用程序无法在地图上对我进行地理定位。
开发环境:
预期结果:应用程序应根据我的位置对我(用户)进行地理定位。实际结果:应用程序不会根据我的位置对我(用户)进行地理定位。警告 :
[未处理的承诺拒绝:错误:位置提供商不可用。确保定位服务已启用。]
到目前为止我尝试过什么?
再次运行 expo 升级以确保所有软件包都设置为正确的版本。
删除 package-lock.json 和 node_modules 并运行 npm install。
设置 Location.getCurrentPositionAsync({accuracy:Location.Accuracy.High})
设置 Location.getCurrentPositionAsync({ enableHighAccuracy: true })
import * as Location from 'expo-location';
import * as Permissions from 'expo-permissions';
async _getLocationAsync() {
let { status } = await Permissions.askAsync(Permissions.LOCATION);
if (status !== 'granted') {
/* If user hasn't granted permission to geolocate himself herself */
Alert.alert(
"User location not detected",
"You haven't granted permission to detect your location.",
[{ text: 'OK', onPress: () => console.log('OK Pressed') }]
);
}
let location = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High });
this.setState({
userLat: location.coords.latitude,
userLng: location.coords.longitude,
navGeoStatus: true,
lat: location.coords.latitude,
lng: location.coords.longitude
});
}```
Run Code Online (Sandbox Code Playgroud)
小智 6
如果有人仍在解决这个问题 - 请检查您是否添加了:
"android": {
"permissions": ["ACCESS_BACKGROUND_LOCATION"]
},
Run Code Online (Sandbox Code Playgroud)
在你的app.json文件中
小智 5
改变
let location = await Location.getCurrentPositionAsync({});Run Code Online (Sandbox Code Playgroud)
为了:
let location = await Location.getLastKnownPositionAsync({
accuracy: 6,
});Run Code Online (Sandbox Code Playgroud)
Kha*_*din -1
用户似乎已授予应用程序使用位置的权限。但当手机本身的定位服务尚未启用时,就会出现此警告。在设置中搜索并启用它
编辑后的答案(添加代码来处理警告):
let gpsServiceStatus = await Location.hasServicesEnabledAsync();
if (gpsServiceStatus) {
let location = await Location.getCurrentPositionAsync({ accuracy: Location.Accuracy.High });
this.setState({
userLat: location.coords.latitude,
userLng: location.coords.longitude,
navGeoStatus: true,
lat: location.coords.latitude,
lng: location.coords.longitude
});
} else {
alert("Enable Location services"); //or any code to handle if location service is disabled otherwise
}
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
11202 次 |
| 最近记录: |