Ну ... либо я вас не понимаю, либо вы меня не понимаете, либо вы не понимаете документацию.Давайте нанесем ваши примеры на карту, и вы скажете мне, что не так.
Первый запрос
Порядок точек при их вводе:
0. 44.7806935, 20.0844793
1. 44.7803089, 20.0842888
2. 44.7798215, 20.0839079
3. 44.7800804, 20.0841333
4. 44.7805088, 20.0843773
var directionDisplay;
var directionsService;
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: true
});
directionsService = new google.maps.DirectionsService();
var myOptions = {
zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP,
}
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
directionsDisplay.setMap(map);
calcRoute();
}
function calcRoute() {
var waypts = [];
stop = new google.maps.LatLng(44.7806935, 20.0844793)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '0');
stop = new google.maps.LatLng(44.7803089, 20.0842888)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '1');
stop = new google.maps.LatLng(44.7798215, 20.0839079)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '2');
stop = new google.maps.LatLng(44.7800804, 20.0841333)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '3');
stop = new google.maps.LatLng(44.7805088, 20.0843773)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '4');
start = new google.maps.LatLng(44.7808248, 20.0845678);
end = new google.maps.LatLng(44.7796273, 20.0838463);
createMarker(start, 'A');
createMarker(end, 'B');
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
console.log(response);
directionsDisplay.setDirections(response);
var route = response.routes[0];
}
});
}
function createMarker(latlng, label = null) {
var marker = new google.maps.Marker({
position: latlng,
map: map,
label: label
});
}
#map-canvas {
height: 200px;
}
<div id="map-canvas"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize">
</script>
Второй запрос
Порядок точек при их вводе и в скобках порядок, в котором они былив первом запросе:
0. 44.7803089, 20.0842888 [1]
1. 44.7806935, 20.0844793 [0]
2. 44.7798215, 20.0839079 [2]
3. 44.7800804, 20.0841333 [3]
4. 44.7805088, 20.0843773 [4]
var directionDisplay;
var directionsService;
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer({
suppressMarkers: true
});
directionsService = new google.maps.DirectionsService();
var myOptions = {
zoom: 3,
mapTypeId: google.maps.MapTypeId.ROADMAP,
}
map = new google.maps.Map(document.getElementById("map-canvas"), myOptions);
directionsDisplay.setMap(map);
calcRoute();
}
function calcRoute() {
var waypts = [];
stop = new google.maps.LatLng(44.7803089, 20.0842888)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '0');
stop = new google.maps.LatLng(44.7806935, 20.0844793)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '1');
stop = new google.maps.LatLng(44.7798215, 20.0839079)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '2');
stop = new google.maps.LatLng(44.7800804, 20.0841333)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '3');
stop = new google.maps.LatLng(44.7805088, 20.0843773)
waypts.push({
location: stop,
stopover: true
});
createMarker(stop, '4');
start = new google.maps.LatLng(44.7808248, 20.0845678);
end = new google.maps.LatLng(44.7796273, 20.0838463);
createMarker(start, 'A');
createMarker(end, 'B');
var request = {
origin: start,
destination: end,
waypoints: waypts,
optimizeWaypoints: true,
travelMode: google.maps.DirectionsTravelMode.WALKING
};
directionsService.route(request, function(response, status) {
if (status == google.maps.DirectionsStatus.OK) {
console.log(response);
directionsDisplay.setDirections(response);
var route = response.routes[0];
}
});
}
function createMarker(latlng, label = null) {
var marker = new google.maps.Marker({
position: latlng,
map: map,
label: label
});
}
#map-canvas {
height: 200px;
}
<div id="map-canvas"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script async defer src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&callback=initialize">
</script>
Насколько я могу судить, порядок точек маршрута идеально подходит в обоих ответах.