michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.31k stars 228 forks source link

Error while trying to get user current location. #283

Open elkee2003 opened 10 months ago

elkee2003 commented 10 months ago

Environment

Screenshot (75)

react-native : The term 'react-native' is not recognized as the name of a cmdlet, function, script file, or operable program. Check the spelling of the name, or if a path was included, verify that the path is correct and try again. At line:1 char:1

Platforms

Android

Versions

Description

`import { View, useWindowDimensions, PermissionsAndroid, Platform, Pressable, ActivityIndicator } from 'react-native' import React, {useState, useEffect} from 'react' import Geolocation from '@react-native-community/geolocation'; import MapView, {PROVIDER_GOOGLE, Marker} from 'react-native-maps';

const GOOGLE_MAPS_APIKEY = '*';

const HomeScreen = () => { 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)
      const {latitude, longitude}=position.coords;
      setMyPosition({latitude,longitude})
    },
    (error) => {
      console.log(error);
    },
    { enableHighAccuracy: true, timeout: 20000, maximumAge: 1000 },
  );
};

requestLocationPermission();

}, []);

return (

) }

export default HomeScreen` Screenshot (77) Screenshot (79)

Screenshot (78)

This code in one of my applications works smoothly. On rendering the screen it shows my currentpositon in the intial region. However this doesn't work for my other applications, I wonder why? This is the error it shows Screenshot (72) . Please I would need a code that would allow me show my current location, immediately the my screen renders. Note: I am developping for Android.

Reproducible Demo