Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

react native navigator.geolocation.getCurrentPosition not working

I am using a real android device (version 5.1)

I am able to determine my position using react-native-maps (correct blue point position on map inside my app ) + I am able use google maps app and go to my position (GPS works).

I am using a big timeout, toggling enableHighAccuracy to true and false, remove options ... etc . all failed to get navigator.geolocation to get data.

Here is my code:

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);

I am getting : ERROR(3): Location request timed out

like image 625
Badis Merabet Avatar asked Nov 16 '17 16:11

Badis Merabet


1 Answers

I know it's too late, but it can help someone else.

Geolocation has been extracted from react native .60 version. If you need an alternative solution

Install react-native-community/geolocation :

npm install @react-native-community/geolocation --save

react-native link @react-native-community/geolocation

Then, to request access to location, you need to add the following line to your app's AndroidManifest.xml

<uses-permission android:name="android.permission.ACCESS_FINE_LOCATION" />

Usage :

import Geolocation from '@react-native-community/geolocation';

Geolocation.getCurrentPosition(info => console.log(info));
like image 52
Sandip Singh Avatar answered Oct 22 '22 14:10

Sandip Singh