0

这是我的地理编码代码的一部分。我希望它只显示所选地点的国家/地区,但它显示所有地址组件,我的问题是我无法指定地址组件对象。有一种方法写在文档上,但我不明白,你能为我做吗?

 if (geocoder) {
                geocoder.geocode({'latLng': latlng}, function(results, status) {
                    if (status == google.maps.GeocoderStatus.OK) {
                        var str = "";
                        $.each(results, function(){
                            str += "address components: <ul>"
                            $.each(this.address_components, function(){
                                str +="<li>"+this.types.join(", ")+": "+this.long_name+"</li>";
                            });
                            str +="</ul>";
                        });
                        $("#geocode_info").html(str);
4

2 回答 2

3

我想您只需要来自响应的国家/地区。
需要一些工作。需要解析国家名称的 json。尝试关注地理编码响应。

for(var addComponent in json.results[0].address_components){
    var component = json.results[0].address_components[addComponent];
    for(typeIndex in component.types ){
        if(component.types[typeIndex]=='country') {
            console.log(component.long_name);
        }
    }   
}
于 2010-12-25T03:43:40.293 回答
0

您可能需要仔细检查您的代码,现在某些地址组件上似乎缺少该类型。

http://maps.google.com/maps/api/geocode/xml?address=3000+Australia&sensor=false

于 2012-09-21T02:24:57.740 回答