Location request timed out on android emulator with React

I'm building my first android app with react-native. What I want to do is display my current location . But it doesn't seem to work on my emulator . Instead I get a "Location request timed out" error in the console. Here is a sample of code from my idex.android.js file:

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

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

The code is very simple so I don't think the error comes from here. I added these permission in my AndroidManifest.xml file (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>

I don't see anything wrong here either, so I think the problem comes from the Android emulator. I use Android Virtual Device Manager to create a Nexus 5, Google APIs (Google Inc.) - API Level 23 virtual device. The geolocation is activated on the device but I did not see any geolocation tool in the developper tools or configuration. So probably the error comes from here. The virtual device does'nt return any location.

How can I fix this ?


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

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

This question is answered in this thread. How to emulate GPS location in the Android Emulator? Sorry for the duplicate. I fixed my problem using telnet.

链接地址: http://www.djcxy.com/p/90644.html

上一篇: 如何解决获取纬度或GPS空指针异常?

下一篇: 使用React的位置请求在Android模拟器上超时