How to create multiple routes and display on the map?

93 views
Skip to first unread message

Fabiano Santos

unread,
Mar 7, 2017, 2:28:17 AM3/7/17
to Google Maps JavaScript API v3
Friends

Is it possible to create multiple routes and display on the map? For example, I would like to display the different routes
[1] from point A to point B;
[2] from point X to point Y and
[3] from point F to point J.

Anyone have any idea how to do this?

example:
Https://static05.cminds.com/wp-content/uploads/prettyroutes.png
Message has been deleted

Fabiano Santos

unread,
Mar 9, 2017, 11:43:11 AM3/9/17
to Google Maps JavaScript API v3

I found this code and it solves my problem.

var location1 = new google.maps.LatLng(34.04429454929703, -118.22793351693724);
var location2 = new google.maps.LatLng(33.688522885631706, -116.15151750131224);

var location3 = new google.maps.LatLng(32.69561555501597, -117.06338273568724);
var location4 = new google.maps.LatLng(34.525395303965354, -117.27212297006224);

var map;
var mapOptions = { center: new google.maps.LatLng(42.5584308, -70.8597732), zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP };

function initialize() {
map = new google.maps.Map(document.getElementById("map_canvas"), mapOptions);
directionsService = new google.maps.DirectionsService();
directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: false,
suppressInfoWindows: true
});
directionsDisplay.setMap(map);

var request = {
origin: location1,
destination: location2,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});


//directionsService = new google.maps.DirectionsService();
directionsDisplay2 = new google.maps.DirectionsRenderer({
suppressMarkers: false,
suppressInfoWindows: true
});
directionsDisplay2.setMap(map);

var request = {
origin: location3,
destination: location4,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay2.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);






Auto Generated Inline Image 1

AlMarwan Group

unread,
Mar 14, 2017, 6:46:06 PM3/14/17
to Google Maps JavaScript API v3
Thanks great share
Reply all
Reply to author
Forward
0 new messages