2011-08-30 6 views
0

GoogleMaps에서 반환 된 응답 개체에서 start_address에 액세스하려고합니다.구문 분석 응답이 googlemaps에서 반환했습니다.

는 시도 :

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

Google지도 :

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

무슨 프로를 바탕으로

// 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, UK를 반환 vided이 난 단지 응답이 구조 (물론 완전하지 않음)을 마련 할 수 있습니다. routes 속성은 배열이므로 인덱스에서 다른 값을 얻기 위해 반복해야하는 경우가있을 수 있습니다.이 경우 반복하고 0을 반복 변수로 바꿉니다.

+0

당신의 전설은 완벽하게 일했습니다. –