Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Geolocation without Prompt

I know about the HTML5 way to do geolocation, but the caveat is that it has to prompt the user for permission.

navigator.geolocation.getCurrentPosition

It gets particularly annoying when I would like to do geolocation right at the home page to serve customized content based on location: no one likes to get a prompt every time he or she visits your site.

The idea is to use external services to do geolocation based on IP address or something similar to get a less accurate estimate of the user's location and give the user a prompt only when that location is really off. Are there any good suggestions as to the services I can use for that purpose?

I know Google has done this already: current location. It shows your current location without giving you any prompt. Does anyone know how to do this?

The MaxMind database is good, but that would require the maintenance of a huge IP database just for that purpose. An API or a Google service is preferred.

I currently make use of the location object returned from the Google Loader to estimate the location, but it is less accurate than the Google search example above.

google.loader.ClientLocation.latitude
google.loader.ClientLocation.longitude
like image 666
Antony Avatar asked Dec 24 '12 06:12

Antony


1 Answers

If you don't mind using 3rd party API, you can use our service https://ip-api.io . It's working with client's IP address and doesn't require any permission from user.

Javascript example:

$.getJSON("http://ip-api.io/json/",
    function(result) {
        console.log(result);
    });

Then you can get location from result.latitude and result.longitude. https://ip-api.io provides much more data like country, city, zip code, timezone and whether this IP address is bot, spammer or TOR node.

like image 55
Andrey E Avatar answered Oct 18 '22 11:10

Andrey E