9

Migrating code from Javascript API 2 to 3. I have a list of locations which i need to plot in the form of a driving directions. This was done in v2 using the following code

directions = new GDirections(map);
directions.loadFromWaypoints(waypoints, {preserveViewport: true});  

Here is my attempt at converting this to V3

var request = {
    origin: startLoc,
    destination: endLoc,
    waypoints: waypoints,
    optimizeWaypoints: true,
    travelMode: google.maps.TravelMode.DRIVING
};          

directionsService.route(request, function(response, status) {
    if (status == google.maps.DirectionsStatus.OK) {
        directionsDisplay.setDirections(response);
    } 
});

Not the whole code, but the general idea. Seems to work fine, with one little issue. When there are more than 8 waypoints, the call fails. This is expected since Google Maps API v3 Docs states

The maximum allowed waypoints is 8, plus the origin, and destination. Maps API for Business customers are allowed 23 waypoints, plus the origin, and destination. Waypoints are not supported for transit directions.

Since i didn't run in to this issue in v2, is this a new restriction with v3? I wonder if i am using something that was not designed for what i need. This is a very lightly used applicaton with 2 users, so i am not sure if an expensive business license is worth the return. Emails to Google maps team have not yet been returned. Any workarounds/pointers will be of great help. Thanks.

BBS
  • 175
  • 1
  • 4
  • 11
  • v2 had a limit of 25 waypoints. In v3, the limit was reduced to 8 – Andrew - OpenGeoCode Nov 07 '13 at 23:41
  • This link is to a similar question and answer - http://stackoverflow.com/questions/13083639/google-maps-v3-waypoints-infowindow-with-random-text – Andrew - OpenGeoCode Nov 07 '13 at 23:43
  • I read the Maps API for business costs $10K/yr though i don't see this on any Google site. Would you have an idea if that is about right? Just looked in to Yahoo, they seem have closed down the Maps API service. – BBS Nov 08 '13 at 00:14

3 Answers3

12

One possible work around (particularly for a lightly used site) is to use multiple DirectionsService requests.

geocodezip
  • 158,664
  • 13
  • 220
  • 245
  • Looks promising, though i dont quite get how the multi-batch results are sorted back together. Will try coding and let you know. Thanks. – BBS Nov 08 '13 at 00:11
  • Sorry, didn't notice the optimizeWaypoints. That might be an issue. – geocodezip Nov 08 '13 at 00:34
  • V. useful, I didn't realise you could make multiple DirectionsService requests, for some reason I thought you could only do one per page – duncan Nov 08 '13 at 09:21
  • @duncan this only works if the waypoints are already sorted. I needed Google to optimize the waypoints for me. Still a cool idea to overcome the limit. – BBS Nov 08 '13 at 16:54
  • 2
    If you need more than 8 waypoints, and you need them optimized, you need to either 1. buy a "maps for business" license (which allows up to 23 waypoints) or 2. optimize the waypoints yourself before using this method of drawing directions. You might be able to use the [DirectionsMatrix Service](https://developers.google.com/maps/documentation/javascript/distancematrix) to optimize the waypoints. – geocodezip Nov 08 '13 at 17:39
  • 2
    I find it strange Google lowered the number of waypoints they let you use going from V2 to V3. Business license costs upwards of $10K/year. I just removed that functionality from the application. It wasn't worth the extra cost. – BBS Nov 13 '13 at 18:17
  • @geocodezip could you paste above example how did you implemented examples? – Paweł Madej Mar 25 '14 at 14:10
  • View source in your browser. – geocodezip Mar 25 '14 at 14:27
  • I Tried and using this i am able to plot 92 waypoints but it's not allowing me more then that and i have more then 900 way points in my route so can any one suggest me for that? – cracker May 21 '14 at 11:19
  • Not surprising. This is still subject to the quota and rate limit. For lots of points it will be prohibitively slow. – geocodezip May 21 '14 at 12:42
2

Use Should need to Use array concept like these...

directionsService[i].route({                                    
                    'origin': start,
                    'destination': end
                    'waypoints': waypts,
                    'travelMode': 'DRIVING'
                       },
                    function (directions, status){              
                                if (status == google.maps.DirectionsStatus.OK) {
                                    directionsDisplay[j].setDirections(directions);
                                }
                            });

in these directionservice and directiondisplay are should be in array logic and using looping concepts use should need to assign start,end and waypoints dynamically and
try sending multiple request means u ll get the route for n number of latlon's ...but the markers ll be repeat with same name after 8 waypoints for that we remove the default markers by using supressmarkers false property...

PeeHaa
  • 71,436
  • 58
  • 190
  • 262
Nandha
  • 21
  • 1
-1

As others said, it's imposable to do it using Google's JavaScript API. However, Google does allow up to 23 waypoints with a server-side request. So using PHP and JavaScript, I was able to make a workaround.

What you have to do is get the "waypoint order" from the server side request and then have JavaScript give you directions between each location.

<?php
  $startLocation = "40.713,-74.0135";
  $endLocation = "40.75773,-73.985708";
  $waypoints = array("40.748433,-73.985656|", "40.689167,-74.044444|");
  $apiKey = "";
  $routeData = json_decode(file_get_contents("https://maps.googleapis.com/maps/api/directions/json?origin=".$startLoc."&destination=".$endLoc."&waypoints=optimize:true|".$waypoints."&key=".$apiKey));
  echo "var waypointsOrder = ". json_encode($routeData->routes[0]->waypoint_order) . ";\n";
?>

var startLocation = {lat: 40.713, lng: -74.0135};
var endLocation = {lat: 40.75773, lng: -73.985708};

//get directions from the origin to the first waypoint
var request = {
    origin: startLocation,
    destination: JSON.parse(places[waypointsOrder[0]][1]),
    travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
          renderDirections(result, map);
    }
});

//get directions from the last waypoint to the destination
var request = {
    origin: JSON.parse(places[waypointsOrder[waypointsOrder.length-1]][1]),
    destination: endLocation,
    travelMode: google.maps.TravelMode.DRIVING
};
directionsService.route(request, function (result, status) {
    if (status == google.maps.DirectionsStatus.OK) {
          renderDirections(result, map);
    }
});

//get directions between each waypoint
for (i = 1; i < waypointsOrder.length; i++) {
  var request = {
      origin: JSON.parse(places[waypointsOrder[i-1]][1]),
      destination: JSON.parse(places[waypointsOrder[i]][1]),
      travelMode: google.maps.TravelMode.DRIVING
  };
  directionsService.route(request, function (result, status) {
      if (status == google.maps.DirectionsStatus.OK) {
          renderDirections(result, map);
      }
  });
}

function renderDirections(result, map) {
  var directionsDisplay = new google.maps.DirectionsRenderer ({
    map: map
  });
  directionsDisplay.setMap(map); 
  directionsDisplay.setDirections(result); 
}
Jacob Richman
  • 471
  • 1
  • 5
  • 8