1

I can't get the 'react-native-geolocation-service' library to work properly.

My problem is that from my position I always get the same coordinates:

Latitude: 37.4226711 Longitude: -122.0849872

Which are the google default coordinates when you can't find the location. (These latitude and longitude values correspond to Mountain View, California, the headquarters of Google).

(I use React Native CLI, not Expo)

In my "AndroidManifest.xml" I set the permissions correctly like this:

 <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" />
 <uses-permission android:name="android.permission.ACCESS_BACKGROUND_LOCATION" />

And on the location of Android Studio I put a location chosen by me (for example in Italy) and clicking on "SET LOCATION":

enter image description here

This is my code:

import React, {useEffect, useState} from 'react';
import {View, Text, StyleSheet, Button} from 'react-native';
import Geolocation from 'react-native-geolocation-service';
import {check, PERMISSIONS, request} from 'react-native-permissions';

const LocationComponent = () => {
  const [latitude, setLatitude] = useState(null);
  const [longitude, setLongitude] = useState(null);

  const CheckPermission = () => {
    check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
      .then(result => {
        if (result === 'granted') GetPosition();
        else {
          request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION)
            .then(newResult => {
              if (newResult === 'granted') GetPosition();
            })
            .catch(error => {
              console.log(error);
            });
        }
      })
      .catch(error => {
        console.log(error);
      });

    const GetPosition = () => {
      Geolocation.getCurrentPosition(
        async position => {
          console.log('Coords', position.coords);

          const {latitude, longitude} = position.coords;

          setLatitude(latitude);
          setLongitude(longitude);
        },
        error => {
          console.log(error.code, error.message);
        },
        {enableHighAccuracy: true, timeout: 15000, maximumAge: 10000},
      );
    };
  };

  return (
    <View style={styles.container}>
      <Button title="Get Position" onPress={CheckPermission} />
      {location ? (
        <View>
          <Text style={styles.locationText}>Latitude: {latitude}</Text>
          <Text style={styles.locationText}>Longitude: {longitude}</Text>
        </View>
      ) : (
        <Text style={styles.locationText}>Getting location...</Text>
      )}
    </View>
  );
};

const styles = StyleSheet.create({
  container: {
    flex: 1,
    justifyContent: 'center',
    alignItems: 'center',
  },
  locationText: {
    fontSize: 18,
  },
});

export default LocationComponent;

I just can't figure out why I always have the same Google default coordinates, what am I doing wrong?

1
  • same problem here. were you able to solve it? Edit: the answer was the cache.. as described here: stackoverflow.com/a/68464142/1199196 Commented Sep 29, 2023 at 12:46

0

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.