如何使用HTML5地理位置查找用户所在的国家/地区? [英] How can I find a user’s country using HTML5 geolocation?
本文介绍了如何使用HTML5地理位置查找用户所在的国家/地区?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我熟悉用于返回用户位置的粗略坐标的HTML5地理定位。
然而,我怎样才能返回其坐标所在国家/地区的名称?解决方案
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng':< YOURLATLNGRESPONSEFROMGEO>},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
if(结果[0]){
var loc = getCountry(results);
}
}
});
函数getCountry(results)
{
for(var i = 0; i< results [0] .address_components.length; i ++)
{
var shortname = results [0] .address_components [i] .short_name;
var longname = results [0] .address_components [i] .long_name;
var type = results [0] .address_components [i] .types;
如果(type.indexOf(country)!= -1)
{
if(!isNullOrWhitespace(shortname))
{
return shortname;
}
else
{
return longname;
函数isNullOrWhitespace(text){
if(text == null){
返回true;
}
return text.replace(/ \s / gi,'').length< 1;
}
这是我使用的:)
I'm familiar with HTML5 geolocation for returning rough coordinates of the user’s location.
However, how can I return the name of the country that their coordinates are in?
解决方案
var geocoder = new google.maps.Geocoder();
geocoder.geocode({'latLng': <YOURLATLNGRESPONSEFROMGEO>}, function(results, status) {
if (status == google.maps.GeocoderStatus.OK) {
if (results[0]) {
var loc = getCountry(results);
}
}
});
function getCountry(results)
{
for (var i = 0; i < results[0].address_components.length; i++)
{
var shortname = results[0].address_components[i].short_name;
var longname = results[0].address_components[i].long_name;
var type = results[0].address_components[i].types;
if (type.indexOf("country") != -1)
{
if (!isNullOrWhitespace(shortname))
{
return shortname;
}
else
{
return longname;
}
}
}
}
function isNullOrWhitespace(text) {
if (text == null) {
return true;
}
return text.replace(/\s/gi, '').length < 1;
}
This is what I use :)
这篇关于如何使用HTML5地理位置查找用户所在的国家/地区?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文