Description
Environment
Windows 10
Platforms
android using expo
Versions
"react-native-get-location": "^5.0.0",
- react-native: 0.77.0
- react:
Description
[TypeError: Cannot read property 'getCurrentPosition' of null]. it raises an error like this, it means couldn't get the coordinates. try the maximum
I am currently using the react native lates where folder structures are changed. I am developing an app meaning while I need to access the current location of the app how It does not work I tried all the maxims. note I have includedthe androidmanifest.xml file also.
Reproducible Demo
import { PermissionsAndroid, Platform, StyleSheet, Text, View } from 'react-native';
import React, { useEffect, useState } from 'react';
import GetLocation from 'react-native-get-location';
const Create = () => {
const [getPermission, setPermission] = useState(false);
const [location, setLocation] = useState(null); // Set to null initially
const [isLoading, setIsLoading] = useState(true); // To handle loading state
useEffect(() => {
// Handle location permission and location fetching
const fetchPermissionAndLocation = async () => {
const permissionGranted = await _getLocationPermission();
if (permissionGranted) {
_getCurrentLocation();
} else {
setIsLoading(false); // Stop loading if permission is denied
}
};
fetchPermissionAndLocation();
}, []);
async function _getLocationPermission() {
if (Platform.OS === 'android') {
try {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'DCA App GPS Permission',
message: 'DCA App needs access to your location',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
}
);
if (granted === PermissionsAndroid.RESULTS.GRANTED) {
console.log("Permission granted");
setPermission(true);
return true;
} else {
console.log('Location permission denied');
setPermission(false);
return false;
}
} catch (err) {
console.warn(err);
setPermission(false);
return false;
}
}
}
function _getCurrentLocation() {
if (GetLocation) {
GetLocation.getCurrentPosition({
enableHighAccuracy: true,
timeout: 60000,
})
.then(location => {
console.log(location);
setLocation(location);
setIsLoading(false); // Stop loading after fetching location
})
.catch(error => {
console.warn(error);
setIsLoading(false); // Stop loading if there's an error
});
} else {
console.warn('GetLocation not properly initialized');
setIsLoading(false);
}
}
return (
<View style={styles.container}>
<Text style={styles.text}>Create</Text>
{isLoading && <Text style={styles.success}>Loading...</Text>}
{getPermission === false && !isLoading && (
<Text style={styles.warning}>Permission not granted</Text>
)}
{getPermission === true && location === null && !isLoading && (
<Text style={styles.success}>Permission granted, fetching location...</Text>
)}
{getPermission === true && location !== null && !isLoading && (
<Text style={styles.success}>
Permission granted, location: {location.latitude}, {location.longitude}
</Text>
)}
</View>
);
};
export default Create;
const styles = StyleSheet.create({
container: {
flex: 1,
justifyContent: 'center',
alignItems: 'center',
backgroundColor: '#f8f8f8',
},
text: {
color: 'black',
fontSize: 20,
fontWeight: 'bold',
},
warning: {
color: 'red',
fontSize: 16,
},
success: {
color: 'green',
fontSize: 16,
},
});