10

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 ?

manu
  • 1,059
  • 1
  • 16
  • 33
  • 1
    Try http://stackoverflow.com/questions/2279647/how-to-emulate-gps-location-in-the-android-emulator – deanmcpherson Feb 10 '16 at 00:49
  • thank you, I don't know why I didn't find this question in the first place. – manu Feb 12 '16 at 12:19
  • **I am facing the same issue, did you find any solution for this?** any of the answer from here did not helped me. I am able to set location in emulator and I can see the results in google map in emulator, but for native it is just throwing a time out error. – manjs Nov 17 '17 at 12:08

2 Answers2

6

set is enableHighAccuracy false, the problem will be solved

navigator.geolocation.getCurrentPosition((position) => {
  console.log(position);
}, error => console.error(error), { enableHighAccuracy: false, timeout: 20000, maximumAge: 1000 });
Burak Demirezen
  • 545
  • 6
  • 12
2

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.

Community
  • 1
  • 1
manu
  • 1,059
  • 1
  • 16
  • 33