Problem Retrieving User's GeoLocalization

4

I'm having trouble retrieving the user's geolocation on the site. Localhost works, but no network. I'm finding that the problem is that it does not have 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 secure origin, such as HTTPS . See link 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 Safari does not work. Can not retrieve geolocation from Localhost. In other browsers it works normally in Localhost.

    
asked by anonymous 09.05.2016 / 14:45

1 answer

1

Browser Restriction

On this w3schools page you will find more details on using geolocation functions with examples. It 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 unsecured 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 though it works at localhost for your tests, it will not work when you publish the site.

    
20.04.2017 / 13:25