javascript - 使用google api从位置中提取省份

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

我想在 'address':address 之后添加边界,但不清楚如何添加。我试过了,但它不起作用:

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

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

请求:威尼斯
回应:VE或Venezia

请求:穆拉诺
回应:VE或Venezia

----改进:

使用 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" ]

谢谢, 安德里亚

最佳答案

您想在类型数组中查找“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)

关于javascript - 使用google api从位置中提取省份,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/6773036/

相关文章:

javascript - 是否可以在 Javascript 中监听原始类型的值变化?

javascript - 谷歌地图中的 route 点

javascript - 幻灯片放映到下拉列表

java - 使用多个标记创建折线

c# - 如何使用新的 WCF REST HttpClient API 设置 GoogleLogin 请求的授权 header

javascript - 使 Google map 在显示指定位置时自动缩小更多

javascript - 如何重定向到 crossrider 中的书签 URL?

javascript - 覆盖 qx.core.Object#bind

javascript - 如何在 Iframe 中显示谷歌地图

android - 点击标记时如何显示气泡