gmaps地址组件类型获取国家/地区名称

gma*_*ser 3 google-maps

我试图使用gmaps V3提供的地址组件类型获取国家名称.

我不知道我怎么能以正确的方式得到它.. http://code.google.com/apis/maps/documentation/javascript/services.html#GeocodingAddressTypes

我想在这里提醒国家名字:

警报(结果[1] .address_component [国家]);

这里是代码..任何帮助都非常感谢..谢谢

  function codeLatLng() {
    var input = document.getElementById("latlng").value;
    var latlngStr = input.split(",",2);
    var lat = parseFloat(latlngStr[0]);
    var lng = parseFloat(latlngStr[1]);
    var latlng = new google.maps.LatLng(lat, lng);
    if (geocoder) {
      geocoder.geocode({'latLng': latlng}, function(results, status) {
        if (status == google.maps.GeocoderStatus.OK) {
          if (results[1]) {
          alert(results[1].address_component[country]);
          } else {
            alert("No results found");
          }
        } else {
          alert("Geocoder failed due to: " + status);
        }
      });
    }
  }
Run Code Online (Sandbox Code Playgroud)

Red*_*ing 5

您需要遍历address_component数组以查找类型为"country"的条目.所以类似于:

// this is just looking at the first result - > results[0]
for (var i = 0; i < results[0].address_components.length; i++)
{
    var addr = results[0].address_components[i];
    // check if this entry in address_components has a type of country
    if (addr.types[0] == "country") 
        alert (addr.long_name);
}
Run Code Online (Sandbox Code Playgroud)