From Latitude and Longitude to Addresses: Using LocationIQ and PHP for Reverse Geocoding

LocationIQ provides a simple and powerful API for reverse geocoding, which converts latitude and longitude into a physical address. In this post, we’ll show you how to use LocationIQ’s reverse geocoding API in PHP to retrieve address information from coordinates.

Step 1: Get an Access Token

  • Go to the LocationIQ website ( and click on the “Sign Up” button in the top right corner of the page.
  • Fill out the registration form and click on the “Sign Up” button to create your account.
  • You will receive an email with login URL. Your account will be created when you click this.
  • Once your account is created, log in to the dashboard.
  • Your API Access Token will be displayed on the dashboard. Make a note of it, as you will need it to use the API.
  • Optional: If your deployment is public-facing, its best to secure your access token with HTTP/ IP referrer restrictions.

Step 2: Install cURL

You’ll need to install the cURL library in order to make HTTP requests to the LocationIQ API. If you’re using a Unix-based system like Linux or macOS, you can install cURL using the package manager of your choice. If you’re using Windows, you can download a pre-compiled binary from the cURL website.

Step 3: Write the PHP Code

$access_token = "YOUR_ACCESS_TOKEN";
$latitude = 37.7749;
$longitude = -122.4194;
$curl = curl_init();
curl_setopt_array($curl, array(
  CURLOPT_URL => "$access_token&lat=$latitude&lon=$longitude&format=json",
$response = curl_exec($curl);
$data = json_decode($response, true);
  1. The first line sets a variable named $access_token to the LocationIQ API key. Replace “YOUR_ACCESS_TOKEN” with your actual API key.
  2. The $latitude and $longitude variables are set to specific coordinates, which will be used as the input for the reverse geocoding operation.
  3. The curl_init() function is used to initialize a new cURL session.
  4. The curl_setopt_array() function is used to set a number of options for the cURL session. These options include:
    • CURLOPT_URL: The URL for the LocationIQ API request. The API key and the latitude and longitude coordinates are included in the URL as parameters.
    • CURLOPT_RETURNTRANSFER: Specifies that the cURL request should return the result as a string instead of printing it directly to the output.
    • CURLOPT_ENCODING: Specifies the encoding type to use.
    • CURLOPT_MAXREDIRS: The maximum number of redirects to follow.
    • CURLOPT_TIMEOUT: The maximum number of seconds to allow cURL to execute.
    • CURLOPT_FOLLOWLOCATION: Specifies whether to follow redirects.
    • CURLOPT_HTTP_VERSION: Specifies the HTTP version to use.
    • CURLOPT_CUSTOMREQUEST: Specifies the HTTP request method to use (in this case, GET).
  1. The curl_exec() function is used to execute the cURL request and return the result as a string, which is stored in the $response variable.
  2. The curl_close() function is used to close the cURL session.
  3. The json_decode() function is used to convert the JSON string stored in $response into a PHP array. This array is stored in the $data variable.
  4. Finally, the print_r() function is used to print the contents of the $data array to the output.

Step 4: Parse the Response

  1. The response obtained from the API is stored in the $response variable.
  2. The json_decode function is used to parse the JSON-formatted response into a PHP associative array.
  3. The true argument passed to json_decode ensures that the response is decoded as an associative array.
  4. Finally, print_r is used to display the decoded response data stored in the $data variable.
$address = $data['address'];
$street = $address['road'];
$city = $address['city'];
$state = $address['state'];
$country = $address['country'];

Optional Parameters

LocationIQ provides several optional parameters that can be added to the API request to control the type and level of information returned. Some of the most commonly used optional parameters include:

  • normalizecity: Controls the level of normalization applied to the city name in the response.
  • normalizeaddress: Makes parsing of the address object easier by returning a predictable and defined list of elements. For responses with no city value in the address section, the next available element in this order – city_districtlocalitytownboroughmunicipalityvillagehamletquarterneighbourhood – from the address section will be normalized to city.
  • statecode: Adds state or province code when available to the state_code key inside the address object.
  • postaladdress: Returns address inside the postaladdress key, that is specifically formatted for each country.
  • showdistance: Returns the straight line distance (meters) between the input location and the result’s location.

And that’s all there is! With just a few lines of code, you can easily use LocationIQ’s reverse geocoding API in PHP to retrieve address information from coordinates. Whether you’re building a website, a mobile app, or a standalone application, LocationIQ’s API provides a fast and reliable way to convert latitude and longitude into a physical address.