Find Distance Between Two Addresses

🔑

Do You Have Your Access Token?

Before you proceed, ensure you have your access token at hand. Can't find your token? No worries — our guide will assist you in either creating a new one or finding your existing token.

Step 1: Obtain Coordinates using Geocoding API

The first step is to obtain the latitude and longitude coordinates for the two addresses using the Geocoding API.

Address1: White House: 1600 Pennsylvania Avenue, Washington, D.C, United States

Use the following API request to obtain the coordinates for the White House:

https://us1.locationiq.com/v1/search?key=YOUR_ACCESS_TOKEN&format=json&q=1600%20Pennsylvania%20Avenue%20NW,%20Washington,%20DC%2020500,%20USA&addressdetails=1

The response will contain the coordinates for the White House.

"lat": "38.897699700000004",
"lon": "-77.03655315"

Address 2: Capitol Hill, Washington, D.C, United States

Use the following API request to obtain the coordinates for Capitol Hill:

https://us1.locationiq.com/v1/search?key=YOUR_ACCESS_TOKEN&format=json&q=capitol%20hill,%20Washington,%20DC%2020500,%20USA&addressdetails=1

The response will provide the coordinates for Capitol Hill.

"lat": "38.8890009",
"lon": "-77.0002537"

Step 2: Calculate Distance and ETA using Routing API

With the obtained coordinates, you can now use the Routing API to calculate the distance and ETA between the two points.

❗️

Coordinate Order

When using our Routing APIs, the order of coordinates for latitude and longitude should be entered as 'lon,lat' instead of the typical 'lat,lon' format. Failure to do so will result in incorrect routing information.

https://us1.locationiq.com/v1/directions/driving/-77.03655315,38.897699700000004;-77.0002537,38.8890009?key=YOUR_ACCESS_TOKEN&overview=simplified&annotations=false

Let's break down the components of the API call:

  1. Base URL: https://us1.locationiq.com/v1/directions/driving/

    This is the base URL of the LocationIQ Directions API for driving directions.

  2. Coordinates: -77.03655315,38.897699700000004;-77.0002537,38.8890009

    These are the coordinates of the two points you want to find directions between. The format for each point is longitude,latitude. In this case, you have two points:

    • Point 1: Longitude -77.03655315, Latitude 38.897699700000004
    • Point 2: Longitude -77.0002537, Latitude 38.8890009
  3. API Key: key=YOUR_ACCESS_TOKEN

    Replace YOUR_ACCESS_TOKEN with your actual LocationIQ API key. This key is used to authenticate your API requests.

  4. Overview: overview=simplified

    This parameter specifies the level of detail in the overview of the route. In this case, the "simplified" overview is chosen, which provides a simplified representation of the route.

  5. Annotations: annotations=false

    This parameter determines whether additional information, such as duration, distance, and speed limit annotations, should be included in the response. Setting it to false means these annotations will not be included.

Here's the formatted JSON response:

{
  "code": "Ok",
  "routes": [
    {
      "geometry": "wbllFjneuMnDLKzGyVCCsu@t@iBiLqe@HwMb[qlArBoF|CsBt@{AReBMgEZcCvIyXPWz]A",
      "legs": [
        {
          "steps": [],
          "summary": "",
          "weight": 3717.5,
          "duration": 442.9,
          "distance": 4873
        }
      ],
      "weight_name": "routability",
      "weight": 3717.5,
      "duration": 442.9,
      "distance": 4873
    }
  ],
  "waypoints": [
    {
      "hint": "0960nNbetJwvAAAAAAAAAFEAAAAAAAAAWEweQQAAAADRr4ZBAAAAABgAAAAAAAAAKAAAAAAAAABUgQEAZn5o-1WGUQL3g2j7JIhRAgEAnwXH89Mq",
      "distance": 133.918709,
      "name": "",
      "location": [-77.037978, 38.897237]
    },
    {
      "hint": "sSXUoP___38lAAAAPgAAAAAAAAAHAAAAQC8lQi142kEAAAAASY_xQCUAAAA-AAAAAAAAAAcAAABUgQEAhhBp-ylmUQLCEWn7KWZRAgAAPwvH89Mq",
      "distance": 27.422745,
      "name": "4th Street Southeast",
      "location": [-77.00057, 38.889001]
    }
  ]
}

Let's break down the structure and the meaning of each field:

  • "code": "Ok": This indicates the status of the response. In this case, the code "Ok" suggests that the request was successful and the data is available.

  • "routes": This is an array that contains the different calculated routes between the specified source and destination points. In this case, there is one route in the array.

    • "geometry": This field contains a string that represents the encoded geometry of the route. This string can be decoded to obtain the route's polyline, which is a series of coordinates that outline the route.

    • "legs": This is an array containing information about different legs of the route. In this case, there is one leg in the array.

      • "steps": This array would typically contain a series of steps that describe turn-by-turn directions for the route. However, in this response, it appears to be empty.

      • "summary", "weight", "duration", "distance": These fields provide summary information about the leg. "weight" usually refers to a measure of route complexity or preference, "duration" indicates the estimated travel time, and "distance" indicates the distance of the leg.

    • "weight_name", "weight", "duration", "distance": These fields provide summary information about the route as a whole. "weight_name" might refer to the metric used to calculate route preferences or complexities, "weight" is the same value as the one in the leg, and "duration" and "distance" provide the total estimated travel time and distance for the entire route.

  • "waypoints": This is an array containing information about the waypoints along the route.

    • For each waypoint:

      • "hint": This is a hint associated with the waypoint, likely used for internal processing or optimization.

      • "distance": The distance from the previous waypoint, helping to understand the spacing between waypoints.

      • "name": The name associated with the waypoint, such as a street name or landmark. It might be empty in some cases.

      • "location": The latitude and longitude coordinates of the waypoint.