使用google api从位置提取省份

时间:2011-07-21 08:09:11

标签: javascript google-maps

我正在尝试使用google map api从某个位置提取一些额外信息。我看看它是可能的,但我不明白我在哪里放置bounds参数。这是我的要求:

var address = document.getElementById("address").value;
    geocoder.geocode( { 'address': address}, function(results, status) {
    if (status == google.maps.GeocoderStatus.OK) {
        alert(results[0].formatted_address);    
    } else {
        alert("Geocode was not successful for the following reason: " + status);
    }
});

我想在“地址”之后添加绑定:地址但不清楚如何。我试过了,但它不起作用:

geocoder.geocode( { 'address': address, "types":["sublocality","political"]}, function(results, status) {

我不知道是否熟悉意大利省,但我需要这样的东西:

要求:威尼斯
回复:VE或威尼斯

要求:Murano
回复:VE或威尼斯

----改进:

使用http://maps.googleapis.com/maps/api/geocode/json?address=murano&sensor=false我可以获得:

{
   "results" : [
      {
         "address_components" : [
            {
               "long_name" : "Murano",
               "short_name" : "Murano",
               "types" : [ "natural_feature" ]
            },
            {
               "long_name" : "Venezia",
               "short_name" : "Venezia",
               "types" : [ "locality", "political" ]
            },
            {
               "long_name" : "Venezia",
               "short_name" : "VE",
               "types" : [ "administrative_area_level_2", "political" ]
            },
            {
               "long_name" : "Veneto",
               "short_name" : "Veneto",
               "types" : [ "administrative_area_level_1", "political" ]
            },
            {
               "long_name" : "Italia",
               "short_name" : "IT",
               "types" : [ "country", "political" ]
            },
            {
               "long_name" : "30141",
               "short_name" : "30141",
               "types" : [ "postal_code" ]
            }
         ],

尝试使用alert打印(results [0] .address_components);它没有告诉我什么,我怎么能提取这个:

           "long_name" : "Venezia",
           "short_name" : "VE",
           "types" : [ "administrative_area_level_2", "political" ]

感谢, 安德烈

2 个答案:

答案 0 :(得分:3)

您想在types数组中查找“administrative_area_level_2”并返回该对象,因此您需要嵌套循环:

response= {
            "results" : [{
                "address_components" : [{
                    "long_name" : "Murano",
                    "short_name" : "Murano",
                    "types" : [ "natural_feature" ]
                },{
                    "long_name" : "Venezia",
                    "short_name" : "Venezia",
                    "types" : [ "locality", "political" ]
                },{
                    "long_name" : "Venezia",
                    "short_name" : "VE",
                    "types" : [ "administrative_area_level_2", "political" ]
                },{
                    "long_name" : "Veneto",
                    "short_name" : "Veneto",
                    "types" : [ "administrative_area_level_1", "political" ]
                },{
                    "long_name" : "Italia",
                    "short_name" : "IT",
                    "types" : [ "country", "political" ]
                },{
                    "long_name" : "30141",
                    "short_name" : "30141",
                    "types" : [ "postal_code" ]
                }
                ]
            }
            ]
        }

    for (var i=0; i<response.results[0].address_components.length; i++) {
            for (var b=0;b<response.results[0].address_components[i].types.length;b++) {
                if (response.results[0].address_components[i].types[b] == "administrative_area_level_2") {
                    //this is the object you are looking for
                    province = response.results[0].address_components[i];
                    break;
                }
            }
        }
        alert(province.short_name + " " + province.long_name)

答案 1 :(得分:2)

我没有看到任何其他解决方案,只能按address_components进行迭代,并将第一个数据与包含2个字母的short_name匹配。

var len = results[0].address_components.length;
var data;
for ( var i = 0; i < len; i++ ) {
    if ( results[0].address_components[i].short_name.length == 2 ) {
        data = results[0].address_components[i];
        break;
    }
}