1

We have json. In json have 10 latitude and 10 longitude.We tried draw line between those points.But We got Status code google.maps.DirectionsResult.MAX_WAYPOINTS_EXCEEDED.So after deleted two points.Now We tried to draw the lines between 8 points it's working nice. But in json some time come 50 to 100 latitude and longitude.We tried like this

 var aa=waypts[0].location.k;
  var bb=waypts[0].location.D;

  var cc=waypts[waypts.length-1].location.k
  var dd=waypts[waypts.length-1].location.D;

var start1= new google.maps.LatLng(aa,bb);
var end1=new google.maps.LatLng(cc, dd);

  var request = {
      origin: start1,
      destination: end1,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: google.maps.TravelMode.DRIVING
  };
  debugger;

  directionsService.route(request, function(response, status) {
  debugger;
    if (status == google.maps.DirectionsStatus.OK) {
      directionsDisplay.setDirections(response);
      var route = response.routes[0];
    }
  });

first tell me how to draw line between more then 8 points.Please guide me

Sphvn
  • 5,247
  • 8
  • 39
  • 57
Pavan Alapati
  • 267
  • 3
  • 5
  • 15
  • possible duplicate of [Google Maps API \[Directions API\] Waypoints limitation?](http://stackoverflow.com/questions/4418875/google-maps-api-directions-api-waypoints-limitation) – A1rPun Mar 18 '15 at 09:17
  • possible related question [Google Maps API to get bus route](http://stackoverflow.com/questions/15315347/google-maps-api-to-get-bus-route) – geocodezip Mar 18 '15 at 10:13
  • possible related question [JavaScript Google Maps polylines : issue in connecting all given GPS locations](http://stackoverflow.com/questions/27609872/javascript-google-maps-polylines-issue-in-connecting-all-given-gps-locations) – geocodezip Mar 18 '15 at 10:14
  • See my answer in similar question with full code example http://stackoverflow.com/questions/8779886/exceed-23-waypoint-per-request-limit-on-google-directions-api-business-work-lev/43458012#43458012 – mikep Apr 17 '17 at 19:54

3 Answers3

5

Short answer not in a single request.

The documentation for Google Maps Directions states that

MAX_WAYPOINTS_EXCEEDED indicates that too many waypoints were provided in the request The maximum allowed waypoints is 8, plus the origin, and destination. ( Google Maps API for Work customers may contain requests with up to 23 waypoints.)

Under the header of Usage Limits

Users of the free API get Up to 8 waypoints (plus origin and destination) allowed in each request and a total of 2,500 directions requests per 24 hour period.

So if you run multiple requests, fetching 8 waypoints (plus origin and destination) at a time you can achieve this. Just note the maximum of 2,500 directions requests per 24 hours.

Sphvn
  • 5,247
  • 8
  • 39
  • 57
1

You should use multiple requests as the other answer suggests. Here is an example:

var service = new maps.DirectionsService();
function calcRoute(points, startIndex) {

    var start = points[startIndex];
    var destinationIndex = startIndex + 8 > points.length - 1 ? points.length - 1 : startIndex + 8;
    var destination = points[destinationIndex];

    function handlerRouteResult(result, status) {

        var path = [];
        if (status === maps.DirectionsStatus.OVER_QUERY_LIMIT) {
            setTimeout(function() {
                calcRoute(points, startIndex);
            }, 100);
        }
        else if (status === maps.DirectionsStatus.OK) {
            var overviewPath = result.routes[0].overview_path;
            for (var routePointIndex = 0; routePointIndex < overviewPath.length; routePointIndex++) {
                path.push(overviewPath[routePointIndex]);
            }
        } else {
            for (var pointIndex = startIndex; pointIndex <= destinationIndex; pointIndex++) {
                path.push(points[pointIndex]);
            }
        }

        var line = new maps.Polyline({
            map: map,
            strokeColor: '#235c23',
            path: path
        });
    }

    var waypoints = [];
    for (var waypointIndex = startIndex; waypointIndex < destinationIndex - 1; waypointIndex++) {
        waypoints.push({ location: points[waypointIndex] });
    }

    service.route({
        origin: start,
        destination: destination,
        travelMode: maps.DirectionsTravelMode.DRIVING,
        waypoints: waypoints
    }, handlerRouteResult);
}

for (var i = 0; i < pathPoints.length - 1; i += 8) {
    calcRoute(pathPoints, i);
}

It also take the OVER_QUERY_LIMIT response in account that you receive you make to many API calls. In this case it will retry the route query.

Tim Cools
  • 1,162
  • 10
  • 19
0

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.

I explained it here along with the code.

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.

Community
  • 1
  • 1
Jacob Richman
  • 471
  • 1
  • 5
  • 8