Reputation: 11746
I'm using google maps and the geocoding API to retrieve a users lat/lng after they enter a zip/postal code but I want to limit the zip/postal codes to that users country so I don't end up with multiple results.
Is there a way to reliably determine a visitors country of origin from the client maybe using jquery or possible decoding the user agent somehow?
My call would end of looking something like this:
var country = ???
var postcode = document.getElementById('postcode').value + ', ' + country;
var geocoder = new google.maps.Geocoder();
geocoder.geocode({ 'address': postcode }, function (results, status) {
if (status == google.maps.GeocoderStatus.OK) {
...
}
});
Any thoughts?
Upvotes: 0
Views: 1109
Reputation: 4995
For the benefit of not relying on a third-party, you can get the client's location by using Google's Loader. Here's a great example of this in use, loading the Google Maps API and ClientLocation.
Upvotes: 1
Reputation: 208
You could use the visitors IP address to look up his origin in a GeoIP Table. There are various tables around, e.g. from MaxMind.
Note that this might fail if the user is behind a proxy server or you are unable to obtain the IP. Apart from that it should be a quite reliable method, depending on your GeoIP table of course.
Upvotes: 1