【发布时间】:2018-07-05 09:02:43
【问题描述】:
讨论和回答后的简要总结:
使用 EXPO sdk 如果没有在 android 中授予 FINE_LOCATION,您将无法获取设备位置。 FINE_LOCATION 是获取位置的唯一方法,因此您无法获取hardware.network.location。这意味着:android > 6 您无法使用 WIFI/移动网络获取当前位置,您必须启用 Location。
世博github讨论:https://github.com/expo/expo/issues/1339
最初的问题:
我正在开发一个 react-native 应用程序,使用 expo 和 react-native-maps,我需要获取用户当前位置的纬度和经度。
我为此使用navigator.geolocation API
在打开 GPS 的情况下,我没有任何问题,但我需要根据网络提供商在没有 GPS 的情况下获取当前位置。
问题是当应用程序运行 with expo on android > 6 我得到这个错误:
21:50:53:在 449 毫秒内完成构建 JavaScript 包 21:50:55: 定位服务被禁用 - node_modules\react-native\Libraries\BatchedBridge\NativeModules.js:80:57 在 - node_modules\react-native\Libraries\BatchedBridge\MessageQueue.js:347:19 在 __invokeCallback - ... 来自框架内部的另外 4 个堆栈帧
在 IO 和 android
这是组件的代码:
class MyComponent extends Component {
componentWillMount = () => {
this.getCurrentLocation();
}
getCurrentLocation = () =>
navigator.geolocation.getCurrentPosition(
(position) => {
let currentUserPosition = position.coords;
alert(JSON.stringify(currentUserPosition));
},
(error) => {
console.log(error);
},
{
enableHighAccuracy: false,
timeout: 20000,
maximumAge: 0,
distanceFilter: 10
}
);
}
这是我的 package.json 依赖项:
"dependencies": {
"expo": "23.0.4",
"native-base": "^2.3.5",
"react": "16.0.0",
"react-native": "0.50.4",
"react-native-extend-indicator": "^0.1.2",
"react-native-maps": "^0.19.0",
"react-native-maps-directions": "^1.3.0",
"react-native-router-flux": "4.0.0-beta.21",
"react-navigation": "1.0.0-beta.21",
"react-navigation-redux-debouncer": "^0.0.2",
"react-redux": "^5.0.6",
"redux": "^3.7.2",
}
我希望 navigator.geolocation 在那种情况下(没有 gps)根据网络提供商获取位置,规范说..
我也尝试过使用 expo 的 Geolocation API(试过这个例子:https://snack.expo.io/@schazers/expo-map-and-location-example),在 GPS 关闭的情况下,我无法获取我的位置..
所以..有没有办法实现我想要的?我错过了什么?
编辑(世博会贡献者回答):
我在 expo github (https://github.com/expo/expo/issues/1339) 上发布了相同的内容,根据他们的说法,如果没有在 android 设备中启用任何级别的 Location ,使用 navigator.geolocation 获取当前位置是不可能的.. 所以 .. 唯一的事情可能发生的情况是,低于 5 的 android 版本默认启用位置,您可以只打开 GPS,而版本 6 及更高版本您必须指定位置级别..
有什么想法吗?
EDIT2(重要!!):
我已确认:
这是 Android 6 设备的安全设置,默认情况下它使用 GPS,我认为 android 5 或更低版本没有,所以事情就是这样。当我使用第二个选项时,它会告诉我位置!
事实上,强制用户启用位置功能就像“嘿,使用你的 GPS!”,并且有很多应用程序可以在不启用位置功能的情况下为您提供近似位置(例如优步),所以问题是,有没有办法只使用 wifi 获取这个 api 的位置?
【问题讨论】:
标签: react-native gps location expo