xantin
xantin

Reputation: 119

app crash when using Geolocation in React-Native

I am using react-native-geolocation-service, I have such function to get coords. And then by Geocoder api get the city by coords. I check the permission status by react-native-permissions:

export const getCity = async (dispatch: Dispatch<any>) => {
  const granted = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);

  if (granted === RESULTS.DENIED) {
    await PermissionsAndroid.request(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION, {
      buttonNegative: undefined,
      buttonNeutral: undefined,
      buttonPositive: '',
      title: 'Access',
      message: 'give access',
    });
  }

  if (granted === RESULTS.GRANTED || granted === RESULTS.LIMITED) {
    Geolocation.getCurrentPosition(
      position => {
        const coords: Coords = {
          Longitude: position.coords.longitude,
          Latitude: position.coords.latitude,
        };
        dispatch(thunks.geocoder.getGeocoderInfo(coords));
        Alert.alert('coords', `${coords.Latitude}, ${coords.Longitude}`);
      },
      error => Alert.alert('Error', `Error ${error.message}`),
      {
        enableHighAccuracy: false,
        timeout: 20000,
        maximumAge: 3600000,
      },
    );
  }
};

So, when my applications starts, I grant the permission to it, and then it just closes. Also I added access into AndroidManifest.xml: <uses-permission android:name="android.permission.ACCESS_FINE_LOCATION"/>. And then I call this function inside my component using UseEffect()

Upvotes: 1

Views: 1397

Answers (2)

zahoor
zahoor

Reputation: 1

This issue can be solved by upgrading your react native to 0.70.1, i just solved that problem and now the gelocaiton service is working fine in both debug and release apk. and dont forget to add use permission in androidmanifest.xml

Upvotes: 0

xantin
xantin

Reputation: 119

I solved my problem. I rewrite my code form question as follows:


type Coords = {
  coords: {
    accuracy?: number | null;
    altitude?: number | null;
    altitudeAccuracy?: number | null;
    heading?: number | null;
    latitude?: number | null;
    longitude?: number | null;
    speed?: number | null;
  };
  mocked?: boolean;
  provider?: string;
  timestamp?: number | null;
};

export const getCity = async (dispatch: Dispatch<any>) => {
  const granted = await check(PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION);
  let geoOptions = {
    enableHighAccuracy: true,
    timeOut: 20000,
    maximumAge: 60 * 60 * 24,
  };
  const geoSuccess = (position: Coords) => {
    const coords = {
      Latitude: position.coords.latitude,
      Longitude: position.coords.longitude,
    };

    dispatch(thunks.geocoder.getGeocoderInfo(coords));
  };

  const geoFailure = (err: any) => {
    console.log({error: err.message});
  };

  if (granted === RESULTS.DENIED) {
    const data = await PermissionsAndroid.request(
      PERMISSIONS.ANDROID.ACCESS_FINE_LOCATION,
      {
        buttonNegative: undefined,
        buttonNeutral: undefined,
        buttonPositive: '',
        title: 'Access',
        message: 'give access',
      },
    );
  }

  if (granted === RESULTS.GRANTED || granted === RESULTS.LIMITED) {
    Geolocation.getCurrentPosition(geoSuccess, geoFailure, geoOptions);
  }
};

So it solved my problem. App now doesn't crush.

Upvotes: 2

Related Questions