I have a problem with the Google DirectionsService. I know it's asynchronous and that is the cause of my troubles. I'd like to wait until the DirectionsService returns a result instead of executing code without an answer. Here is a sample:
function snap_to_road (lat) {
var position;
var request = {
origin: lat,
destination: lat,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
return response.routes[0].legs[0].start_location;
}
});
}
alert(snap_to_road(current.latLng));
The alert
always shows: "undefined". Is there any way to solve this?