Google Directions API

Solving TSPs with the Google Directions API

Google also provides a way to solve simple TSPs of real-world locations without downloading OR-Tools. If you have a Google Directions API key, you can solve TSPs of real-world locations with the Directions API , providing the locations in a URL and getting the response back as JSON. You'll need your own free Directions API key for development, or an enterprise key for commercial use.

As an example, here's a URL that can be used to find a short tour of winemaking regions in South Australia, beginning in Adelaide. If you want to try this from your browser, replace API_KEY at the end of the URL with your 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

The result will be a long JSON response detailing the solution, complete with Google Maps directions:

{
   "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",
...