iam trying to get the country name using the Address Component Types available from gmaps V3.
i dont know how i can get it the right way.. http://code.google.com/apis/maps/documentation/javascript/services.html#GeocodingAddressTypes
iam开发者_Go百科 trying to alert the country name liks here :
alert(results[1].address_component[country]);
and here`s the code.. any help is really appreciated..thanks
  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);
        }
      });
    }
  }
You need to iterate through the address_component array to find the entry with a type of "country". So something like:
// 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);
}
 
         
                                         
                                         
                                         
                                        ![Interactive visualization of a graph in python [closed]](https://www.devze.com/res/2023/04-10/09/92d32fe8c0d22fb96bd6f6e8b7d1f457.gif) 
                                         
                                         
                                         
                                         加载中,请稍侯......
 加载中,请稍侯......
      
精彩评论