简体   繁体   中英

GeoLocation - get ISO code of country

I have found references to address / country in the w3c spec ( http://dev.w3.org/geo/api/spec-source-v2 ), but no actual example or references to how to get the country's iso code of the current user?

Using something like: http://tawd.co.uk/test-geo3.html I'm able to get the lat and long of the site visitor, but I need a quick way of getting the country iso code too, how would I do this?

According the W3C specs it should be done with the requestAddress position options.

navigator.geolocation.getCurrentPosition(
   successFunction, 
   errorFunction, 
   {"requestAddress": true}
);

then this would try to figure out the address giving the latitud and longitud and return a position in the success callback with an address additional property which would contain the country and much more. Unfortunately this is just an w3c spec and seems that nowdays (at the moment to post this) seems that, even if many modern browsers have implement the PositionOptions almost any of them have implemented the requestAddress feature yet.

A possible way to temporary solve your problem could be to emulate what requestAddress should do by using the reverse geocoding google maps api to get the address from the coords: https://developers.google.com/maps/documentation/geocoding/#ReverseGeocoding . Then once you get the google maps address you can get the country from there: https://developers.google.com/maps/documentation/geocoding/#Types .... but .... i don't know if this way is not "quick way" enough for you.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM