michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.32k stars 232 forks source link

It takes forever for my location to update #338

Open elkee2003 opened 5 days ago

elkee2003 commented 5 days ago

Environment

Platforms

Android

Versions

Description

it takes forever for my location to load, and in turn it delays my map. When I was using expo location, I didn't have this problem, but since I switched to geolocation I delays a lot this is my code: `import { View, useWindowDimensions, ActivityIndicator, Image, PermissionsAndroid, Platform, } from 'react-native' import React, {useState, useEffect} from 'react' import MapView, {Marker, PROVIDER_GOOGLE} from 'react-native-maps'; import Geolocation from '@react-native-community/geolocation'; import * as Location from 'expo-location'; import styles from './styles' import TMediums from '../../../assets/data/TMediums'

const HomeMap = () => { const {width, height} = useWindowDimensions() const [location, setLocation] = useState(null); const [errorMsg, setErrorMsg] = useState(null);

const getImage=(type)=>{
  if (type === 'Micro X'){
    return require('../../../assets/atuaImages/Bicycle.png')
  }
  if (type === 'Moto X'){
      return require('../../../assets/atuaImages/Bike.jpg')
  }
  if (type === 'Maxi Batch'){
      return require('../../../assets/atuaImages/top-UberXL.png')
  }
  if (type === 'Maxi'){
      return require('../../../assets/atuaImages/Deliverybicycle.png')
  }
  return require('../../../assets/atuaImages/Walk.png')
}

useEffect(() => {
  let watchId;

  const requestLocationPermission = async () => {
      try {
          // For both iOS and Android
          if (Platform.OS === 'ios' || Platform.OS === 'android') {
          Geolocation.requestAuthorization(); // Request permission on iOS and Android
          }

          // Watch the user's location and update it continuously
          watchId = Geolocation.watchPosition(
          (position) => {
              const { latitude, longitude } = position.coords;
              setLocation({ latitude, longitude });
              console.log('Updated Location:', position);
          },
          (error) => {
              console.error('Geolocation error:', error);
              setErrorMsg('Error fetching location');
          },
          {
              enableHighAccuracy: true,
              timeout: 20000,
              maximumAge: 1000,
              distanceFilter: 500, // Update based on distance (e.g., every 500 meters)
          }
          );
      } catch (error) {
          console.error('Location permission error:', error);
          setErrorMsg('Failed to request location permission');
      }
  };

  requestLocationPermission();

  // Cleanup subscription when the component unmounts
  return () => {
    if (watchId !== null) {
      Geolocation.clearWatch(watchId);
    }
  };
}, []);

if (!location || !location.latitude || !location.longitude) {
  return <ActivityIndicator style={{ marginTop: 30 }} size="large" />;
}

return (

{TMediums.map((TMedium)=>{ return })}

) }

export default HomeMap`

Reproducible Demo

Vipul3452 commented 2 days ago

Same here! In some devices it take so much time, and when I turn off the location and turn it on, the geolocation api takes forever to return anythng