Google Directions Service with Waypoints not working - google-maps

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
};

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]));

Add stopover to waypoints

I am having trouble adding stopover points to my google maps route. This is my calcRoute function below. Basically I have an array of latlng objects called waypts and all the points between waypts[0] and waypts[waypts.length-1] should be stopover points. I know I just need to loop through those points and make a stopover with something like this:
<script type="text/javascript">
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
var phpway = <?php echo json_encode($phpway); ?>;
var waypts = [];
for(var i = 0; i < phpway.length; i+=2){
waypts.push(new google.maps.LatLng(phpway[i], phpway[i+1]));
}
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var chicago = new google.maps.LatLng(41.850033, -87.6500523);
var mapOptions = {
zoom: 6,
center: chicago
}
map = new google.maps.Map(document.getElementById('map_canvas'), mapOptions);
directionsDisplay.setMap(map);
}
function calcRoute() {
var start = waypts[0];
var end = waypts[waypts.length-1];
var mywaypts = [];
for (var i = 1; i < waypts.length-1; i++) {
mywaypts.push({
location:waypts[i],
stopover:true});
}
var request = {
origin: start,
destination: end,
waypoints: mywaypts,
optimizeWaypoints: true,
travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
var route = response.routes[0];
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
</script>
You should not include your start/end points in your waypoints array, and just pass your waypts array in your request this way:
var request = {
origin : start,
destination : end,
waypoints: waypts,
travelMode : google.maps.TravelMode.DRIVING
};
See doc: https://developers.google.com/maps/documentation/javascript/directions?hl=FR#Waypoints

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

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)

Displaying multiple routes on a google map

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
});
}
}
});