如何使用谷歌方向API优化路线

时间:2016-08-26 17:59:45

标签: google-maps

我正在使用google方向api来解决旅行商问题。

显然,api采用一个名为optimize:true的参数,然后返回一个"waypoint_order": [ 1, 0, 2, 3 ],告诉您优化路线的最佳航点顺序。

事情是,当你尝试优化路线时,API就不起作用了,你得到NO_RESULTS。

例如,使用此url查看添加optimize:true|位时API如何失败。

不工作(NO_RESULTS错误):

https://maps.googleapis.com/maps/api/directions/json?origin=place_id:ChIJdd4hrwug2EcRmSrV3Vo6llI&destination=place_id:ChIJh1a5WhEMa0gRY1JU4PEam8Q&waypoints=optimize:true|place_id:ChIJPeqVDlONbEgRk4X1zrUsKDs|place_id:ChIJ_WegsaCYc0gRlCypaxXgLjs|&key=YOUR_KEY

工作(但不是优化):

不优化网址:https://maps.googleapis.com/maps/api/directions/json?origin=place_id:ChIJdd4hrwug2EcRmSrV3Vo6llI&destination=place_id:ChIJh1a5WhEMa0gRY1JU4PEam8Q&waypoints=place_id:ChIJPeqVDlONbEgRk4X1zrUsKDs|place_id:ChIJ_WegsaCYc0gRlCypaxXgLjs|&key=YOUR_KEY

有人知道他们是否停止支持路线优化?

由于

1 个答案:

答案 0 :(得分:1)

在我看来,当你传入PlaceIds时,这是DirectionsService中的一个错误。我用Javascript API复制了它。使用地址有效:

  waypts = [];
  waypts.push({
  location: "Plymouth, UK", // {placeId:"ChIJPeqVDlONbEgRk4X1zrUsKDs"},
  stopover: true
  });
  waypts.push({
  location: "Bournemouth, UK", // {placeId:"ChIJ_WegsaCYc0gRlCypaxXgLjs"},
  stopover: true
  });
  var request = {
    origin: "London, UK", //{placeId:"ChIJdd4hrwug2EcRmSrV3Vo6llI"},
    destination: "Newquay, UK", //{placeId: "ChIJh1a5WhEMa0gRY1JU4PEam8Q"},
    waypoints: waypts,
    optimizeWaypoints: true,
    travelMode: 'DRIVING'
  };

fiddle using addresses (returns waypoint order=1,0)

但是相同的位置(我使用这些placeIds来获取上面的地址)使用placeIds不起作用:

  waypts = [];
  waypts.push({
  location: {placeId:"ChIJPeqVDlONbEgRk4X1zrUsKDs"},
  stopover: true
  });
  waypts.push({
  location: {placeId:"ChIJ_WegsaCYc0gRlCypaxXgLjs"},
  stopover: true
  });
  var request = {
    origin: {placeId:"ChIJdd4hrwug2EcRmSrV3Vo6llI"},
    destination: {placeId: "ChIJh1a5WhEMa0gRY1JU4PEam8Q"},
    waypoints: waypts,
    optimizeWaypoints: true,
    travelMode: 'DRIVING'
  };

fiddle using placeId (returns ZERO_RESULTS

可能与问题跟踪器中的此问题相关:Issue 8979: Bug: Can't use combination of placeId and String for origin/destination