从jquery获得一个xml的subchild值

问题描述:

我使用谷歌地图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。 谢谢!

对不起,我的英语...

你解决了你的问题吗?如果不尝试此功能:

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