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.6k stars 291 forks source link

undefined is not a function (near '...(0, _reactNativeGeolocationService.watchPosition) #284

Closed chxru closed 3 years ago

chxru commented 3 years ago

Device Info: Android emulator: API 29 react-native-geolocation-service@5.3.0-beta.1 react-native@0.63.3

I tried the simplest code for Geolocation.watchPosition

const options: GeoOptions = {
  accuracy: {android: 'high'},
  enableHighAccuracy: true,
};

try {
  watchPosition(
    (position) => {
      console.log(position);
    },
    (error) => {
      console.error(error);
    },
    options,
  );
} catch (error) {
  console.error(error);
}

Neither IDE nor ESLint throw up errors, but when I run the app, this error appears.
[TypeError: undefined is not a function (near '...(0, _reactNativeGeolocationService.watchPosition)...')]

Same error for the getCurrentPosition

Had to wrap the watchPosition inside a try catch to get this error, otherwise the error message starts with "Possible Unhandled Promise Rejection". (see the below image)

image

khlling commented 3 years ago

I'm also getting the same error on iOS:

react-native-geolocation-service@5.3.0-beta.1

I both tried importing GeoLocation via direct import and also methods themselves:


import * as Geolocation from "react-native-geolocation-service";

export const hasLocationPermission = async () => {
  if (
    Platform.OS === "ios" ||
    (Platform.OS === "android" && Platform.Version < 23)
  ) {
    return true;
  }

  const hasPermission = await PermissionsAndroid.check(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
  );

  if (hasPermission) {
    return true;
  }

  const status = await PermissionsAndroid.request(
    PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION
  );

  if (status === PermissionsAndroid.RESULTS.GRANTED) {
    return true;
  }

  if (status === PermissionsAndroid.RESULTS.DENIED) {
    ToastAndroid.show("Location permission denied by user.", ToastAndroid.LONG);
  } else if (status === PermissionsAndroid.RESULTS.NEVER_ASK_AGAIN) {
    ToastAndroid.show(
      "Location permission revoked by user.",
      ToastAndroid.LONG
    );
  }

  return false;
};
/**
 * Gets current location based on GPS
 * @param onSuccess on Success callback function
 * @param onError on Error callback function
 */
export const getLocation = async (
  onSuccess: Geolocation.SuccessCallback,
  onError: Geolocation.ErrorCallback
) => {
  const permission = await hasLocationPermission();

  if (!permission) {
    return;
  }
  console.log("Geolocation", Geolocation);
  console.log("onSuccess", onSuccess);
  console.log("onError", onError);

  return Geolocation.getCurrentPosition(onSuccess, onError, {
    enableHighAccuracy: true,
    timeout: 15000,
    maximumAge: 10000,
    distanceFilter: 50,
    forceRequestLocation: true,
  });
};
Agontuk commented 3 years ago

Most probably a setup issue, try reinstalling the package again. Also check if react native picks up the package during pod install or react-native run-android

chxru commented 3 years ago

Changing

import {  watchPosition } from 'react-native-geolocation-service'

to

import Geolocation from 'react-native-geolocation-service';
Geolocation.watchPosition()

fixed the issue