michalchudziak / react-native-geolocation

Geolocation APIs for React Native
MIT License
1.27k stars 219 forks source link

[iOS]Only able to obtain the location immediately when the app starts #300

Open YaoHuiJi opened 4 months ago

YaoHuiJi commented 4 months ago

I encountered a strange problem that has left me very confused. My app is only able to get coordinates once through "watchPosition" when it starts up, and the retrieval speed is very fast (the small arrow icon at the status bar indicating location retrieval briefly turns blue and solid ). However, afterwards, it fails to retrieve the address again, whether through watchPosition or getCurrentPosition, and I no longer see the location icon in the status bar turning solid, it remains a hollow/outline icon

However, in the following scenarios, it is able to retrieve coordinates once again, but only once:

  1. Killing the app and then restarting it.
  2. Switching the app to the background, disabling any location-related settings such as GPS, Wi-Fi, cellular data, in the system settings, and then re-enabling them before returning to the app.

I suspect it may be related to the cellular or GPS signals on my phone. However, after testing various settings, such as enabling/disabling high accuracy or adjusting timeout settings, the problem persists. Additionally, other geo apps, such as Apple's Map or Reminder apps, or other map apps are able to obtain the correct location.

It looks like watchPosition and getCurrentPosition only get the address once when the application starts, and then never again, and each failed attempt results in the following error:

{"PERMISSION_DENIED": 1, "POSITION_UNAVAILABLE": 2, "TIMEOUT": 3, "code": 3, "message": "Unable to fetch location within 120.0s."}

The info.plist:

截屏2024-02-26 08 32 57

I'm out of my mind😫. I have no idea what's going on. Can someone give me some ideas? Any thought possible.

YaoHuiJi commented 4 months ago

I did some more testing and found that it seemed to be watchPosition, and once the app ran watchPosition listener, getCurrentPosition no longer return the coords, but always return an error 'Unable to fetch location within xxx s'. I've removed the use of watchPosition from the code, and now getCurrentPosition immediately gets the coordinates every time.

This also explains why my App was still getting coordinates 2 days ago, because I just added the watchPosition code yesterday, trying to make the update more timely, but I couldn't get coordinates anymore... It took me more than a day to find the cause, but I should have thought it was the watchPosition problem earlier.

I don't know why, but it seems like the problem is solved, and I'll do more testing, Don't use watchPosition and getCurrentPosition together, hopefully to help someone else with the same problem.

aacamhi commented 4 months ago

@YaoHuiJi were you using getCurrentPosition and WatchPosition in the same component? Or are you saying you cant use them together in the same app or in the same stack.. im facing the same issue and my current implementation consists of a custom hook that executes getCurrentLocation or watchPosition depending of the needs of the screen, but im never using both methods on the same instance of the hook or the component that calls it.

YaoHuiJi commented 4 months ago

@YaoHuiJi were you using getCurrentPosition and WatchPosition in the same component? Or are you saying you cant use them together in the same app or in the same stack.. im facing the same issue and my current implementation consists of a custom hook that executes getCurrentLocation or watchPosition depending of the needs of the screen, but im never using both methods on the same instance of the hook or the component that calls it.

In my case, getCurrentPosition can't return coords if watchPosition is listening. So I remove the watchPosition and use setInterval+getCurrentPosition to update coords regularly. Everything works fine now.

aacamhi commented 4 months ago

@YaoHuiJi were you using getCurrentPosition and WatchPosition in the same component? Or are you saying you cant use them together in the same app or in the same stack.. im facing the same issue and my current implementation consists of a custom hook that executes getCurrentLocation or watchPosition depending of the needs of the screen, but im never using both methods on the same instance of the hook or the component that calls it.

In my case, getCurrentPosition can't return coords if watchPosition is listening. So I remove the watchPosition and use setInterval+getCurrentPosition to update coords regularly. Everything works fine now.

Ok, so im guessing my cleanup methods which clear the watch are probably not working as expected.. thanks!