Я пытаюсь отобразить маршрут с помощью серии путевых точек. Проблема возникает, когда количество путевых точек превышает предел, установленный API. Я прочитал из stackoverflow, что есть способ, которым ошибка предела не появляется, но я не могу понять, как это сделать. Кто-нибудь может мне помочь?
var addedWaypoints = [];
var map;
function initMapLogistic() {
map = new google.maps.Map(document.getElementById('map-logistic'), {
zoom: 4,
center: {lat: -32.949798, lng: -60.681911},
});
var directionsService = new google.maps.DirectionsService;
var directionsRenderer = new google.maps.DirectionsRenderer({
/* draggable: true, */
map: map,
panel: document.getElementById('right-panel')
});
directionsRenderer.addListener('directions_changed', function() {
computeTotalDistance(directionsRenderer.getDirections());
});
var start = new google.maps.LatLng(-32.949798, -60.681911);
var end = new google.maps.LatLng(-32.949798, -60.681911);
google.maps.event.addListener(map, 'click', function(event) {
// placeMarker(event.latLng);
addWaypoint(event.latLng);
displayRoute(start, end, directionsService, directionsRenderer);
});
displayRoute(start, end, directionsService, directionsRenderer);
}
function displayRoute(origin, destination, service, display) {
var waypoints = [{location: new google.maps.LatLng(-32.930572, -60.662137)},
{location: new google.maps.LatLng(-32.923554, -60.665930)},
{location: new google.maps.LatLng(-32.936270, -60.652841)}];
for (var i=0; i<addedWaypoints.length;i++) {
waypoints.push({location: addedWaypoints[i]});
}
service.route({
origin: origin,
destination: destination,
waypoints: waypoints,
optimizeWaypoints: true,
travelMode: 'DRIVING',
avoidTolls: true,
avoidHighways: true
}, function(response, status) {
if (status === 'OK') {
display.setDirections(response);
var leg = response.routes[0].legs[0];
createMarker(leg.start_location,"Inicio y fin");
} else {
alert('Could not display directions due to: ' + status);
}
});
}
function addWaypoint(latLng) {
addedWaypoints.push(latLng);
}
google.maps.event.addDomListener(window, "load", initMapLogistic);