Geolocation is unable to show current location
elkee2003 opened this issue · 1 comments
I used this same line of code in one of my project and it worked, but for some reason, i am unable to get current location in my currenet project.
`import { View, Image, useWindowDimensions, PermissionsAndroid, Platform, } from 'react-native'
import React, {useState,useEffect} from 'react'
import MapView, { PROVIDER_GOOGLE, Marker } from 'react-native-maps';
import Geolocation from '@react-native-community/geolocation';
import TMediums from '../../assets/data/TMediums';
navigator.geolocation = require('@react-native-community/geolocation');
const HomeMap = () => {
const {width, height} = useWindowDimensions()
const [myPosition, setMyPosition] = useState(null)
// useEffect Hook for Location
useEffect(() => {
const requestLocationPermission = async () => {
if (Platform.OS === 'android') {
const granted = await PermissionsAndroid.request(
PermissionsAndroid.PERMISSIONS.ACCESS_FINE_LOCATION,
{
title: 'Atua Location Request',
message: 'Atua needs access to your location.',
buttonNeutral: 'Ask Me Later',
buttonNegative: 'Cancel',
buttonPositive: 'OK',
},
);
if (granted === PermissionsAndroid.RESULTS.DENIED) {
console.log('Location permission denied');
return;
}
}
Geolocation.getCurrentPosition(
(position) => {
console.log(position)
setMyPosition({
latitude: position.coords.latitude,
longitude: position.coords.longitude,
});
},
(error) => {
console.log(error);
},
{ enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
);
};
requestLocationPermission();
}, []);
<MapView
style={{width, height:height - 150}}
provider={PROVIDER_GOOGLE}
showsUserLocation
followsUserLocation
initialRegion={{
latitude: myPosition.latitude,
longitude: myPosition.longitude,
latitudeDelta: 0.0222,
longitudeDelta: 0.0121,
}}
>`
The error message, I get is that it is unable to read myPosition.latitude.
Like I said, this line of code worked previously for my other project, but it's not working here. Hope to get a quick response from you. Thank you