2011-08-30 81 views
0

我想從GoogleMaps返回的響應對象中訪問start_address。googlemaps返回的解析響應

嘗試:

response['start_address'] 
response['routes']['start_address'] 
response['routes']['legs']['start_address'] 

谷歌地圖:

directionsDisplay = new google.maps.DirectionsRenderer(); 
directionsDisplay.setMap($gmap); 
directionsDisplay.setPanel(document.getElementById(directionHtml)); 
directionsService.route(request, function(response, status){ 
    if(status == google.maps.DirectionsStatus.OK){ 
      directionsDisplay.setDirections(response); 
      alert(response['routes']['legs']['start_address']); 
    } 
}); 

response.toSource()根據你親什麼

({routes:[ 
    {bounds:{ba:{b:53.51456, d:53.529900000000005}, V:{d:-1.1919300000000002, b:-1.1283400000000001}}, 
    copyrights:"Map data \xA92011 Tele Atlas", 
    legs:[ 
     {distance:{text:"5.9 km", value:5910}, 
     duration:{text:"11 mins", value:688}, 
     end_address:"18 Spring Lane, Sprotbrough, Doncaster DN5 7, UK", 
     end_location:{Pa:53.51555, Qa:-1.1919299999999566}, 
     start_address:"42 High St, Doncaster DN1 1, UK", start_location:{Pa:53.52307, Qa:-1.1337300000000141}, steps:[ 
ETC... 

回答

1

vided我只能拿出響應的這種結構(不完全當然):

// for testing purposes I assume to have the actual response 
// assigned to a response var 
var response = { 
routes:[{ 
     bounds:{ba:{b:53.51456, d:53.529900000000005}, V:{d:-1.1919300000000002, b:-1.1283400000000001}}, 
     copyrights:"Map data \xA92011 Tele Atlas", 
     legs:[{ 
       distance:{text:"5.9 km", value:5910}, 
       duration:{text:"11 mins", value:688}, 
       end_address:"18 Spring Lane, Sprotbrough, Doncaster DN5 7, UK", 
       end_location:{Pa:53.51555, Qa:-1.1919299999999566}, 
       start_address:"42 High St, Doncaster DN1 1, UK", start_location:{Pa:53.52307, Qa:-1.1337300000000141} 
      }] 
    }] 
}; 

如果你這樣做response.routes[0].legs[0].start_address返回42 High St, Doncaster DN1 1, UKroutes屬性是一個數組,因此可能會出現需要迭代才能從索引獲取不同值的情況,如果這種情況只是迭代它並用迭代器變量替換0

+0

您的傳奇,非常感謝您的工作。 –