Geocoding is the process that allows converting an address into geographic coordinates. There are two types of geocoding, Forward and Reverse. Forward geocoding is the process of finding associated geographic coordinates (latitude and longitude) from a given address. Reverse geocoding is the process of finding an address from a given coordinate (latitude and longitude). The Geocoding API provides an easy way to convert geographic coordinates into the human-readable addresses and retrieve coordinates from the address.
There are various Geocoding API available for Forward and Reverse geocoding, positionstack API is one of the best free Geocoding API among them. Positionstack API provides an easy-to-use solution forward and reverse geocoding. Positionstack API offers geocoding REST API that enables you to do the following:
The positionstack Geocoding API can be used with any programming languages (PHP, Python, Ruby, jQuery, Nodejs, Go, etc.). In this tutorial, we will show you how to get coordinates data from address and address from geographic coordinates with positionstack geocoding API using PHP.
Follow the below simple steps to integrate positionstack API in PHP.
The Access Key is required to authenticate and access the positionstack API.
http_build_query()
function to pass required params in the positionstack API.access_key
parameter.query
parameter.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',
]);
To get the coordinates data from address, call positionstack API via HTTP GET request using cURL in PHP.
// 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:
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with an https.
https://api.positionstack.com/v1/forward
To get the address from coordinates, call positionstack API via HTTP GET request using cURL in PHP.
// 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:
To make secure API requests use HTTPS (SSL) encryption by calling API URL begins with an https.
https://api.positionstack.com/v1/reverse
After a successful API request, the coordinates/address information will be returned as JSON format. Use json_decode()
function to convert the JSON response to array in PHP.
// Convert API json response to array
$apiResult = json_decode($api_response, true);
The positionstack API is free to use, there also premium plans are available for advanced uses. In the example code, we have used some required parameters on API call. Various configuration options are available in positionstack API, you can use these to customize the search result. For a complete reference, see the documentation of positionstack API.
Do you want to get implementation help, or enhance the functionality of this script? Click here to Submit Service Request