【问题标题】:How to optimize route using the google direction API如何使用谷歌方向 API 优化路线
【发布时间】:2017-01-03 11:22:36
【问题描述】:

我正在使用google direction 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

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

谢谢

【问题讨论】:

标签: google-maps


【解决方案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

【讨论】:

    猜你喜欢
    • 2015-02-25
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-07-30
    • 1970-01-01
    相关资源
    最近更新 更多