The process of turning an address into geographic coordinates is known as geocoding. Forward and reverse geocoding are the two methods of geocoding. The process of determining associated geographic coordinates (latitude and longitude) from a given address is known as forward geocoding. The method of determining an address from a set of coordinates is known as reverse geocoding (latitude and longitude). The Geocoding API makes converting geographic coordinates to human-readable addresses and retrieving coordinates from addresses simple.
For forward and reverse geocoding, there are a variety of Geocoding APIs available, with positionstack API being one of the top free Geocoding APIs available. The Positionstack API is a simple forward and reverse geocoding solution. Positionstack API provides a geocoding REST API that lets you perform things like:
Geocoding in the Forward Direction
Batch Requests for Reverse Geocoding
Multiple Languages Embeddable Maps
Any programming language can utilise the positionstack Geocoding API (PHP, Python, Ruby, jQuery, Nodejs, Go, etc.). In this article, we’ll show you how to use the positionstack geocoding API in PHP to get coordinates data from an address and an address from geographic coordinates.
To use the positionstack API in PHP, simply follow the steps below.
Obtain an API Access Key
Create an account on positionstack before you begin.
The API key can be found in the dashboard’s Your API Access Key section.
Configuring the API
To authenticate and access the positionstack API, you’ll need your Access Key.
To pass needed params in the positionstack API, generate the query string using the http build query() function.
In the access key parameter, enter the API Access Key.
In the query parameter, specify the coordinates/address.
Forward Geocoding:
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'query' => '1600 Pennsylvania Ave NW',
'region' => 'Washington',
'output' => 'json',
'limit' => 1,
]);
Reverse Geocoding:
$queryString = http_build_query([
'access_key' => 'YOUR_ACCESS_KEY',
'query' => '48.2084,16.3731',
'language' => 'ES',
]);
PHP’s Forward Geocoding
To acquire the coordinates data from an address, use cURL in PHP to make an HTTP GET request to the positionstack API.
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.positionstack.com/v1/forward', $queryString);
// Create a new cURL resource
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL resource
curl_close($ch);
HTTPS Encryption:
Use HTTPS (SSL) encryption to make secure API queries by using API URLs that begin with a https.
https://api.positionstack.com/v1/forward
PHP for reverse geocoding
To get the address from coordinates, use cURL in PHP to make an HTTP GET request to the positionstack API.
// API URL with query string
$apiURL = sprintf('%s?%s', 'http://api.positionstack.com/v1/reverse', $queryString);
// Create a new cURL resource
$ch = curl_init();
// Set URL and other appropriate options
curl_setopt($ch, CURLOPT_URL, $apiURL);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
// Execute and get response from API
$api_response = curl_exec($ch);
// Close cURL resource
curl_close($ch);
HTTPS Encryption: Use HTTPS (SSL) encryption to make secure API queries by using API URLs that begin with a https.
https://api.positionstack.com/v1/reverse
Data on Geographic Coordinates
The coordinates/address information will be returned in JSON format after a successful API request. To convert a JSON response to an array in PHP, use the json decode() function.
// Convert API json response to array
$apiResult = json_decode($api_response, true);
Note :
The positionstack API is free to use, however subscription plans for more sophisticated uses are available. On the API request, we utilised several required parameters in the example code. Positionstack API has a number of configuration parameters that you may use to modify the search results. See the positionstack API documentation for a detailed reference.