admin管理员组文章数量:1417070
I'm building a website that uses geolocation in HTML5 to provide a service specific to the region in which the user is. Here is what I have been doing: I use the Javascript in this SO question to get country and administrative region names. Then I search the country and region names in a database and it returns location-specific data that I can display.
Here is the script:
<script src=""></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]) {
for (var i = 0; i < results[0].address_ponents.length; i++)
{
var longname = results[0].address_ponents[i].long_name;
var type = results[0].address_ponents[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 is working fine. I had no problem with it, until I used an OS and browser set to a different languages. The script then returned the country and region names in that language. Of course, I couldnt find any match in my database.
So, the question(s) is (are): is there a way to get language-independent country and region codes from google reverse geolocation? or is there a way to always get it in english? am I better off using IP geolocation then? or should I use a totally different method?
Thanks for the help.
I'm building a website that uses geolocation in HTML5 to provide a service specific to the region in which the user is. Here is what I have been doing: I use the Javascript in this SO question to get country and administrative region names. Then I search the country and region names in a database and it returns location-specific data that I can display.
Here is the script:
<script src="https://maps.googleapis./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]) {
for (var i = 0; i < results[0].address_ponents.length; i++)
{
var longname = results[0].address_ponents[i].long_name;
var type = results[0].address_ponents[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 is working fine. I had no problem with it, until I used an OS and browser set to a different languages. The script then returned the country and region names in that language. Of course, I couldnt find any match in my database.
So, the question(s) is (are): is there a way to get language-independent country and region codes from google reverse geolocation? or is there a way to always get it in english? am I better off using IP geolocation then? or should I use a totally different method?
Thanks for the help.
Share Improve this question edited May 23, 2017 at 12:32 CommunityBot 11 silver badge asked Jul 17, 2013 at 15:25 Julien SpronckJulien Spronck 15.5k5 gold badges49 silver badges57 bronze badges 02 Answers
Reset to default 1You should use the short_name
value for the country, which is the two-digit country code. These are ISO standard codes and should be the basis for your database lookup, not the localized country name.
Another options, which has ISO codes for administrative regions, is MaxMind's GeoIP2 JavaScript API:
http://dev.maxmind./geoip/geoip2/javascript/
It uses W3C geolocation when available and falls back on MaxMind's IP geolocation database otherwise. The free version requires attribution somewhere on your site.
本文标签:
版权声明:本文标题:javascript - How can I get the country and region code using geolocation instead of full country and region names - Stack Overfl 内容由网友自发贡献,该文观点仅代表作者本人, 转载请联系作者并注明出处:http://www.betaflare.com/web/1745257645a2650196.html, 本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌抄袭侵权/违法违规的内容,一经查实,本站将立刻删除。
发表评论