Problem Recovering User Geolocation

Asked

Viewed 605 times

4

I’m having trouble retrieving user geolocation on the site. Localhost works, but not on the network. I’m guessing the problem is that it has no SSL certificate and the site is accessed by HTTP and not HTTPS.

See the error:

getCurrentPosition() and watchPosition() are deprecated on insecure Origins. To use this Feature, you should consider switching your application to a Secure origin, such as HTTPS. See https://goo.gl/rStTGz for more Details.

My code:

if(navigator.geolocation){
   navigator.geolocation.getCurrentPosition(function(pos){
       var lat = pos.coords.latitude;
       var lng = pos.coords.longitude;

       console.log(lat + ' - ' + lng);
   }
}

And in Safari it doesn’t work. You can’t recover geolocation from Localhost. In other browsers it works normally on Localhost.

  • 2

    About safari, I saw that to support geolocation, the version has to be 5.0 +.

  • 2

    Peter Park, remember that this type of interaction with the browser requires the permission of the end user of your application. Otherwise all your efforts will be in vain

  • Really, @Viniciusdutra ? Gee... Life...

  • you can always use https://letsencrypt.org/

1 answer

1

Browser Restriction

On this page of w3schools you can find more details about the use of geolocation functions with examples. Even has code for you to check if the browser supports this functionality.

Basically there are two alerts:

Since this can Compromise Privacy, the position is not available unless the user approves it.

Whenever you request in your code, the user will receive a message to approve the request.

As of Chrome 50, the Geolocation API will only work on Secure contexts such as HTTPS. If your site is Hosted on an non-secure origin (such as HTTP) the requests to get the users Location will no longer Function.

The function only works in a secure context, such as HTTPS. Even if it works on localhost for your tests, it won’t work when you publish the site.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.