2010-06-05 40 views
3

iam嘗試使用gmaps V3中提供的地址組件類型獲取國家/地區名稱。gmaps地址組件類型獲取國家名稱

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

IAM試圖在這裏提醒國名鏈接纔可:

警報(結果[1] .address_component [國家] );

和here`s代碼.. 任何幫助真的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); 
     } 
     }); 
    } 
    } 

回答

4

您需要通過address_component迭代陣列找到一個類型的國家」的條目」。所以像這樣:

// 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); 
} 
相關問題