android - react native navigator.geolocation.getCurrentPosition 不工作

标签 android react-native geolocation navigator react-native-maps

我使用的是真正的 android 设备(5.1 版)

我可以使用 react-native-maps 确定我的位置(我的应用程序内 map 上正确的蓝点位置)+ 我可以使用谷歌地图应用程序并转到我的位置(GPS 工作)。

我正在使用大超时,将 enableHighAccuracy 切换为 true 和 false,删除选项......等等。均未能通过navigator.geolocation获取数据。

这是我的代码:

var options = {
  enableHighAccuracy: true,
  timeout: 5000,
  maximumAge: 0
};

function success(pos) {
  var crd = pos.coords;

  console.log('Your current position is:');
  console.log(`Latitude : ${crd.latitude}`);
  console.log(`Longitude: ${crd.longitude}`);
  console.log(`More or less ${crd.accuracy} meters.`);
};

function error(err) {
  console.warn(`ERROR(${err.code}): ${err.message}`);
};

navigator.geolocation.getCurrentPosition(success, error, options);

我得到:错误(3):位置请求超时

最佳答案

我知道为时已晚,但它可以帮助其他人。

地理定位已从 React Native .60 版本中提取。如果您需要替代解决方案

安装 react-native-community/geolocation :

npm install @react-native-community/geolocation --save

react-native link @react-native-community/geolocation

然后,要请求访问位置,您需要将以下行添加到应用的 AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

用法:

import Geolocation from '@react-native-community/geolocation';

Geolocation.getCurrentPosition(info => console.log(info));

关于android - react native navigator.geolocation.getCurrentPosition 不工作,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/47334159/

相关文章:

android - 找不到方法 commandLine()

java - Android ServerSocket端口、连接超时问题

android - 具有可绘制背景的自定义按钮 : Padding difference in Android 4 vs. Android 2.3

android - 当用户打开抽屉时调用函数(onClick 按钮或幻灯片)

android - 在应用程序启动期间获取当前位置

javascript - 在 Safari 上通过地理位置查找当前位置

java - 选项卡式 Activity 中的Android声音

react-native - React native redux saga 如何向用户显示错误

reactjs - ES6 - 根据真值过滤对象

c# - 如何将地理坐标解析为口语?