【问题标题】:Location request timed out on android emulator with React-Native带有 React-Native 的 android 模拟器上的位置请求超时
【发布时间】:2016-05-19 18:17:16
【问题描述】:

我正在使用 react-native 构建我的第一个 android 应用。 我想做的是显示我当前的位置。但它似乎不适用于我的模拟器。相反,我在控制台中收到“位置请求超时”错误。这是我的 idex.android.js 文件中的代码示例:

initGeoloc() {
   navigator.geolocation.getCurrentPosition(
     (position) => { 
       console.log(JSON.stringify(position));
     }
   );
}

render() {
   this.initGeoloc();
   ... some more code and the view

代码非常简单,所以我认为错误不是来自这里。 我在我的 AndroidManifest.xml 文件中添加了这些权限(ACCESS_FINE_LOCATION、ACCESS_COARSE_LOCATION):

<manifest xmlns:android="http://schemas.android.com/apk/res/android"
package="com.myapp">
    <uses-permission android:name="android.permission.INTERNET" />
    <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />
    <uses-permission android:name="android.permission.ACCESS_COARSE_LOCATION" />
    <application
       android:allowBackup="true"
       android:label="@string/app_name"
       android:icon="@mipmap/ic_launcher"
       android:theme="@style/AppTheme">
       <activity
         android:name=".MainActivity"
         android:label="@string/app_name"
         android:configChanges="keyboard|keyboardHidden|orientation|screenSize">
         <intent-filter>
             <action android:name="android.intent.action.MAIN" />
             <category android:name="android.intent.category.LAUNCHER" />
         </intent-filter>
       </activity>
       <activity android:name="com.facebook.react.devsupport.DevSettingsActivity" />
       <uses-library android:name="com.google.android.maps" />
       <meta-data android:name="com.google.android.geo.API_KEY" android:value="mykey"/>
</application>

我也没有发现任何问题,所以我认为问题出在 Android 模拟器上。我使用 Android Virtual Device Manager 创建 Nexus 5、Google APIs (Google Inc.) - API Level 23 虚拟设备。地理定位已在设备上激活,但我在开发工具或配置中没有看到任何地理定位工具。所以可能错误来自这里。 虚拟设备不返回任何位置。

我该如何解决这个问题?

【问题讨论】:

  • 谢谢,我不知道为什么我一开始没有找到这个问题。
  • 我也面临同样的问题,您找到解决方案了吗?这里的任何答案都没有帮助我。我可以在模拟器中设置位置,并且可以在模拟器中的谷歌地图中看到结果,但对于本机来说,它只是抛出一个超时错误。

标签: android android-emulator geolocation react-native


【解决方案1】:

设置为enableHighAccuracy false,问题就解决了

navigator.geolocation.getCurrentPosition((position) => {
  console.log(position);
}, error => console.error(error), { enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 });

【讨论】:

  • 请记住,enableHighAccuracy: false 可能会导致位置非常不准确。
【解决方案2】:

这个问题在这个帖子中得到了回答。 How to emulate GPS location in the Android Emulator? 对不起,重复。我使用 telnet 解决了我的问题。

【讨论】:

    猜你喜欢
    • 2017-04-28
    • 2021-03-14
    • 2017-11-13
    • 1970-01-01
    • 1970-01-01
    • 2020-06-30
    • 1970-01-01
    • 2016-11-29
    • 1970-01-01
    相关资源
    最近更新 更多