6

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
Antony
  • 14,900
  • 10
  • 46
  • 74
  • Google shows me a city across the state from me, while asking for a user's location is more likely to be accurate. Why not just ask once and save it? – Waleed Khan Dec 24 '12 at 06:57
  • I am serving content that makes use of the current location on the homepage. It would be awkward to give a prompt for first time visitors. I will of course save this info when I am certain of his or her location. – Antony Dec 24 '12 at 07:03
  • @Antony For privacy concerns this data cannot be collected from the browser without prompting the user for the first time. Using the IP based services is your best bet but it has it's own best guesses. Also last I checked (2-3 years ago) it was being offered as paid service. I don't remember the services I checked though, sorry. – Tanzeel Kazi Dec 24 '12 at 07:08
  • 3
    I do notice something like the [Google Geolocation API](https://developers.google.com/maps/documentation/business/geolocation/). But I am afraid I may not have enough (financial) resources for that at the moment. – Antony Dec 24 '12 at 07:11
  • 1
    Possible duplicate of [js geolocation but without prompting - possible?](http://stackoverflow.com/questions/14538883/js-geolocation-but-without-prompting-possible) – leo Mar 18 '16 at 13:52
  • Why not save the lat and long response in a cookie? Then check for that cookie each time the user loads the page up? If the cookie doesn't exist or if it is null (declined), then they have either never allowed their browser provide the lat and long or intentionally not allowed it, Then the html5 geolocation method is not called. Voila. – Mike Kormendy Jun 05 '16 at 22:08
  • @MikeKormendy No, I need to know their current position the *first time* they enter the page. If the cookie does not exist and I don't call the geolocation method, voila, I have no idea where the user is. If I do call the method, I have just annoyed all the first time visitors. Cookie cannot solve the problem with first time visitors. – Antony Jun 06 '16 at 01:28
  • What I have done in this case is attempt to provide the closest approximate geolocation based on their IP address using a purchased database of IPs. Understanding that IP geolocation isn't as accurate, we then added the option to change their location by way of browser API and then stored that in their computer in a cookie for future visits. That's the best you can do without breaching privacy measures built into the browser. – Mike Kormendy Jun 22 '16 at 21:52

2 Answers2

2

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.

Andrey E
  • 605
  • 4
  • 8
0

Google's current location only works because you gave Google the right to your location at some point in time on that given browser. If you were to open up any new browser and do the same query, you would see that it can only yield a much more generic location.

I've been trying to find the answer to this myself, and you were on the right track with Google's loader. The problem is that it is more or less deprecated due to the reliability and accuracy of the HTML5 offering.

I'm very curious if you ever found a workaround. Some people might suggest maxmind, though I found it to be as accurate (moreso inaccurate) as google.loader.

It makes sense why a browser would prompt for such information, I'm just surprised there isn't some kind of workaround/hack other than using IPs.

Trevor
  • 1,284
  • 3
  • 15
  • 33