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

It takes forever for my location to update #338

Open
elkee2003 opened this issue Oct 18, 2024 · 2 comments
Open

It takes forever for my location to update #338

elkee2003 opened this issue Oct 18, 2024 · 2 comments
Labels
bug Something isn't working

Comments

@elkee2003
Copy link

Environment

Platforms

Android

Versions

  • Android:
  • iOS:
  • react-native-geolocation:
  • react-native:
  • react:

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 (

<MapView
style={{width, height: height - 120}}
provider={PROVIDER_GOOGLE}
initialRegion={{
latitude: location.latitude,
longitude: location.longitude,
latitudeDelta: 0.0922,
longitudeDelta: 0.0421,
}}
showsUserLocation
>
{TMediums.map((TMedium)=>{
return <Marker
key={TMedium.id}
coordinate={{ latitude : TMedium.latitude , longitude : TMedium.longitude }}>
<Image style={{width:50,
height:70,
resizeMode:'contain',
transform:[{
rotate:${TMedium.heading}.deg
}]
}}
source={getImage(TMedium.type)}/>

})}


)
}

export default HomeMap`

Reproducible Demo

@elkee2003 elkee2003 added the bug Something isn't working label Oct 18, 2024
@elkee2003 elkee2003 changed the title I takes forever for my location to update It takes forever for my location to update Oct 18, 2024
@Vipul3452
Copy link

Vipul3452 commented Oct 22, 2024

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

@VincentSastra
Copy link

I downgraded to 3.3.0 and fixed the issue

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

3 participants