Displaying multiple routes on a google map - google-maps

I am trying to display multiple routes on same map but am unable to do so.
No matter what I do I get only one route.
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
Any pointers will be helpful.

I had the same problem. This thread on a Google group for Google maps shows how to do it.
The author (a Google employee), wrote this:
You should be able to create two DirectionsRenderer objects, each that
use the same map and different DirectionsResults.
var map = new google.maps.Map(document.getElementById("map_canvas"));
function renderDirections(result) {
var directionsRenderer = new google.maps.DirectionsRenderer;
directionsRenderer.setMap(map);
directionsRenderer.setDirections(result);
}
var directionsService = new google.maps.DirectionsService;
function requestDirections(start, end) {
directionsService.route({
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}, function(result) {
renderDirections(result);
});
}
requestDirections('Huntsville, AL', 'Boston, MA');
requestDirections('Bakersfield, CA', 'Vancouver, BC');
I tried it and it does work.

Did you try as follows?
Here I have captured one path and displayed it. You can do the same by writing pointsArray = result.routes[1].overview_path; besides it and display it in a new loop.
directionsService.route (request, function (result, status)
{
if (status == google.maps.DirectionsStatus.OK)
{
directionsDisplay.setDirections (result);
pointsArray = result.routes[0].overview_path;
var i = 0;
var j = 0;
for (j = 0; j < pointsArray.length; j++)
{
var point1 = new google.maps.Marker ({
position:pointsArray [j],
draggable:false,
map:map,
flat:true
});
}
}
});

Related

routes are not defined when calling calcRoute with params (latLng) - Google Maps

I need to call pass params to calcRoute() on click, what I am doing is:
function calcRoute(source,destt) {
var start = new google.maps.LatLng(source);
var end = new google.maps.LatLng(destt);
var request = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(result, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(result);
}
});
}
Click Event:
$('#go').click(function(){
var from= "24.942834,67.121237";
var to = "24.944908,67.124491";
calcRoute(from,to);
});
It's returning the status with ZERO_RESULT
It was working fine when I hard coded lat n lng in calcRoute(), i.e
var start = new google.maps.LatLng(24.942834,67.121237);
var end = new google.maps.LatLng(24.944908,67.124491);
Thanks for any Help.
Direct string will not work as LatLng object you have to convert "24.942834,67.121237" to google.maps.LatLng to make it work.
$('#go').click(function(){
var from= new google.maps.LatLng(24.942834,67.121237);// convert it to latlng object
var to = new google.maps.LatLng(24.944908,67.124491);
calcRoute(from,to);
});
if you have "24.942834,67.121237" then you can use it like this:
var str="24.942834,67.121237";
var latlng=str.split(',')
var to = new google.maps.LatLng(parseFloat(latlng[0]), parseFloat(latlng[1]));

Google Directions Service with Waypoints not working

Here is my code for displaying waypoints with google directions:-
// Show Route (-)
if (e.keyCode == 109 && $("#booking-docket").dialog("isOpen")) {
var pickup = $('#txt-pickup-lat-long').val();
var pickupLat = pickup.split(",")[0];
var pickupLng = pickup.split(",")[1];
var destination = $('#txt-destination-lat-long').val();
var destinationLat = destination.split(",")[0];
var destinationLng = destination.split(",")[1];
var via = $('#txt-via-lat-long').val();
var viaLat = via.split(",")[0];
var viaLng = via.split(",")[1];
var start = new google.maps.LatLng(parseFloat(pickupLat), parseFloat(pickupLng));
var end = new google.maps.LatLng(parseFloat(destinationLat), parseFloat(destinationLng));
var waypts = new google.maps.LatLng(parseFloat(viaLat), parseFloat(viaLng));
var request = {
origin:start,
destination:end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
It worked perfectly fine until I added the waypoint in, not sure why but nothing happens now when I try to show the route, any ideas?
Your original example could be changed like:
//var waypts = new google.maps.LatLng(parseFloat(viaLat), parseFloat(viaLng));
var request = {
origin:start,
destination:end,
waypoints: [{
location: new google.maps.LatLng(parseFloat(viaLat), parseFloat(viaLng)),
stopover: false
}],
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};

How to show two routes on the same map?

How to show two routes on the same map?
Example: A->B y C->D.
Using Google Maps API.
function calcRoute1() {
var start = 'Huanchaco-Trujillo';
var end = 'Lince-Lima';
var request1 = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request1, function(response1, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response1);
}
});
}
function calcRoute2() {
var start = 'Chiclayo-Lambayeque';
var end = 'Tacna-Tacna';
...
}
This works for me:
var directionsService1 = new google.maps.DirectionsService();
var directionsDisplay1 = new google.maps.DirectionsRenderer();
function calcRoute1() {
var start = 'Huanchaco-Trujillo';
var end = 'Lince-Lima';
var request1 = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService1.route(request1, function(response1, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay1.setDirections(response1);
directionsDisplay1.setMap(map);
}
});
}
var directionsService2 = new google.maps.DirectionsService();
var directionsDisplay2 = new google.maps.DirectionsRenderer();
function calcRoute2() {
var start = 'Chiclayo-Lambayeque';
var end = 'Tacna-Tacna';
var request2 = {
origin:start,
destination:end,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService2.route(request2, function(response2, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay2.setDirections(response2);
directionsDisplay2.setMap(map);
}
});
}
google.maps.event.addDomListener(window, 'load', load);
working example

google maps change v2 into version v3 problems

I have some problems with upgrading google maps v2 in v3.
Follow code is v2:
function routing(target, width, lenght, name, zip, city, street) {
if (GBrowserIsCompatible()) {
if (counter > 0) {directions.clear()};
counter++;
var map = new GMap2(document.getElementById("map"));
map.addControl(new GSmallMapControl());
map.addControl(new GMapTypeControl());
directionsPanel = document.getElementById("route");
directions = new GDirections(map, directionsPanel);
var startpoint = name+', '+zip+' '+city+', '+street
if (target == 'messe') {
directions.load("from: "+startpoint+"#"+width+","+lenght+" to: Messe, 50679 #T_koeln#, Deutz-Mülheimer Straße 40#50.9473327,6.98312820000001");
};
if (target == 'airport') {
directions.load("from: "+startpoint+"#"+width+","+lenght+" to: Flughafen Koeln, 50679 #T_koeln#, Kennedystraße#50.8782914,7.122399800000039");
};
}
}
here is the v3 code:
var directionsService = new google.maps.DirectionsService();
var directionsDisplay = new google.maps.DirectionsRenderer();
function routing(target, width, lenght, name, zip, city, street) {
if (counter > 0) {directionsDisplay.setMap(null)};
counter++;
var mapOptions = {
zoom: #zoomfaktor_detail#,
center: new google.maps.LatLng(width, length),
overviewMapControl: true,
overviewMapControlOptions:{opened:true},
mapTypeId: google.maps.MapTypeId.ROADMAP
};
var map = new google.maps.Map(document.getElementById("map"),mapOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("route"));
var startpoint = name+', '+zip+' '+city+', '+street
if (target == 'messe') {
end = 'Messe, 50679 #T_koeln#, Deutz-Mülheimer Straße 40#50.9473327,6.98312820000001';
start = startpoint+"#"+width+","+lenght;
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
}
if (target == 'airport') {
end = 'Flughafen Koeln, 50679 #T_koeln#, Kennedystraße#50.8782914,7.122399800000039';
start = startpoint+"#"+width+","+lenght;
request = {
origin: start,
destination: end,
travelMode: google.maps.TravelMode.DRIVING
};
}
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
Why does it not function? Has someone an idea?
It isn't working because the route can't be found and the directions service is returning an error which is (or was) being silently ignored.
I suspect the problem is the "#" in your start and end addresses. Remove them (and the text after them), give the directions service valid addresses (or give it the coordinates as a google.maps.LatLng object).

How to add multiple (separated with a plus symbol) waypoints to a map

Thanks to geocodezip , I have managed to create a map with directions functionality and XML markers loaded. There is a problem left though:
How can I add waypoints to it? They should be separated by plus symbol or something else appropriate.
Here is the current code:
<script type="text/javascript">
//<![CDATA[
// global "map" variable
var map = null;
var rendererOptions = {
draggable: true
};
var directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);;
var directionsService = new google.maps.DirectionsService();
// A function to create the marker and set up the event window function
function createMarker(latlng, name, html) {
var marker = new google.maps.Marker({
position: latlng,
map: map,
zIndex: Math.round(latlng.lat()*-100000)<<5
});
var contentString = html;
google.maps.event.addListener(marker, 'click', function() {
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
}
// ===== request the directions =====
function getDirections() {
// ==== Set up the walk and avoid highways options ====
var request = {};
if (document.getElementById("walk").checked) {
request.travelMode = google.maps.DirectionsTravelMode.WALKING;
} else {
request.travelMode = google.maps.DirectionsTravelMode.DRIVING;
}
if (document.getElementById("highways").checked) {
request.avoidHighways = true;
}
if (document.getElementById("alternatives").checked) {
request.provideRouteAlternatives = true;
}
// ==== set the start and end locations ====
var saddr = document.getElementById("saddr").value
var daddr = document.getElementById("daddr").value
request.origin = saddr;
request.destination = daddr;
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
function initialize() {
// create the map
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(43.907787,-79.359741),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
// Read the data from example.xml
if (document.getElementById("showmarkers").checked) {
downloadUrl("example.xml", function(doc) {
var xmlDoc = xmlParse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var html = markers[i].getAttribute("html");
var label = markers[i].getAttribute("label");
// create the marker
var marker = createMarker(point,label,html);
}
});
}
// Read the data from example2.xml
if (document.getElementById("showmarkers2").checked) {
downloadUrl("example2.xml", function(doc) {
var xmlDoc = xmlParse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var html = markers[i].getAttribute("html");
var label = markers[i].getAttribute("label");
// create the marker
var marker = createMarker(point,label,html);
}
});
}
}
var infowindow = new google.maps.InfoWindow(
{
size: new google.maps.Size(150,50)
});
//]]>
</script>
This is the code I have used to generate waypoints, but it's not working now:
var via = document.getElementById("via").value;
if (via) {
so = via.split("+")
if (so.length > 1) {
var wpArray = [];
for (i=0; (i<so.length); i++) {
wpArray.push({location: so[i], stopover:true})
}
request = {
origin:saddr,
destination:daddr,
waypoints: wpArray,
provideRouteAlternatives: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}
} else {
request = {
origin:saddr,
destination:daddr,
waypoints:[{location:via, stopover:true}],
provideRouteAlternatives: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}
}
} else {
request = {
origin:saddr,
destination:daddr,
provideRouteAlternatives: true,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}
}
EDIT This is the working function:
function getDirections() {
// ==== Set up the walk and avoid highways options ====
var request = {};
if (document.getElementById("walk").checked) {
request.travelMode = google.maps.DirectionsTravelMode.WALKING;
} else {
request.travelMode = google.maps.DirectionsTravelMode.DRIVING;
}
if (document.getElementById("highways").checked) {
request.avoidHighways = true;
}
if (document.getElementById("alternatives").checked) {
request.provideRouteAlternatives = true;
}
// ==== set the start and end locations ====
var start = document.getElementById("start").value
var end = document.getElementById("end").value
request.origin = start;
request.destination = end;
request.waypoints = via;
var so;
var via = document.getElementById("via").value;
if (via) {
so = via.split("+")
if (so.length > 1) {
var wpArray = [];
for (i=0; (i<so.length); i++) {
wpArray.push({location: so[i], stopover:false})
}
request = {
origin:start,
destination:end,
waypoints: wpArray,
provideRouteAlternatives: true,
avoidHighways: document.getElementById("highways").checked,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}
} else {
request = {
origin:start,
destination:end,
waypoints:[{location:via, stopover:false}],
provideRouteAlternatives: true,
avoidHighways: document.getElementById("highways").checked,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}
}
} else {
request = {
origin:start,
destination:end,
provideRouteAlternatives: true,
avoidHighways: document.getElementById("highways").checked,
travelMode: google.maps.DirectionsTravelMode.DRIVING
}
}
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
Not sure why you have 2 xml files or what their format is. Here is an example which shows/hides markers based on their categories (again translated from Mike Williams' v2 tutorial):
http://www.geocodezip.com/v3_MW_example_categories.html
(also copied to How to show/hide markers from XML file without refreshing the page with the part of the question it applied to)