Agontuk / react-native-geolocation-service

React native geolocation service for iOS and android
https://www.npmjs.com/package/react-native-geolocation-service
MIT License
1.61k stars 291 forks source link

GetCurrentLocation doesn't work on Android #324

Closed Mariam-K-code-care closed 2 years ago

Mariam-K-code-care commented 2 years ago

Hello, the issue is the same as here, was wondering if there are any solutions on this repo for this

mashad6 commented 2 years ago

Having same problem, any solution on this?

CoryBall commented 2 years ago

I was having the same problem, my old code was this (if yours is similar then maybe my solution will work for you):

Note: the hasLocationPermission() function was directly copied from the package's example app

const hasPermission = await hasLocationPermission();

if (!hasPermission) {
  return;
}

let tryLocation: GeoPosition | undefined = undefined;

Geolocation.getCurrentPosition(
  (position: GeoPosition) => {
    tryLocation = position;
  },
  (error: GeoError) => {
    console.error(error);
  },
  {
    accuracy: {
      android: 'high',
      ios: 'best',
    },
    enableHighAccuracy: true,
    timeout: 1000 * 60 * 2,
    maximumAge: 1000 * 10,
    distanceFilter: 0,
  } as GeoOptions,
);

if (!tryLocation) {
  return;
}

const location = tryLocation as GeoPosition;

The issue was that it was just continuing through the Geolocation.getCurrentPosition function and not hitting a callback before moving on to when I used location

I wrapped the function in a promise and it is working as expected now. Try it out

export async function getCurrentLocation(): Promise<GeoPosition | undefined> {
  const hasPermission = await hasLocationPermission();

  if (!hasPermission) {
    return;
  }
  return new Promise(resolve => {
    Geolocation.getCurrentPosition(
      (position: GeoPosition) => {
        resolve(position);
        return position;
      },
      (geoError: GeoError) => {
        console.error(geoError);
        return;
      },
      {
        accuracy: {
          android: 'high',
          ios: 'best',
        },
        timeout: 1000 * 4,
        maximumAge: 1000 * 10,
        distanceFilter: 0,
      } as GeoOptions,
    );
  });
}
Agontuk commented 2 years ago
const hasPermission = await hasLocationPermission();

if (!hasPermission) {
  return;
}

let tryLocation: GeoPosition | undefined = undefined;

Geolocation.getCurrentPosition(
  (position: GeoPosition) => {
    tryLocation = position;
  },
  (error: GeoError) => {
    console.error(error);
  },
  {
    accuracy: {
      android: 'high',
      ios: 'best',
    },
    enableHighAccuracy: true,
    timeout: 1000 * 60 * 2,
    maximumAge: 1000 * 10,
    distanceFilter: 0,
  } as GeoOptions,
);

if (!tryLocation) {
  return;
}

const location = tryLocation as GeoPosition;

This'll not work because getCurrentPosition is an async method. You're synchronously declaring variable location before the method returning any result.

Closing this as there's no sample repo to reproduce this.