With my code I create an array of Google Maps Markers, where I need pointA and pointB to paint a route on the map and calculate the kilometers.
function addMarker(location, addrs, counter) {
if(counter <= 2){
marker = new google.maps.Marker({
position: location,
map: exports.map,
animation: google.maps.Animation.DROP,
title: "Point: "+ counter + "Address:" + addrs
});
exports.markers.push(marker);
}else{
calculateRoute();
alert("Ya terminaste de seleccionar la ruta");
}
} // Sets the map on all markers in the array.
Once I capture my 2 Markes, I would like to display and paint the path between pointA and pointB.
So, I made a function called calculateRute() where I read my array of Markers and get the position (which are my POINTO and OBPOINT coordinates), I assign it to another array where I just want to put the coordinates and show them in an ALERT (), so far so good.
function calculateRoute(){
for (var i = 0; i < exports.markers.length; i++) {
maping[i] = exports.markers[i].position;
//show my coordenates
alert(maping[i]);
}
var request = {
origin: maping[0],
destination: maping[1],
travelMode: 'DRIVING'
}
directionsService.route(request, function(result, status){
if (status=="OK") {
directionsDisplay.setDirections(result);
}
})
}
When we go to request and pass the attributes of the ARRAY of COORDINATES (pointA and pointB) by their position to ORIGIN and DESTINATION, IT DOES NOT SHOW ME ANYTHING.
I can't get the coordinates as such and assign them, I don't understand why it doesn't work the way I'm handling it.
Logical, in the end it only paints my two Markers and doesn't paint the route because I don't get the coordinates.
I managed to get the longitude and latitude by doing 2 arrays, I don't think it's the best option, but it has worked for me so far: