0

I'm trying to use the function:

 navigator.geolocation.getCurrentPosition(success, error, options)

and it's working fine on Firefox, but on Chrome this returns error.

I saw a lot of related questions with the same issue. My question is, there's another function/ solution that works both in Firefox and Chrome ?

  • "Simply doesn't work" here means... neither the `success` nor `error` callback is ever called? Or only the `error` callback is called? Or the `getCurrentPosition` function itself isn't defined? What options are you supplying? Can you supply a complete simple example? What related questions did you see? – apsillers Jan 10 '17 at 19:30
  • `getCurrentPosition()` and `watchPosition()` no longer work on insecure origins. To use this feature, you should consider switching your application to a secure origin, such as HTTPS. Maybe this is your reason? – ishaan Jan 10 '17 at 19:38
  • Possible duplicate of [getCurrentPosition() and watchPosition() are deprecated on insecure origins](http://stackoverflow.com/q/32106849/710446) – apsillers Jan 10 '17 at 19:40
  • It is calling an error, probably ughitsaaron and ishaan are right, i'm trying to call from an insecure origin (codepen.io). What options do I have if I could not change to HTTPS ? – Rafael Herculano Jan 10 '17 at 21:30
  • @RafaelHerculano The duplicate I linked to above has a fallback solution that uses the Google Maps geoloation API (in the top-voted, non-accepted answer). (You will need to get your own Google Maps API key for your domain and use your own key instead of the one used in the example there, of course.) – apsillers Jan 11 '17 at 16:30

1 Answers1

0

It's unclear what you mean by "simply doesn't work". However, it may be useful to clarify that success and error in the example you provided are callbacks that pass in either a position or an error object as an argument. So, for instance, the following should work in Chrome:

navigator.geolocation.getCurrentPosition(
  position => console.log(position),
  err => console.log(err)
);

Note that Chrome will throw an error if you call getCurrentPosition() from an insecure (e.g., non-HTTPS) origin.

ughitsaaron
  • 517
  • 4
  • 10