Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/cocoa/3.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 谷歌地图API有时不返回AddressDetails中的PostalCode_Javascript_Jquery_Json_Google Maps - Fatal编程技术网

Javascript 谷歌地图API有时不返回AddressDetails中的PostalCode

Javascript 谷歌地图API有时不返回AddressDetails中的PostalCode,javascript,jquery,json,google-maps,Javascript,Jquery,Json,Google Maps,我创建了一个函数来获取位置输入,将其传递给Google Maps Javascript API V2,并返回一个纬度/经度/标准化的地址,我必须将该地址拆分为数据库中的不同字段(即城市、州、邮政)。我需要至少一个输入,以便从谷歌返回的数据将始终具有 国家 城市(地点) 州(行政区) 邮政的 横向/纵向 下面是Google返回的JSON对象结构的一些信息。 问题:在某些情况下,Placemark.AddressDetails将不包含邮政编码,而摘要Placemark.address将包含邮政

我创建了一个函数来获取位置输入,将其传递给Google Maps Javascript API V2,并返回一个纬度/经度/标准化的地址,我必须将该地址拆分为数据库中的不同字段(即城市、州、邮政)。我需要至少一个输入,以便从谷歌返回的数据将始终具有

  • 国家
  • 城市(地点)
  • 州(行政区)
  • 邮政的
  • 横向/纵向
下面是Google返回的JSON对象结构的一些信息。

问题:在某些情况下,Placemark.AddressDetails将不包含邮政编码,而摘要Placemark.address将包含邮政编码

例如,以下输入示例将在Placemark.address内返回邮政编码,但不返回Placemark.AddressDetails

  • 威斯康星州密尔沃基创新大道10437号,邮编:53226
  • 妈妈
  • 帝国大厦-->这实际上返回了次行政区域下某个奇怪的“从属位置”中的邮政编码
以下是我的故障排除代码:

<script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<!-- You must insert your maps API script here. -->
<script language="javascript" type="text/javascript" src="http://www.json.org/json2.js"></script>

<script type="text/javascript"> 
    $(document).ready(function()
    {
        var geocoder = new GClientGeocoder();

        $.geolocateAddress = function(address)
        {  
            geocoder.getLocations(address, function(response)
            {
                if (response != null && response.Placemark != undefined)
                {

                    var placemark = response.Placemark[0];
                    // Placemark has Post code or higher accuracy -> http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GGeoAddressAccuracy
                    if (placemark.AddressDetails.Accuracy >= 5)
                    {
                        alert(placemark.address);
                        try {
                            alert(placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
                        } catch (err) {
                            alert('What the deuce?  Why is there no postal code?... I bet you the placemark.address has the postal code.');
                            alert(JSON.stringify(placemark));
                        }
                    } else {
                        alert('Sorry, this requires at least a postal code input for accuracy')
                    }
                } else {
                    alert('Failed to geo locate address');
                }
            });
        };

        $("#location").blur(function(event)
        {
            var address = $("#location").val();
            $.geolocateAddress(address);
        });

    });
</script>

<form action="" method="get"> 
    <input title="Location" type="text" value="" id="location" /> 
</form>

$(文档).ready(函数()
{
var geocoder=new GClientGeocoder();
$.geolocateAddress=函数(地址)
{  
geocoder.getLocations(地址、函数(响应)
{
if(response!=null&&response.Placemark!=未定义)
{
var placemark=response.placemark[0];
//Placemark具有邮政编码或更高的精确度->http://code.google.com/apis/maps/documentation/javascript/v2/reference.html#GGeoAddressAccuracy
如果(placemark.AddressDetails.Accurance>=5)
{
警报(placemark.address);
试一试{
警报(placemark.AddressDetails.Country.AdministrativeArea.SubAdministrativeArea.Locality.PostalCode.PostalCodeNumber);
}捕捉(错误){
警报(‘见鬼!为什么没有邮政编码?……我打赌placemark.address有邮政编码’);
警报(JSON.stringify(placemark));
}
}否则{
警报('抱歉,这至少需要输入邮政编码才能确保准确性')
}
}否则{
警报(“未能地理定位地址”);
}
});
};
$(“#位置”).blur(函数(事件)
{
变量地址=$(“#位置”).val();
$.geologateAddress(地址);
});
});
呸。。。关于谷歌地图的位置标记模式,有什么我不理解的吗?通过升级到Google Maps Javascript API V3,我是否能够解决这个问题?(V2 JSON对象在我看来更直观)


我马上就要认输了。希望有人能帮忙

自从地理编码服务存在以来,这一直是一个问题

有关一些背景信息,请参阅我在2008年发布到问题追踪器的“bug报告/功能请求”:

在这篇文章中,帕梅拉·福克斯和托尔·米切尔是谷歌的员工,他们当时是地图团队的一员

一年半后,谷歌做出了一些改进:

事情有了很大的改善,但地理编码并不是一门精确的科学

在任何情况下,您可能正在使用旧版本的地理编码器,因为我通过此请求获得邮政编码:


这是一个直接的http请求,但是API V3应该返回相同的数据。

。我将尝试使用V3API,看看这是否解决了我的问题,并让您知道结果。谢谢刚刚实施。V3比V2更健壮。。。如果我需要隔离一个邮政编码(例如),那么必须遍历address_组件才能找到我想要的“type”,这感觉有点奇怪。但似乎效果很好。谢谢我认为API的版本在这里并不重要。如果有门牌号/街道号,则返回完整地址。我还没有广泛地测试过这个,但它似乎与我的随机测试一起工作。