Gal Gal - 11 days ago 7
Javascript Question

How to get visitor's location (i.e. country) using javascript geolocation

I'm looking for a javascript library to extend the native geolocation function

if(navigator.geolocation) {
navigator.geolocation.getCurrentPosition(function(position) {
var latitude = position.coords.latitude;
var longitude = position.coords.longitude;
});
}


so that I could use the visitor's country name (perhaps return an informative array).

So far all I've been able to find are functions that display a google maps interface but none actually gave what I want, except for this library which worked well in this example but for some reason didn't work on my computer. I'm not sure why that went wrong there.

Anyways, do you know a javascript library that can simply return an array containing information like country, city etc from latitude and longitude values?

Thanks.

Answer

You don't need to locate the user if you only need their country. You can look their ip up in any ip to location service (hostip.info, maxmind). This will be accurate most of the time.

If you really need to get their location you can get their lat/lng with that method then query google's or yahoo's reverse geocoding service

Comments