1

I'm trying to use navigator.geolocation.getCurrentPosition to find lat long using android emulator. It is not picking any location. I thought that problem is with my emulator. I tried using Telnet, and also altered my code based on:

{ enableHighAccuracy: true, timeout: 1000, maximumAge: 1000 } 

I also tried false option. Nothing worked. Please note that in expo snack i can get location using "Web" option, but not as android and IOS. so the problem is not browser based

I just found the code on expo snack, it is not showing the lat long here as well. Please see if you can help me find what is the problem. I prefer not to use Geolocation of expo, and would like to stick to the navigator.geolocation option.

Please see snack link below: https://snack.expo.io/@coder79/geolocation-example

enter image description here

sal abb
  • 111
  • 2
  • 12

3 Answers3

2

You should set the geolocation permissions for iOS and Android (like this guide shows under the section Setting Permissions for iOS and Android)

Even if you use the navigation api, your app still need to work with these permissions.

But remember, navigator.geolocation is deprecated in React-Native. You should not rely on this api, even while using the expo-cli.

I would recommend the Location api if you are using the expo-cli.

For someone using a bare RN project, you could use @react-native-community/geolocation instead.

Kevin Métivier
  • 68
  • 2
  • 10
  • Thanks Kevin. I am following what help seys, but i still got these two errors, when i run this :Invariant Violation: "main" has not been registered. This can happen if: * Metro (the local dev server) is run from the wrong folder. Check if Metro is running, stop it and restart it in the current project. * A module failed to load due to an error and `AppRegistry.registerComponent` wasn't called. 2. @ react-native-community/geologation: NativeMOdile.RNCGeolocation is null. To fix this issue try: Run react-native link @react-native-community/geolocation – sal abb Dec 10 '20 at 18:20
  • I am new and i got it wrong, I am using expo-cli, i think i can use navigator.geolocation – sal abb Dec 10 '20 at 18:28
  • [navigator.geolocation](https://archive.reactnative.dev/docs/0.58/geolocation#docsNav) is deprecated since RN 0.58. This would be a bad practice to use this api since it's no longer maintained. Using the expo-cli, i would recommend you to use the expo [Location](https://docs.expo.io/versions/latest/sdk/location/) api. – Kevin Métivier Dec 11 '20 at 13:30
2

Make sure you set up the geolocation in the Android Emulator. You can find a very good guide on how to set it in this SO answer: https://stackoverflow.com/a/45098850/8798164

p-syche
  • 575
  • 1
  • 5
  • 17
1

It works for me, so I think the problem is that if you request for geolocation and then get denied, you can never request for geolocation again and nothing will happen. Maybe you would have to go to site settings for your own page to allow geolocation.

Timothy Chen
  • 431
  • 3
  • 8
  • thanks Timothy, or is it my desktop? becuae my code also has same issue. But what can be done? – sal abb Dec 10 '20 at 15:37
  • What type of computer are you using? I know that some browsers don't support it. You can find a complete list here: https://developer.mozilla.org/en-US/docs/Web/API/Navigator/geolocation#Browser_compatibility – Timothy Chen Dec 10 '20 at 15:44
  • It is Dell. The location option is ON. Also please see snapshot of chrome. Please note that in expo snack i can get location using "Web" option, but not as android and IOS. so the problem is not browser based – sal abb Dec 10 '20 at 15:50