Pavan Alapati
Pavan Alapati

Reputation: 267

Google Maps, draw lines between more than 8 waypoints

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

Upvotes: 1

Views: 4388

Answers (3)

Jacob Richman
Jacob Richman

Reputation: 471

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.

Upvotes: 0

Tim Cools
Tim Cools

Reputation: 1172

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.

Upvotes: 1

Sphvn
Sphvn

Reputation: 5343

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.

Upvotes: 5

Related Questions