I have created the following code using Google Maps API that should make a direction line on Google Maps between two given addresses. My Code is:
When you call the function calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB);
the positions pointA
and pointB
are undefined because they are the result of the asyncronous call of geocoder.geocode
.
Move the calculateAndDisplayRoute
function call after you've obtained a result from geocoder.geocode
geocoder.geocode({
'address': document.getElementById('addressTo').value
}, function(results, status) {
var location = results[0].geometry.location;
poi
if (status == google.maps.GeocoderStatus.OK) {
calculateAndDisplayRoute(directionsService, directionsDisplay, pointA, pointB);
}
Since you're sending two geocoding requests you will need to wait for the geocoder status for each request.