10 defined(
'JPATH_PLATFORM') or die;
39 parent::__construct($options = null, $uri = null);
40 $this->http = $http ? $http :
new JHttp($this->options);
50 public function getKey()
52 return $this->getOption(
'key');
64 public function setKey($key)
66 $this->setOption(
'key', $key);
78 public function getMapID()
80 return $this->getOption(
'mapid') ? $this->getOption(
'mapid') :
'map_canvas';
92 public function setMapID($id)
94 $this->setOption(
'mapid', $id);
106 public function getMapClass()
108 return $this->getOption(
'mapclass') ? $this->getOption(
'mapclass') :
'';
120 public function setMapClass($class)
122 $this->setOption(
'mapclass', $class);
134 public function getMapStyle()
136 return $this->getOption(
'mapstyle') ? $this->getOption(
'mapstyle') :
'';
148 public function setMapStyle($style)
150 $this->setOption(
'mapstyle', $style);
162 public function getMapType()
164 return $this->getOption(
'maptype') ? $this->getOption(
'maptype') :
'ROADMAP';
176 public function setMapType($type)
178 $this->setOption(
'maptype', strtoupper($type));
190 public function getAdditionalMapOptions()
192 return $this->getOption(
'mapoptions') ? $this->getOption(
'mapoptions') : array();
204 public function setAdditionalMapOptions($options)
206 $this->setOption(
'mapoptions', $options);
218 public function getAdditionalJavascript()
220 return $this->getOption(
'extrascript') ? $this->getOption(
'extrascript') :
'';
232 public function setAdditionalJavascript($script)
234 $this->setOption(
'extrascript', $script);
246 public function getZoom()
248 return $this->getOption(
'zoom') ? $this->getOption(
'zoom') : 0;
260 public function setZoom($zoom)
262 $this->setOption(
'zoom', $zoom);
274 public function getCenter()
276 return $this->getOption(
'mapcenter') ? $this->getOption(
'mapcenter') : array(0, 0);
290 public function setCenter($location, $title =
true, $markeroptions = array())
294 $title = is_string($title) ? $title : null;
296 if (!$marker = $this->addMarker($location, $title, $markeroptions))
300 $location = $marker[
'loc'];
302 elseif (is_string($location))
304 $geocode = $this->geocodeAddress($location);
311 $location = $geocode[
'geometry'][
'location'];
312 $location = array_values($location);
315 $this->setOption(
'mapcenter', $location);
331 public function addMarker($location, $title = null, $options = array())
333 if (is_string($location))
340 $geocode = $this->geocodeAddress($location);
347 $location = $geocode[
'geometry'][
'location'];
351 $title = implode(
', ', $location);
354 $location = array_values($location);
355 $marker = array(
'loc' => $location,
'title' => $title,
'options' => $options);
357 $markers = $this->listMarkers();
358 $markers[] = $marker;
359 $this->setOption(
'markers', $markers);
371 public function listMarkers()
373 return $this->getOption(
'markers') ? $this->getOption(
'markers') : array();
385 public function deleteMarker($index = null)
387 $markers = $this->listMarkers();
391 $index = count($markers) - 1;
394 if ($index >= count($markers) || $index < 0)
396 throw new OutOfBoundsException(
'Marker index out of bounds.');
399 $marker = $markers[$index];
400 unset($markers[$index]);
401 $markers = array_values($markers);
402 $this->setOption(
'markers', $markers);
414 public function isAsync()
416 return $this->getOption(
'async') === null ?
true : $this->getOption(
'async');
426 public function useAsync()
428 $this->setOption(
'async',
true);
440 public function useSync()
442 $this->setOption(
'async',
false);
454 public function getAsyncCallback()
456 return $this->getOption(
'callback') ? $this->getOption(
'callback') :
'initialize';
468 public function setAsyncCallback($callback)
470 $this->setOption(
'callback', $callback);
482 public function hasSensor()
484 return $this->getOption(
'sensor') === null ?
false : $this->getOption(
'sensor');
494 public function useSensor()
496 $this->setOption(
'sensor',
true);
508 public function noSensor()
510 $this->setOption(
'sensor',
false);
522 public function getAutoload()
524 return $this->getOption(
'autoload') ? $this->getOption(
'autoload') :
'false';
536 public function setAutoload($type =
'onload')
538 $this->setOption(
'autoload', $type);
550 public function getHeader()
552 if (!$this->getOption(
'key'))
554 throw new UnexpectedValueException(
'A Google Maps API key is required.');
557 $zoom = $this->getZoom();
558 $center = $this->getCenter();
559 $maptype = $this->getMapType();
560 $id = $this->getMapID();
561 $scheme = $this->isSecure() ?
'https' :
'http';
562 $key = $this->getKey();
563 $sensor = $this->hasSensor() ?
'true' :
'false';
565 $setup =
'var mapOptions = {';
566 $setup .=
"zoom: {$zoom},";
567 $setup .=
"center: new google.maps.LatLng({$center[0]},{$center[1]}),";
568 $setup .=
"mapTypeId: google.maps.MapTypeId.{$maptype},";
569 $setup .= substr(json_encode($this->getAdditionalMapOptions()), 1, -1);
571 $setup .=
"var map = new google.maps.Map(document.getElementById('{$id}'), mapOptions);";
573 foreach ($this->listMarkers() as $marker)
575 $loc = $marker[
'loc'];
576 $title = $marker[
'title'];
577 $options = $marker[
'options'];
579 $setup .=
'new google.maps.Marker({';
580 $setup .=
"position: new google.maps.LatLng({$loc[0]},{$loc[1]}),";
581 $setup .=
'map: map,';
582 $setup .=
"title:'{$title}',";
583 $setup .= substr(json_encode($options), 1, -1);
587 $setup .= $this->getAdditionalJavascript();
589 if ($this->isAsync())
591 $asynccallback = $this->getAsyncCallback();
593 $output =
'<script type="text/javascript">';
594 $output .=
"function {$asynccallback}() {";
598 $onload =
"function() {";
599 $onload .=
'var script = document.createElement("script");';
600 $onload .=
'script.type = "text/javascript";';
601 $onload .=
"script.src = '{$scheme}://maps.googleapis.com/maps/api/js?key={$key}&sensor={$sensor}&callback={$asynccallback}';";
602 $onload .=
'document.body.appendChild(script);';
607 $output =
"<script type='text/javascript' src='{$scheme}://maps.googleapis.com/maps/api/js?key={$key}&sensor={$sensor}'>";
608 $output .=
'</script>';
609 $output .=
'<script type="text/javascript">';
611 $onload =
"function() {";
616 switch ($this->getAutoload())
619 $output .=
"window.onload={$onload};";
623 $output .=
"$(document).ready({$onload});";
627 $output .=
"window.addEvent('domready',{$onload});";
631 $output .=
'</script>';
643 public function getBody()
645 $id = $this->getMapID();
646 $class = $this->getMapClass();
647 $style = $this->getMapStyle();
649 $output =
"<div id='{$id}'";
653 $output .=
" class='{$class}'";
658 $output .=
" style='{$style}'";
661 $output .=
'></div>';
675 public function geocodeAddress($address)
677 $url =
'http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=' . urlencode($address);
678 $response = $this->http->get($url);
680 if ($response->code < 200 || $response->code >= 300)
682 throw new RuntimeException(
'Error code ' . $response->code .
' received geocoding address: ' . $response->body .
'.');
685 $data = json_decode($response->body,
true);
689 throw new RuntimeException(
'Invalid json received geocoding address: ' . $response->body .
'.');
691 if ($data[
'status'] !=
'OK')
696 return $data[
'results'][0];