Is there a way to determine a visitors country from the client?

我是研究僧i 提交于 2019-12-08 09:39:43

问题


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?


回答1:


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.




回答2:


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.



来源:https://stackoverflow.com/questions/13921563/is-there-a-way-to-determine-a-visitors-country-from-the-client

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!