谷歌地图地理编码器返回状态 [英] google maps geocoder to return state

查看:106
本文介绍了谷歌地图地理编码器返回状态的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用Google地图地理编码器对邮编进行地理编码,并且希望它返回邮政编码所在的状态并将其存储在变量local中。我收到一个错误,指出本地是未定义的。为什么?

查看下面的代码:

  var address = document。 。的getElementById( 地址)值; 
var radius = document.getElementById(radius).value;
var latitude = 40;
var longitude = 0;
var local;
geocoder.geocode({'address':address},function(results,status){
if(status == google.maps.GeocoderStatus.OK){
latlng =(results [ 0] .geometry.location);
latitude = latlng.lat();
longitude = latlng.lng();
//将状态缩写存储在变量local中
local = results [0] .address_components.types.adminstrative_area_level_1;
}

else {
alert(由于以下原因,Geocode不成功:+ status);
}
});


解决方案

我认为问题其实就是 address_components 可能会有多个组件,并且所有邮递区号的顺序不一定相同。所以你必须遍历结果找到正确的结果。

 < html xmlns =http://www.w3.org/1999/xhtml> 
< script type =text / javascriptsrc =http://maps.google.com/maps/api/js?sensor=false>< / script>
< script type =text / javascript>
var geocoder = new google.maps.Geocoder();
function test()
{
var address = document.getElementById(address)。value;
var local = document.getElementById(local);
var latitude = 40;
var longitude = 0;
geocoder.geocode({'address':address},function(results,status)
{
if(status == google.maps.GeocoderStatus.OK)
{
latlng =(results [0] .geometry.location);
latitude = latlng.lat();
longitude = latlng.lng();
//存储状态缩写在变量局部
for(var ix = 0; ix< results [0] .address_components.length; ix ++)
{
if(results [0] .address_components [ix] .types [0] ==administrative_area_level_1)
{
local.value = results [0] .address_components [ix] .short_name;
}
}
}
else
{
alert(由于以下原因Geocode不成功:+ status;
}
});
}
< / script>
< / head>
< body>
< input type ='text'id ='address'value ='84102'/>
< input type ='text'id ='local'value =''/>
< a href ='#'onclick =test(); >尝试< / A>
< / body>
< / html>


I am using google maps geocoder to geocode a zipcode and I want it to return the state that the zipcode is located in and store it in the variable "local". I am getting an error indicating that local is undefined. Why?

see code below:

var address=document.getElementById("address").value;
var radius=document.getElementById("radius").value;
var latitude=40;
var longitude=0;
var local;
geocoder.geocode( { 'address': address}, function(results, status){
if (status==google.maps.GeocoderStatus.OK){
latlng=(results[0].geometry.location);
latitude=latlng.lat();
longitude=latlng.lng();
//store the state abbreviation in the variable local
local=results[0].address_components.types.adminstrative_area_level_1;
}   

else{
    alert("Geocode was not successful for the following reason: " + status);
}
});

解决方案

I think the issue is actually that address_components will likely have more than one component, and the order isn't necessarily the same for all zip codes. So you have to iterate through the results to find the correct one.

<html xmlns="http://www.w3.org/1999/xhtml">
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript">
var geocoder = new google.maps.Geocoder();
function test()
{
    var address=document.getElementById("address").value;
    var local = document.getElementById("local");
    var latitude=40;
    var longitude=0;
    geocoder.geocode( { 'address': address}, function(results, status)
    {
        if (status==google.maps.GeocoderStatus.OK)
        {
            latlng=(results[0].geometry.location);
            latitude=latlng.lat();
            longitude=latlng.lng();
            //store the state abbreviation in the variable local
            for(var ix=0; ix< results[0].address_components.length; ix++)
            {
                if (results[0].address_components[ix].types[0] == "administrative_area_level_1")
                {
                    local.value=results[0].address_components[ix].short_name;
                }
            }
        }
        else
        {
            alert("Geocode was not successful for the following reason: " + status);
        }
    });
}
</script>
</head>
<body>
    <input type='text' id='address' value='84102' />
    <input type='text' id='local' value='' />
    <a href='#' onclick="test();" >try</a>
</body>
</html>

这篇关于谷歌地图地理编码器返回状态的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆