Google Directions API

使用 Google Directions API 解决 TSP 问题

Google 还提供了一种无需下载 OR 工具即可解决实际地点的简单 TSP 的方法。如果您有 Google Directions API 密钥,则可以使用 Directions API 解决现实世界地点的 TSP,在网址中提供地点并以 JSON 格式返回响应。您需要有自己的免费 Directions API 密钥用于开发,或拥有用于商业用途的企业密钥。

例如,下面的网址可用于查找南澳大利亚州的葡萄酒酿造地区的简短导览,从阿德莱德开始。如果您想在浏览器中尝试此操作,请将网址末尾的 API_KEY 替换为您的密钥。

https://maps.googleapis.com/maps/api/directions/json?origin=Adelaide,SA&destination=Adelaide,SA&waypoints=optimize:true|Barossa+Valley,SA|Clare,SA|Connawarra,SA|McLaren+Vale,SA&key=API_KEY

结果将是一个详细的 JSON 响应,其中详细说明了解决方案,其中包含 Google 地图说明:

{
   "routes" : [
      {
         "bounds" : {
            "northeast" : {
               "lat" : -33.8347115,
               "lng" : 140.8547058
            },
            "southwest" : {
               "lat" : -37.3511758,
               "lng" : 138.4951576
            }
         },
         "copyrights" : "Map data ©2014 Google",
         "legs" : [
            {
               "distance" : {
                  "text" : "139 km",
                  "value" : 139119
               },
               "duration" : {
                  "text" : "1 hour 51 mins",
                  "value" : 6648
               },
               "end_address" : "Clare SA 5453, Australia",
               "end_location" : {
                  "lat" : -33.8333395,
                  "lng" : 138.6117283
               },
               "start_address" : "Adelaide SA, Australia",
               "start_location" : {
                  "lat" : -34.9285894,
                  "lng" : 138.5999429
               },
               "steps" : [
                  {
                     "distance" : {
                        "text" : "70 m",
                        "value" : 70
                     },
                     "duration" : {
                        "text" : "1 min",
                        "value" : 6
                     },
                     "end_location" : {
                        "lat" : -34.9285338,
                        "lng" : 138.6007031
                     },
                     "html_instructions" : "Head \u003cb\u003eeast\u003c/b\u003e on \u003cb\u003eReconciliation Plaza\u003c/b\u003e toward \u003cb\u003eVictoria Square\u003c/b\u003e",
...