2010-10-08 42 views
1

我使用谷歌地圖v3,並且我想要獲得標記地理編碼國家的簡稱。但我無法從google maps'xml中獲取short_name子圖。 返回的xml是這樣的:http://code.google.com/intl/hu/apis/maps/documentation/geocoding/#XML從jquery獲得一個xml的subchild值

我需要這個例子中的「US」。這是我的函數:

function codeLatLng(latlng) { 
    if (geocoder) { 
    geocoder.geocode({'latLng': latlng}, function(results, status) { 
    if (status == google.maps.GeocoderStatus.OK) { 
     var str = ''; 
     jq.each(results, function(){ 
     str += this.formatted_address+'|'; 
     }); 
     str = str.split('|'); 
     jq('#address').text(str[0]); 
    } 
    }); 
    } 
} 

但我需要在第七屆address_component的「short_name」 subchild。 謝謝!

對不起,我的英語...

回答

0

你解決了你的問題嗎?如果不嘗試此功能:

function codeLatLng(latlng){ 
    if (geocoder) { 
     geocoder.geocode({ 
      'location': latlng 
     }, function(results, status){ 
      if (status == google.maps.GeocoderStatus.OK) { 
       var str = ''; 
       var i = 0, len = results.length, loop = true; 
       while (i < len && loop) { 
        for (var j = 0, len2 = results[i].address_components.length; j < len2; j++) { 
         if (results[i].address_components[j].types[0] == 'country') { 
          str = results[i].address_components[j].short_name; 
          loop = false; 
          break; 
         } 
        } 
        i++; 
       } 
       jq('#address').text(str); 
      } 
     }); 
    } 
} 
+0

這工作得很好,謝謝! – kree 2010-11-02 11:13:49