I'm developing a JS webapp with Google Maps API.
I have to calculate the distance from A/D to B, B to C and C to A/D (please check the next image).
https://i.stack.imgur.com/FkaK9.jpg
But, on the map, I have to render ONLY the second leg (legs[1], B to C). (check the image for more info)
https://i.stack.imgur.com/8j6WF.jpg
What can I do to hide the first and last leg of my route? Of course, I could do another request, one for the map, one for the calculations, but I'm trying to be as efficient as possible.
My JS:
var mpos = "via Melzi deril 38 20154 Milano";
var madd = $("#i_madd_v").val() + ', ' + $("#i_madd_n").val() + ', ' + $("#i_madd_c").val();
var dadd = $("#i_dadd_v").val() + ', ' + $("#i_dadd_n").val() + ', ' + $("#i_dadd_c").val();
console.log(madd);
console.log(dadd);
function initMap() {
const directionsService = new google.maps.DirectionsService();
const directionsRenderer = new google.maps.DirectionsRenderer();
var centro = new google.maps.LatLng(45.462861, 9.186453);
var mapOptions = {
zoom:11,
center: centro,
disableDefaultUI: true
}
map = new google.maps.Map(document.getElementById('mappa'), mapOptions);
directionsRenderer.setMap(map);
calcRoute(directionsService, directionsRenderer);
}
function calcRoute(directionsService, directionsRenderer) {
var request = {
origin:mpos,
destination:mpos,
waypoints: [
{
location: madd,
stopover: true
},
{
location: dadd,
stopover: true
},
],
travelMode: 'DRIVING'
};
directionsService.route(request, function(response, status) {
if (status == 'OK') {
console.log(response);
console.log(status);
directionsRenderer.setDirections(response);
}
});
}
initMap();
});