forked from anthony-mills/google-places
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathgoogleGeocoding.php
210 lines (161 loc) · 8.13 KB
/
googleGeocoding.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
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
<?php
class googleGeocoding {
const OK_STATUS = 'OK';
const COMPONENTS_FIELD_NAME = 'components';
const LAT_LNG_FIELD_NAME = 'latlng';
const SENSOR_FIELD_NAME = 'sensor';
const ADDRESS_FIELD_NAME = 'address';
const BOUNDS_FIELD_NAME = 'bounds';
const LANGUAGE_FIELD_NAME = 'language';
const REGION_FIELD_NAME = 'region';
public $_outputType = 'json'; //either json, xml or array
public $_errors = array();
private $_apiKey = '';
private $_apiUrl = 'https://maps.googleapis.com/maps/api/geocode';
// REQUIRED
private $_address; // Required if no latlng or components - The address that you want to geocode.
private $_latlng; // Required if no address or components - The textual latitude/longitude value for which you wish to obtain the closest, human-readable address. See Reverse Geocoding for more information.
private $_components; // Required if no address or latlng - A component filter for which you wish to obtain a geocode. See Component Filtering for more information. The components filter will also be accepted as an optional parameter if an address is provided.
private $_sensor = 'false'; // Indicates whether or not the geocoding request comes from a device with a location sensor. This value must be either true or false.
// OPTIONAL
private $_bounds; // The bounding box of the viewport within which to bias geocode results more prominently. This parameter will only influence, not fully restrict, results from the geocoder.
private $_language = 'en'; // The language in which to return results. See the list of supported domain languages. Note that we often update supported languages so this list may not be exhaustive. If language is not supplied, the geocoder will attempt to use the native language of the domain from which the request is sent wherever possible.
private $_region; // The region code, specified as a ccTLD ("top-level domain") two-character value. This parameter will only influence, not fully restrict, results from the geocoder. (For more information see Region Biasing below.)
private $_curloptSslVerifypeer = true; // option CURLOPT_SSL_VERIFYPEER with true value working not always
/**
* constructor - creates a googleGeocoding object with the specified API Key
*
* @param $apiKey - the API Key to use
*/
public function __construct($apiKey) {
$this->_apiKey = $apiKey;
}
/**
* executeAPICall - Executes the Google Geocode API call specified by this class's members and returns the results as an array
*
* @return mixed - the array resulting from the Google Geocode API call specified by the members of this class
*/
public function executeAPICall() {
$this->_checkErrors();
$urlParameters = $this->_formatParametersForURL();
$URLToCall = $this->_apiUrl . '/' . $this->_outputType . '?'. $urlParameters;
$result = json_decode($this->_curlCall($URLToCall), true);
$formattedResults = $this->_formatResults($result);
return $formattedResults;
}
/**
* _checkErrors - Checks to see if this google Geocoding request has all of the required fields as far as we know. In the
* event that it doesn't, it'll populate the _errors array with an error message for each error found.
*/
private function _checkErrors() {
if (empty($this->_apiKey)) {
$this->_errors[] = 'API Key is is required but is missing.';
}
if (($this->_outputType != 'json') && ($this->outputType != 'xml')) {
$this->_errors[] = 'OutputType is required but is missing.';
}
}
/**
* _formatParametersForURL - Formats the parameters of the Google Geocoding call for a GET request
*
* @return string - the parameters in URL string form
*/
private function _formatParametersForURL() {
return self::ADDRESS_FIELD_NAME.'='.$this->_address .
'&'.self::LAT_LNG_FIELD_NAME.'='.$this->_latlng .
'&'.self::COMPONENTS_FIELD_NAME.'='.$this->_components .
'&'.self::LANGUAGE_FIELD_NAME.'='.$this->_language .
'&'.self::BOUNDS_FIELD_NAME.'='.$this->_bounds .
'&'.self::REGION_FIELD_NAME.'='.$this->_region .
'&'.self::SENSOR_FIELD_NAME.'='.$this->_sensor;
}
/**
* _formatResults - Formats the results in such a way that they're easier to parse (especially addresses)
*
* @param mixed $result - the Google Geocode result array
* @return mixed - the formatted Google Geocode result array
*/
private function _formatResults($result) {
$formattedResults = array();
$formattedResults['errors'] = $this->_errors;
// for backward compatibility
$resultColumnName = 'result';
if (!isset($result[$resultColumnName])) {
$resultColumnName = 'results';
}
$formattedResults['result'] = $result[$resultColumnName];
if(isset($result['status']) && $result['status'] == self::OK_STATUS && isset($result[$resultColumnName]['address_components'])) {
foreach($result[$resultColumnName]['address_components'] as $key => $component) {
if($component['types'][0]=='street_number') {
$address_street_number = $component['short_name'];
}
if($component['types'][0]=='route') {
$address_street_name = $component['short_name'];
}
if($component['types'][0]=='locality') {
$address_city = $component['short_name'];
}
if($component['types'][0]=='administrative_area_level_1') {
$address_state = $component['short_name'];
}
if($component['types'][0]=='postal_code') {
$address_postal_code = $component['short_name'];
}
}
$formattedResults['result']['address_fixed']['street_number'] = $address_street_number;
$formattedResults['result']['address_fixed']['address_street_name'] = $address_street_name;
$formattedResults['result']['address_fixed']['address_city'] = $address_city;
$formattedResults['result']['address_fixed']['address_state'] = $address_state;
$formattedResults['result']['address_fixed']['address_postal_code'] = $address_postal_code;
}
return $formattedResults;
}
/**
* _curlCall - Executes a curl call to the specified url with the specified data to post and returns the result. If
* the post data is empty, the call will default to a GET
*
* @param $url - the url to curl to
* @param array $dataToPost - the data to post in the curl call (if any)
* @return mixed - the response payload of the call
*/
private function _curlCall($url, $dataToPost = array()) {
$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, FALSE);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, $this->_curloptSslVerifypeer);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, TRUE);
if (!empty($dataToPost)) {
curl_setopt($ch, CURLOPT_POSTFIELDS, $dataToPost);
}
$body = curl_exec($ch);
curl_close($ch);
return $body;
}
/***********************
* Getters and Setters *
***********************/
public function setAddress($address) {
$this->_address = $address;
}
public function setLatlng($latlng) {
$this->_latlng = $latlng;
}
public function setComponents($components) {
$this->_components = $components;
}
public function setSensor($sensor) {
$this->_sensor = $sensor;
}
public function setBounds($bounds) {
$this->_bounds = $bounds;
}
public function setLanguage($language) {
$this->_language = $language;
}
public function setRegion($region) {
$this->_region = $region;
}
public function setCurloptSslVerifypeer($curloptSslVerifypeer) {
$this->_curloptSslVerifypeer = $curloptSslVerifypeer;
}
}