-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathbdGoogleMapsEmbedApi.php
More file actions
317 lines (305 loc) · 10.5 KB
/
bdGoogleMapsEmbedApi.php
File metadata and controls
317 lines (305 loc) · 10.5 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
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
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
<?php
/**
* This classes uses google maps embed api for fast google maps
* more info https://developers.google.com/maps/documentation/embed/guide?hl=nl
*
* make sure to set your apikey before using other functions by doing :
* bdGoogleMapsEmbedApi::$API_KEY = 'your apikey'
*
* @author Barry Dam 2014
* @version 1.0.2
*/
class bdGoogleMapsEmbedApi {
/**
* set the apikey befor using this script by
* bdGoogleMapsEmbedApi::$API_KEY = KEY
*/
public static $API_KEY = false;
/**
* private vars
*/
private $arrSettings = array(
'mode' => false, //place, directions, view or search
'width' => '100%',
'height' => 300 // default
);
/**
* Default params needed for the Google Maps Embed API
* $array will be extended in )__construct according to the map mode
*/
private $arrApiParamsRequired = array();
private $arrApiParamsOptional = array(
'center' => false, // lat,lng
'zoom' => false, // (int) zoomlevel
'maptype' => 'roadmap', // (string) roadmap (default) or satelite
'language' => false, // if not supported default lang by visitor browser will be used in de API
'region' => false // defines the appropriate borders and labels to displa
);
/**
* Magic methods
*/
public function __construct($getMode = false, $getArrOptions = array())
{
$arrModes = array('place', 'directions', 'view', 'search' );
if (! in_array($getMode, $arrModes)) {
throw new Exception($getMode.' is not a valid mode! use '.implode(' or ', $arrModes));
}
$this->arrSettings['mode'] = $getMode;
/**
* Add the required params to $arrApiParamsRequired
* && Add some optional params
*/
switch ($getMode) {
case 'place' :
$this->arrApiParamsRequired['q'] = false; // (string) place or address
break;
case 'directions' :
$this->arrApiParamsRequired = array(
'origin' => false,
'destination' => false
);
$arrApiParamsOptional['waypoints'] = false; // (string) when just one waypoint or (array) when multiple waypoints are used
$arrApiParamsOptional['mode'] = false; // (string) driving, walking, bicycling, transit or flying
$arrApiParamsOptional['avoid'] = false; // (string or array) tolls, ferries and / or higwights (e.g. tolls or array('tolls','ferries'))
$arrApiParamsOptional['units'] = false; // (string) metric or imperial
break;
case 'view' :
$this->arrApiParamsRequired['center'] = false; // (string) lat lng coords
unset($this->arrApiParamsOptional['center']);
break;
case 'search' :
$this->arrApiParamsRequired['q'] = false ; // (string) specifies the search term. It can include a geographic restriction, such as in+Seattle or near+98033.
break;
}
$this->setOptions($getArrOptions);
}
/**
* Setters
*/
/**
* The height of the iframe
*/
public function setHeight($getIntHeight = false) {
$getIntHeight = str_ireplace('px', '', $getIntHeight);
$this->arrSettings['height'] = $getIntHeight;
}
/**
* The width of the iframe
*/
public function setWidth($getIntWidth = false) {
$getIntWidth = str_ireplace('px', '', $getIntWidth);
if (is_numeric($getIntWidth)) $this->arrSettings['width'] = $getIntWidth;
}
/**
* @param associative (array) $getArrOptions
*/
public function setOptions($getArrOptions = array())
{
if (! $getArrOptions || ! is_array($getArrOptions)) return;
foreach ($getArrOptions as $key => $val) $this->setOption($key, $val);
}
public function setOption($getKey = false, $getValue = false)
{
if (! $getKey || ! $getValue || ! $this->arrSettings['mode']) return;
/**
* First check for required params
*/
$boolOptionSet = false;
switch ($getKey) {
case 'q' :
$boolOptionSet = true;
if (in_array($this->arrSettings['mode'], array('place', 'search'))) {
$this->arrApiParamsRequired['q'] = preg_replace('/\s+/', '+', $getValue);
}
break;
case 'origin' :
case 'destination' :
$boolOptionSet = true;
if ($this->arrSettings['mode'] === 'directions') {
$this->arrApiParamsRequired[$getKey] = preg_replace('/\s+/', '+', $getValue);
}
break;
case 'center' :
$boolOptionSet = true;
if ($this->arrSettings['mode'] === 'view') {
/**
* When mode is view. center can only be a valid lat lng coord!
*/
$arrCoords = explode(',', $getValue);
if (count($arrCoords) == 2) {
$lat = trim($arrCoords[0]);
$lng = trim($arrCoords[1]);
if (
is_numeric($lat) &&
is_numeric($lng)
) $this->arrApiParamsRequired['center'] = $lat.','.$lng;
}
} else if ($this->arrSettings['mode'] !== 'place') {
$this->arrApiParamsRequired['center'] = preg_replace('/\s+/', '+', $getValue);
}
break;
}
if ($boolOptionSet) return true;
/**
* Optional directions params
*/
if ($this->arrSettings['mode'] === 'directions') {
switch ($getKey) {
case 'waypoints':
case 'avoid':
$boolOptionSet = true;
if (is_array($getValue))
$getValue = implode('|', $getValue);
$this->arrApiParamsOptional[$getKey] = preg_replace('/\s+/', ',', $getValue);
break;
case 'mode' :
$boolOptionSet = true;
if (in_array($getValue, array('driving', 'walking', 'bicycling', 'transit', 'flying')))
$this->arrApiParamsOptional['mode'] = $getValue;
break;
case 'units' :
$boolOptionSet = true;
if (in_array($getValue, array('metric', 'imperial')))
$this->arrApiParamsOptional['mode'] = $getValue;
break;
}
}
if ($boolOptionSet) return true;
/**
* Other optional params
*/
switch ($getKey) {
case 'zoom' :
$boolOptionSet = true;
if (is_numeric($getValue))
$this->arrApiParamsOptional['zoom'] = $getValue;
break;
case 'maptype' :
if (in_array($getValue, array('roadmap', 'satellite')))
$this->arrApiParamsOptional['maptype'] = $getValue;
$boolOptionSet = true;
break;
default :
if (array_key_exists($getKey, $this->arrApiParamsOptional)) {
$boolOptionSet = true;
$this->arrApiParamsOptional[$getKey] = $getValue;
} else {
$boolOptionSet = false;
}
break;
}
return $boolOptionSet;
}
/**
* getters
*/
private function getApiUrl() {
if (! self::$API_KEY)
throw new Exception('ApiKey is not set!');
$arrUrlParams = array();
$strUrlParams = '';
/**
* Check if the required fields are set
*/
foreach ($this->arrApiParamsRequired as $keyRequired => $valueRequired) {
if ($valueRequired === false)
throw new Exception('Required param '.$keyRequired.' is not set!');
else
$strUrlParams .= '&'.$keyRequired.'='.$valueRequired;
}
/**
* Check the optional params
*/
foreach ($this->arrApiParamsOptional as $keyOptional => $valueOptional) {
if ($valueOptional !== false) $strUrlParams .= '&'.$keyOptional.'='.$valueOptional;
}
/**
* build and return the api url
*/
return 'https://www.google.com/maps/embed/v1/'.$this->arrSettings['mode'].'?key='.self::$API_KEY.$strUrlParams;
}
public function getContent()
{
$strUrl = $this->getApiUrl();
if (! $strUrl) return false;
$strDivWith = (stripos($this->arrSettings['width'], '%') === false)? $this->arrSettings['width'].'px' : $this->arrSettings['width'] ;
$strDivHeight = (stripos($this->arrSettings['height'], '%') === false)? $this->arrSettings['height'].'px' : $this->arrSettings['height'] ;
return '
<div class="bdGoogleMapsEmbedApi" style="height:'.$strDivHeight.';width:'.$strDivWith.';">
<iframe
width="100%"
height="100%"
frameborder="0" style="border:0"
src="'.$strUrl.'">
</iframe>
</div>
';
}
public function echoContent()
{
echo $this->getContent();
}
/**
* Convenience methods
*/
public static function getAddressFromLatLng($getLat = false, $getLng = false)
{
if (! $getLat || ! $getLng) return false;
$strUrl = 'http://maps.googleapis.com/maps/api/geocode/json?latlng='.$getLat.','.$getLng.'&sensor=true';
$strResult = file_get_contents($strUrl);
if (! $strResult) return $getLat.','.$getLng;
$arrResult = json_decode($strResult, true);
if (empty($arrResult['results'][0]['formatted_address']))
return $getLat.','.$getLng;
else
return $arrResult['results'][0]['formatted_address'];
}
public static function getLatLngFromAddress($getAddress = false)
{
if (! $getAddress) return false ;
$strUrl = 'http://maps.googleapis.com/maps/api/geocode/json?address='. preg_replace('/\s+/', ',', $getAddress) .'&sensor=true';
$strResult = file_get_contents($strUrl);
if (! $strResult) return $getLat.','.$getLng;
$arrResult = json_decode($strResult, true);
if (empty($arrResult['results'][0]['geometry']['location']))
return $getLat.','.$getLng;
else
return $arrResult['results'][0]['geometry']['location']['lat'].','.$arrResult['results'][0]['geometry']['location']['lng'];
}
/**
* Static methods
*/
/**
* Can only use this when @param (string) $getMode is 'place' or 'view'
*/
public static function createByLatLng($getLat = false, $getLng = false, $getMode = 'place')
{
if (! $getLat || ! $getLng)
return false;
if (! in_array($getMode, array('place', 'view')))
throw new Exception('Param $getMode can only be place or view');
$arrOptions = array();
if ($getMode == 'place')
$arrOptions['q'] = self::getAddressFromLatLng($getLat, $getLng);
// both needed for place and view
$arrOptions['center'] = $getLat.','.$getLng;
$obj = new bdGoogleMapsEmbedApi($getMode, $arrOptions);
$obj->setOption('zoom', 15);
return $obj;
}
public static function createByAddress($getAddress = false, $getMode = 'place')
{
if (! $getAddress) return false;
if (! in_array($getMode, array('place', 'view')))
throw new Exception('Param $getMode can only be place or view');
$arrOptions = array();
if ($getMode == 'place')
$arrOptions['q'] = $getAddress;
// both needed for place and view
$arrOptions['center'] = self::getLatLngFromAddress($getAddress);
$obj = new bdGoogleMapsEmbedApi($getMode, $arrOptions);
$obj->setOption('zoom', 15);
return $obj;
}
};
?>