javascript - How can I get the country and region code using geolocation instead of full country and region names -
i'm building website uses geolocation in html5 provide service specific region in user is. here have been doing: use javascript in so question country , administrative region names. search country , region names in database , returns location-specific data can display.
here script:
<script src="https://maps.googleapis.com/maps/api/js?sensor=false"></script> <script> var region = ""; var country = ""; function getlocation() { if (navigator.geolocation) { navigator.geolocation.getcurrentposition(showposition,nogeolocation); } else { ... } } function showposition(position) { var geocoder = new google.maps.geocoder(); var latlong = new google.maps.latlng(position.coords.latitude,position.coords.longitude); geocoder.geocode({'latlng': latlong}, function(results, status) { if (status == google.maps.geocoderstatus.ok) { if (results[0]) { (var = 0; < results[0].address_components.length; i++) { var longname = results[0].address_components[i].long_name; var type = results[0].address_components[i].types; if (type.indexof("administrative_area_level_1") != -1) { region = longname; } if (type.indexof("country") != -1) { country = longname; } } } }); } function nogeolocation() { ... } getlocation(); </script>
this script working fine. had no problem it, until used os , browser set different languages. script returned country , region names in language. of course, couldnt find match in database.
so, question(s) (are): there way language-independent country , region codes google reverse geolocation? or there way in english? better off using ip geolocation then? or should use totally different method?
thanks help.
you should use short_name
value country, two-digit country code. these iso standard codes , should basis database lookup, not localized country name.
Comments
Post a Comment