Closed chxru closed 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,
});
};
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
Changing
import { watchPosition } from 'react-native-geolocation-service'
to
import Geolocation from 'react-native-geolocation-service';
Geolocation.watchPosition()
fixed the issue
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
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)