-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathreverse_geocode.php
More file actions
51 lines (42 loc) · 1.12 KB
/
reverse_geocode.php
File metadata and controls
51 lines (42 loc) · 1.12 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
<?php
/**
* Geocodio Reverse Geocoding Example
* This example shows you how to geocode a single coordinate using the
* following endpoint:
*
* GET /v1.7/reverse
*
* Note:
* Remember to set your API Key in config.php
*/
require('config.php');
// This is the address that we want to geocode
$coordinate = '33.738987255507, -116.40833849559';
// Construct URL
$url = 'https://api.geocod.io/v1.7/reverse?q=' . urlencode($coordinate) . '&api_key=' . urlencode(API_KEY);
// Perform request and parse the JSON output
$response = json_decode(file_get_contents($url));
if ($response && count($response->results) > 0) {
$firstResult = $response->results[0];
// Print address components
print_r($firstResult->address_components);
// Print the full formatted address
echo $firstResult->formatted_address;
} else {
echo 'No results' . PHP_EOL;
}
/**
* Example output:
*
* stdClass Object
* (
* [number] => 42331
* [street] => Bob Hope
* [suffix] => Dr
* [city] => Rancho Mirage
* [county] => Riverside County
* [state] => CA
* [zip] => 92270
* )
* 42331 Bob Hope Dr, Rancho Mirage, CA 92270
*/