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 (https://locationiq.com/) 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
<?php $access_token = "YOUR_ACCESS_TOKEN"; $latitude = 37.7749; $longitude = -122.4194; $curl = curl_init(); curl_setopt_array($curl, array( CURLOPT_URL => "https://us1.locationiq.com/v1/reverse?key=$access_token&lat=$latitude&lon=$longitude&format=json", CURLOPT_RETURNTRANSFER => true, CURLOPT_ENCODING => "", CURLOPT_MAXREDIRS => 10, CURLOPT_TIMEOUT => 0, CURLOPT_FOLLOWLOCATION => true, CURLOPT_HTTP_VERSION => CURL_HTTP_VERSION_1_1, CURLOPT_CUSTOMREQUEST => "GET", )); $response = curl_exec($curl); curl_close($curl); $data = json_decode($response, true); print_r($data); ?>
- The first line sets a variable named
$access_tokento the LocationIQ API key. Replace “YOUR_ACCESS_TOKEN” with your actual API key.
$longitudevariables are set to specific coordinates, which will be used as the input for the reverse geocoding operation.
curl_init()function is used to initialize a new cURL session.
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).
curl_exec()function is used to execute the cURL request and return the result as a string, which is stored in the
curl_close()function is used to close the cURL session.
json_decode()function is used to convert the JSON string stored in
$responseinto a PHP array. This array is stored in the
- Finally, the
print_r()function is used to print the contents of the
$dataarray to the output.
Step 4: Parse the Response
- The response obtained from the API is stored in the
json_decodefunction is used to parse the JSON-formatted response into a PHP associative array.
trueargument passed to
json_decodeensures that the response is decoded as an associative array.
print_ris used to display the decoded response data stored in the
$address = $data['address']; $street = $address['road']; $city = $address['city']; $state = $address['state']; $country = $address['country'];
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
cityvalue in the address section, the next available element in this order –
neighbourhood– 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.