开发者

Parse response returned by googlemaps

I am trying to access the start_address from the response object returned from GoogleMaps.

Tried:

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

Google Maps开发者_JS百科:

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...


Based on what you provided I can only come up with this structure of the response (not complete of course):

// 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}
            }]
    }]
};

And if you do response.routes[0].legs[0].start_address it returns 42 High St, Doncaster DN1 1, UK. The routes property is an array so there may be cases where you need to iterate to get different values from indexes, if that's the case just iterate over it and replace 0 with the iterator variable.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜