Hello guys,
I am allowing our users to simply click on map and place two markers,
based on which we then calculate directions between these two markers.
The thing is that I am taking the position of the markers, and
sometimes people do not choose an option from the autocomplete menu,
so my algorithm doesn't work. What I would like to do, is to check
whether the markers have known position and if not, take the value of
the input boxes and do a geocoding query to get the position and then
calculate the route. I can't simply pass the value of the input boxes
to the origin/destination arguments, because I actually need to place
the markers first and thus I need the location of the given address.
Here is what I have now:
function calcRoute() {
if (!marker_from.getPosition()) {
var from = addressToLatLng($("#from").val());
marker_from.setPosition(from);
}
if (!marker_to.getPostion()) {
var to = addressToLatLng($("#to").val());
marker_to.setPosition(to);
}
if (!marker_from.getPostion() || !marker_to.getPostion())
return;
var request = {
origin:marker_from.getPostion(),
destination:marker_to.getPostion(),
travelMode:google.maps.TravelMode.DRIVING,
provideRouteAlternatives:true
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
directionsDisplay.setMap(map);
}
});
}
function addressToLatLng(address) {
geocoder.geocode({'address': address}, function(results,
status) {
if (status == google.maps.GeocoderStatus.OK) {
return results[0].geometry.location;
}
});
}
Although my geocode request is successful (I checked from within the
addressToLatLng function), it somehow never returns the actual value
to my variables from/to. Therefore I don't reach the route() request.
I guess it's because the geocoder request is async, but I do not know
how should I handle this issue - I want to place the markers first (if
they are not placed already) and then do the route() request?
Thank you in advance.
--
You received this message because you are subscribed to the Google Groups
"Google Maps JavaScript API v3" group.
To post to this group, send email to [email protected].
To unsubscribe from this group, send email to
[email protected].
For more options, visit this group at
http://groups.google.com/group/google-maps-js-api-v3?hl=en.