Google路线指示服务返回错误的航点点订单

时间:2019-02-22 12:20:12

标签: google-maps google-maps-api-3 google-maps-api-2 google-direction

我有两个请求,我只更改了第二个请求中发送航点的顺序,我将航点一移到了第二个,其他三个航点相同,但是Google返回的“ waypoint_order”未按预期进行更改。 要求1: https://maps.googleapis.com/maps/api/directions/json?origin=44.7808248%2020.0845678&destination=44.7796273%2020.0838463&waypoints=optimize:true%7C44.7806935%2020.0844793%7C44.7803089%2020.0842888%7C44.7798215%2020.0839079%7C44.7800804%2020.0841333%7C44.7805088%2020.0843773&key=YOUR_API_KEY 回应1: “ waypoint_order”:[0,4,1,3,2]

请求2: https://maps.googleapis.com/maps/api/directions/json?origin=44.7808248%2020.0845678&destination=44.7796273%2020.0838463&waypoints=optimize:true%7C44.7803089%2020.0842888%7C44.7806935%2020.0844793%7C44.7798215%2020.0839079%7C44.7800804%2020.0841333%7C44.7805088%2020.0843773&key=YOUR_API_KEY 回应2: “ waypoint_order”:[1,4,0,3,2]

我希望仅在第二个响应中切换前两个航点。 我想念什么吗? 我以最简单的示例为例,这也发生在更多的航点上。预先感谢!

Red marker is Destination, Green origin, Orange ones are waypoints. As said I picked the simpliest example

1 个答案:

答案 0 :(得分:1)

嗯...我不理解您,或者您不了解我,或者您不了解文档。让我们在地图上绘制示例,然后您会告诉我哪里出了问题。

第一个请求

您输入的积分顺序:

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>

第二个请求

输入时的Waypoint顺序,并在方括号中显示它们在第一个请求中的顺序:

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>

据我所知,两个回复中的路标顺序都很好