开发者

Locating a user using geolocation?

I would like to know what the best way is to get the latitude and longitude of the user visiting my page. And i want it to work in IE as well, so html5 is out of t开发者_JS百科he question :)

Thanks


You can use JavaScript to make a call out to this service - which will return all the major details you are looking for:

http://j.maxmind.com/app/geoip.js

It will make JavaScript functions available to you with the following info:

function geoip_country_code() { return 'CA'; }
function geoip_country_name() { return 'Canada'; }
function geoip_city()         { return 'Toronto'; }
function geoip_region()       { return 'ON'; }
function geoip_region_name()  { return 'Ontario'; }
function geoip_latitude()     { return '44.7150'; }
function geoip_longitude()    { return '-76.3470'; }
function geoip_postal_code()  { return ''; }
function geoip_area_code()    { return ''; }
function geoip_metro_code()   { return ''; }

Note the data above is just a faked example...


If a relatively coarse location is sufficient (usually at the city level), then a Geo IP database might be appropriate (there are several providers for those, just google it).

Note also, that those can be fooled relatively easy by proxy servers. But in most cases it should be fine.


Google has a great geocoding API ... http://code.google.com/apis/maps/documentation/geocoding/ ... which unlike most Geo IP based lookups, uses a number of different methods, including sensors (i.e. iPhone GPS) if available, or gears, down to simple Geo IP lookups.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜