位置请求在具有React-Native的Android模拟器上超时

man*_*anu 10 android geolocation android-emulator react-native

我正在使用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
Run Code Online (Sandbox Code Playgroud)

代码非常简单,所以我不认为错误来自这里.我在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>
Run Code Online (Sandbox Code Playgroud)

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

我怎样才能解决这个问题 ?

Bur*_*zen 6

set是enableHighAccuracy false,问题就解决了

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


man*_*anu 2

这个问题在这个线程中得到了回答。如何在Android模拟器中模拟GPS位置? 抱歉重复。我使用 telnet 解决了我的问题。