react-native-geolocation icon indicating copy to clipboard operation
react-native-geolocation copied to clipboard

Error while trying to get user current location.

Open elkee2003 opened this issue 2 years ago • 0 comments

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

  • react-native info
  •   + CategoryInfo          : ObjectNotFound: (react-nati  
     ve:String) [], CommandNotFoundException
      + FullyQualifiedErrorId : CommandNotFoundException 
    
    
    

Platforms

Android

Versions

  • Android:
  • iOS:
  • react-native-geolocation: 3.1.0
  • react-native: 0.72.7
  • react: 18.2.0 Screenshot (76)

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 ( <View> <MapView style={{width, height:height-150}} provider={PROVIDER_GOOGLE} showsUserLocation initialRegion={{ latitude: myPosition.latitude, longitude: myPosition.longitude , latitudeDelta: 0.0922, longitudeDelta: 0.0421, }} > <Marker title={'hi'} description={'Good'} coordinate={{ latitude:37.43199, longitude:-122.099 }}/> </MapView> </View> ) }

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

elkee2003 avatar Nov 17 '23 19:11 elkee2003