开发者

Address component type in google maps geocoding

this is a part of my geocoding code. I want it to show only country of the selected place, but it shows all address components, My problem is I cant specify the address components object. There is a way of doing it that开发者_如何学Python is written on documentation but I didnt understand, can you do it for me.

 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);


I guess you need only country from the response that comes.
Need some work.Need to parse the json for country name. Try following for geocoding response.

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);
        }
    }   
}


You may want to double check your code, the type now appears to be missing on some address components.

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

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜