Google Directions API

  • Google Directions API can be used to solve Traveling Salesperson Problems (TSPs) for real-world locations without needing OR-Tools.

  • This requires a Google Directions API key, obtainable for free for development or as an enterprise key for commercial purposes.

  • By providing locations within a structured URL, the API returns a JSON response detailing the optimal route and directions.

  • The response includes comprehensive information like distances, durations, and step-by-step navigation instructions.

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

Except as otherwise noted, the content of this page is licensed under the Creative Commons Attribution 4.0 License, and code samples are licensed under the Apache 2.0 License. For details, see the Google Developers Site Policies. Java is a registered trademark of Oracle and/or its affiliates.

Last updated 2024-08-28 UTC.