Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Geolocation is unable to show current location #278

Open
elkee2003 opened this issue Oct 25, 2023 · 1 comment
Open

Geolocation is unable to show current location #278

elkee2003 opened this issue Oct 25, 2023 · 1 comment
Labels
bug Something isn't working

Comments

@elkee2003
Copy link

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

@elkee2003 elkee2003 added the bug Something isn't working label Oct 25, 2023
@ankurrup83
Copy link

for me it is different i am getting the location but it is the default like it is the location of the google headquarters of usa

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
bug Something isn't working
Projects
None yet
Development

No branches or pull requests

2 participants