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 e 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
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 e 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
Share Improve this question edited Mar 18, 2015 at 9:34 Sphvn 5,3638 gold badges41 silver badges58 bronze badges asked Mar 18, 2015 at 9:11 Pavan AlapatiPavan Alapati 2673 gold badges5 silver badges15 bronze badges 4- possible duplicate of Google Maps API [Directions API] Waypoints limitation? – A1rPun Commented Mar 18, 2015 at 9:17
- possible related question Google Maps API to get bus route – geocodezip Commented Mar 18, 2015 at 10:13
- possible related question JavaScript Google Maps polylines : issue in connecting all given GPS locations – geocodezip Commented Mar 18, 2015 at 10:14
- See my answer in similar question with full code example stackoverflow./questions/8779886/… – mikep Commented Apr 17, 2017 at 19:54
3 Answers
Reset to default 5Short 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.
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.
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.