【问题标题】:react native navigator.geolocation.getCurrentPosition not working反应本机 navigator.geolocation.getCurrentPosition 不起作用
【发布时间】:2018-04-30 06:10:19
【问题描述】:

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

我可以使用 react-native-maps 确定我的位置(在我的应用程序内的地图上正确的蓝点位置)+ 我可以使用谷歌地图应用程序并转到我的位置(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);

我得到:ERROR(3): Location request timed out

【问题讨论】:

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


    【解决方案1】:

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

    地理位置已从 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));
    

    【讨论】:

    • {enableHighAccuracy: true, timeout: 20000, maximumAge: 10000} 是怎么回事?
    • 这很奇怪,我不知道为什么我们无法使用此地理位置当前位置获得正确坐标的最佳解释在哪里
    【解决方案2】:

    两件事。

    1) 如果没有缓存坐标,那么对于高精度响应来说,这实际上并不是一个高超时 2) 某些设备的高精度设置存在问题。

    尝试将 30000 毫秒设置为超时并移除高精度,直到找到一个有效的设置。

    编辑:我从 React Native 中发现了我记忆中的长错误:https://github.com/facebook/react-native/issues/7495

    那么,试试这个:

    1) 移除 maximumAge 属性 2)如果这不起作用,请删除第三个参数并使用本机模块中的默认值。意思是,不要发送选项对象。那应该行得通。

    【讨论】:

    • 感谢您的回复。我只是从这里粘贴代码:developer.mozilla.org/fr/docs/Web/API/Geolocation/…。但我使用不同的设置 `{ enableHighAccuracy: false, timeout: 50000, maximumAge: 0 }` 并且失败了。
    • 我得到了这个结果:imgur.com/a/JL40m。打开页面几秒钟后。但我无法以编程方式获取任何数据。
    • 我不认为这是高精度,因为直径太大。您的手机中是否启用了gps?您是否已授予该应用程序对精细位置许可的访问权限? (在清单和应用的安全页面中)
    • 我已将 enableHighAccuracy 切换为 false 和 true。是的,我已经添加: 到 AndroidManifest.xml 文件,我仍然得到同样的错误。如果您有兴趣,我愿意分享代码。
    • 移除maximumAge设置并重试。不是 0,去掉就行了。
    【解决方案3】:

    删除 maximumAge 解决了这里的问题!以防万一有人在 2019 年或以后仍然遇到此问题

    【讨论】:

      【解决方案4】:

      要请求获取位置信息,您需要将以下行添加到应用的 AndroidManifest.xml:&lt;uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" /&gt;

      对于具有 API >= 23 的 android,您需要额外的步骤来检查:您需要使用 PermissionsAndroid API 请求 ACCESS_FINE_LOCATION 权限。

      PermissionsAndroid.request(
            PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
            {
              title: "Location Accessing Permission",
              message: "App needs access to your location"
            }
          );
      

      只有在那之后运行navigator.geolocation.getCurrentPosition(success, error, options);

      【讨论】:

        【解决方案5】:

        我用过它,它工作正常:{enableHighAccuracy: false, timeout: 50000}

        async watchPosition() {
            console.log('watchPosition');        
            await navigator.geolocation.getCurrentPosition(
                (position) => {
                  console.log('Position -> ',position);
                },
                (error) => console.log(error)
                {enableHighAccuracy: false, timeout: 50000}
            );
        }
        

        【讨论】:

          猜你喜欢
          • 2018-07-16
          • 1970-01-01
          • 2016-07-20
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          相关资源
          最近更新 更多