how can i parse the response from google direction services ? I am calling javascript code from my iOS WebView, i want to return some value from java script to iOS. I can see the response when i use alert function to display, but it not returning value. I think this is may be the function is callback function. So what should i do ? please tell me the solution
This is my code:
var value = function() {
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
// var response=new google.maps.DirectionsResult;
var map = new google.maps.Map(document.getElementById('map'), {
zoom:7,
mapTypeId: google.maps.MapTypeId.ROADMAP
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('panel'));
var request = {
origin: 'Chicago',
destination: 'New York',
travelMode: google.maps.TravelMode.TRANSIT
};
var temp;
directionsService.route(request,
function(response, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
t=response.routes[0].copyrights;
temp=t;
alert(t);
directionsDisplay.setDirections(response);
return t;
}
} );
return temp;
};
Instead of using the returnValue of value()
as a variable inside another function you must call the other function inside the callback of directionsService.route()
and pass t
as argument to the other function.
Requesting the directionsService is an asynchronous process, it isn't finished yet when value()
returns temp
, that's why temp
still will be undefined
.