-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathbatch_geocode.php
90 lines (75 loc) · 2.41 KB
/
batch_geocode.php
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
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
<?php
/**
* Geocodio Batch Geocoding Example
* This example shows you how to geocode up to 10,000 addresses at once using the
* batch geocoding endpoint.
*
* POST /v1.7/geocode
*
* Note:
* Remember to set your API Key in config.php
*/
require('config.php');
// Construct URL
$url = 'https://api.geocod.io/v1.7/geocode?api_key=' . urlencode(API_KEY);
// Define addresses to geocode
$addresses = [
'42370 Bob Hope Drive, Rancho Mirage CA',
'1290 Northbrook Court Mall, Northbrook IL',
'4410 S Highway 17 92, Casselberry FL',
'15000 NE 24th Street, Redmond WA',
'17015 Walnut Grove Drive, Morgan Hill CA'
];
// Encode addresses into JSON
$json = json_encode($addresses);
// Create CURL request
$ch = curl_init($url);
// Very large batch requests can take a couple of minutes to run, so we make sure to give plenty
// of room before timing out.
curl_setopt($ch, CURLOPT_TIMEOUT, 60 * 10);
// General CURL options
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POSTFIELDS, $json);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, true);
curl_setopt($ch, CURLOPT_HTTPHEADER, [
'Content-Type: application/json',
'Content-Length: ' . strlen($json)
]);
// Get response and decode the JSON
$response = json_decode(curl_exec($ch));
curl_close($ch);
if (!isset($response->results)) {
echo 'Error!';
var_dump($response);
}
// Output decoded json
foreach ($response->results as $result) {
echo 'Input: ' . $result->query . PHP_EOL;
$geocodeResults = $result->response->results;
echo 'Output: ';
if (count($geocodeResults) > 0) {
$firstResult = $geocodeResults[0];
echo $firstResult->location->lat . ', ' . $firstResult->location->lng;
} else {
echo 'No results';
}
echo PHP_EOL . PHP_EOL; // Line break
}
/**
* Example output:
*
* Input: 42370 Bob Hope Drive, Rancho Mirage CA
* Output: 33.738987255507, -116.40833849559
*
* Input: 1290 Northbrook Court Mall, Northbrook IL
* Output: 42.120176, -87.838815
*
* Input: 4410 S Highway 17 92, Casselberry FL
* Output: 28.661468, -81.313989
*
* Input: 15000 NE 24th Street, Redmond WA
* Output: 47.63150504878, -122.14160607317
*
* Input: 17015 Walnut Grove Drive, Morgan Hill CA
* Output: 37.148936142857, -121.68332857143
*/