2 namespace Geo\View\Helper;
4 use Cake\Core\Configure;
5 use Cake\Core\Exception\Exception;
6 use Cake\Routing\Router;
10 use Geo\View\Helper\JsBaseEngineTrait;
13 * This is a CakePHP helper that helps users to integrate GoogleMap v3
14 * into their application by only writing PHP code. This helper depends on jQuery.
16 * Capable of resetting itself (full or partly) for multiple maps on a single view.
18 * CodeAPI: http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/basics.html
19 * Icons/Images: http://gmapicons.googlepages.com/home
22 * @author Mark Scherer
23 * @link http://www.dereuromark.de/2010/12/21/googlemapsv3-cakephp-helper/
24 * @license MIT License (http://www.opensource.org/licenses/mit-license.php)
25 * @property \Cake\View\Helper\HtmlHelper $Html
27 class GoogleMapHelper extends Helper {
29 use JsBaseEngineTrait;
31 const API = 'maps.google.com/maps/api/js';
33 const STATIC_API = 'maps.google.com/maps/api/staticmap';
38 public static $mapCount = 0;
43 public static $markerCount = 0;
48 public static $iconCount = 0;
53 public static $infoWindowCount = 0;
58 public static $infoContentCount = 0;
60 const TYPE_ROADMAP = 'R';
62 const TYPE_HYBRID = 'H';
64 const TYPE_SATELLITE = 'S';
66 const TYPE_TERRAIN = 'T';
72 self::TYPE_ROADMAP => 'ROADMAP',
73 self::TYPE_HYBRID => 'HYBRID',
74 self::TYPE_SATELLITE => 'SATELLITE',
75 self::TYPE_TERRAIN => 'TERRAIN'
78 const TRAVEL_MODE_DRIVING = 'D';
80 const TRAVEL_MODE_BICYCLING = 'B';
82 const TRAVEL_MODE_TRANSIT = 'T';
84 const TRAVEL_MODE_WALKING = 'W';
89 public $travelModes = [
90 self::TRAVEL_MODE_DRIVING => 'DRIVING',
91 self::TRAVEL_MODE_BICYCLING => 'BICYCLING',
92 self::TRAVEL_MODE_TRANSIT => 'TRANSIT',
93 self::TRAVEL_MODE_WALKING => 'WALKING'
101 public $helpers = ['Html'];
104 * Google maker config instance variable
108 public $markers = [];
113 public $infoWindows = [];
118 public $infoContents = [];
128 public $matching = [];
138 protected $_mapIds = []; // Remember already used ones (valid xhtml contains ids not more than once)
145 protected $_defaultConfig = [
146 'zoom' => null, // global, both map and staticMap
147 'lat' => null, // global, both map and staticMap
148 'lng' => null, // global, both map and staticMap
150 'type' => self::TYPE_ROADMAP,
157 'streetViewControl' => false,
158 'navigationControl' => true,
159 'mapTypeControl' => true,
160 'scaleControl' => true,
161 'scrollwheel' => false,
162 'keyboardShortcuts' => true,
165 'scaleOptions' => [],
166 'defaultLat' => 51, // only last fallback, use Configure::write('Google.lat', ...); to define own one
167 'defaultLng' => 11, // only last fallback, use Configure::write('Google.lng', ...); to define own one
174 //'shadow' => true // for icons
176 'geolocate' => false,
179 'showMarker' => true,
180 //'showInfoWindow' => true,
183 'useMultiple' => false, // Using single infowindow object for all
189 'disableAutoPan' => false
192 //'autoCenter' => true,
193 'animation' => null, // BOUNCE or DROP https://developers.google.com/maps/documentation/javascript/3.exp/reference#Animation
194 'icon' => null, // => default (red marker) //http://google-maps-icons.googlecode.com/files/home.png
199 'draggable' => false,
201 'directions' => false, // add form with directions
202 'open' => false, // New in 1.5
205 'id' => 'map_canvas',
217 'color' => '#FF0000',
222 'travelMode' => self::TRAVEL_MODE_DRIVING,
223 'unitSystem' => 'METRIC',
224 'directionsDiv' => null,
227 'geolocate' => null //TODO
230 'keydragzoom' => false, // http://google-maps-utility-library-v3.googlecode.com/svn/tags/keydragzoom/
231 'markermanager' => false, // http://google-maps-utility-library-v3.googlecode.com/svn/tags/markermanager/
232 'markercluster' => false, // http://google-maps-utility-library-v3.googlecode.com/svn/tags/markerclusterer/
234 'autoCenter' => false, // try to fit all markers in (careful, all zooms values are omitted)
235 'autoScript' => false, // let the helper include the necessary js script links
236 'block' => true, // for scripts
237 'localImages' => false,
238 'https' => null, // auto detect
245 protected $_runtimeConfig = [];
250 protected $_apiIncluded = false;
255 protected $_gearsIncluded = false;
260 protected $_located = false;
263 * @param \Cake\View\View|null $View
264 * @param array $config
266 public function __construct(View $View, array $config = []) {
267 parent::__construct($View, $config);
271 * @param array $config
274 public function initialize(array $config) {
275 parent::initialize($config);
277 $defaultConfig = Hash::merge($this->_defaultConfig, (array)Configure::read('GoogleMap'));
278 $config = Hash::merge($defaultConfig, $config);
280 if (isset($config['api']) && !isset($config['map']['api'])) {
281 $config['map']['api'] = $config['api'];
283 if (isset($config['zoom']) && !isset($config['map']['zoom'])) {
284 $config['map']['zoom'] = $config['zoom'];
286 if (isset($config['lat']) && !isset($config['map']['lat'])) {
287 $config['map']['lat'] = $config['lat'];
289 if (isset($config['lng']) && !isset($config['map']['lng'])) {
290 $config['map']['lng'] = $config['lng'];
292 if (isset($config['type']) && !isset($config['map']['type'])) {
293 $config['map']['type'] = $config['type'];
295 if (isset($config['size'])) {
296 $config['div']['width'] = $config['size']['width'];
297 $config['div']['height'] = $config['size']['height'];
299 if (isset($config['staticSize'])) {
300 $config['staticMap']['size'] = $config['staticSize'];
302 // the following are convenience defaults - if not available the map lat/lng/zoom defaults will be used
303 if (isset($config['staticZoom'])) {
304 $config['staticMap']['zoom'] = $config['staticZoom'];
306 if (isset($config['staticLat'])) {
307 $config['staticMap']['lat'] = $config['staticLat'];
309 if (isset($config['staticLng'])) {
310 $config['staticMap']['lng'] = $config['staticLng'];
312 if (isset($config['localImages'])) {
313 if ($config['localImages'] === true) {
314 $config['localImages'] = Router::url('/img/google_map/', true);
319 if (!empty($config['inline'])) {
320 trigger_error('Deprecated inline option, use block instead.', E_USER_DEPRECATED);
321 $config['block'] = null;
324 $this->_config = $config;
325 $this->_runtimeConfig = $this->_config;
329 * JS maps.google API url.
331 * Options read via configs
334 * - language (iso2: en, de, ja, ...)
336 * You can adds more after the URL like "&key=value&..." via
337 * - query string array: additional query strings (e.g. callback for deferred execution - not supported yet by this helper)
339 * @param array $query
340 * @return string Full URL
342 public function apiUrl(array $query = []) {
343 $url = $this->_protocol() . static::API;
345 if ($this->_runtimeConfig['map']['api']) {
346 $query['v'] = $this->_runtimeConfig['map']['api'];
348 if ($this->_runtimeConfig['key']) {
349 $query['key'] = $this->_runtimeConfig['key'];
352 if ($this->_runtimeConfig['language']) {
353 $query['language'] = $this->_runtimeConfig['language'];
357 $query = http_build_query($query);
359 $url .= '?' . $query;
369 public function gearsUrl() {
370 $this->_gearsIncluded = true;
371 $url = $this->_protocol() . 'code.google.com/apis/gears/gears_init.js';
376 * @return string currentMapObject
378 public function name() {
379 return 'map' . static::$mapCount;
383 * @return string currentContainerId
385 public function id() {
386 return $this->_runtimeConfig['div']['id'];
390 * Make it possible to include multiple maps per page
391 * resets markers, infoWindows etc
393 * @param bool $full true=optionsAsWell
396 public function reset($full = true) {
397 static::$markerCount = static::$infoWindowCount = 0;
398 $this->markers = $this->infoWindows = [];
400 $this->_runtimeConfig = $this->_config;
405 * Set the controls of current map
408 * - zoom, scale, overview: TRUE/FALSE
410 * - map: FALSE, small, large
411 * - type: FALSE, normal, menu, hierarchical
412 * TIP: faster/shorter by using only the first character (e.g. "H" for "hierarchical")
414 * @param array $options
417 public function setControls(array $options = []) {
418 if (isset($options['streetView'])) {
419 $this->_runtimeConfig['map']['streetViewControl'] = $options['streetView'];
421 if (isset($options['zoom'])) {
422 $this->_runtimeConfig['map']['scaleControl'] = $options['zoom'];
424 if (isset($options['scrollwheel'])) {
425 $this->_runtimeConfig['map']['scrollwheel'] = $options['scrollwheel'];
427 if (isset($options['keyboardShortcuts'])) {
428 $this->_runtimeConfig['map']['keyboardShortcuts'] = $options['keyboardShortcuts'];
430 if (isset($options['type'])) {
431 $this->_runtimeConfig['map']['type'] = $options['type'];
436 * This the initialization point of the script
437 * Returns the div container you can echo on the website
439 * @param array $options associative array of settings are passed
440 * @return string divContainer
442 public function map(array $options = []) {
444 $this->_runtimeConfig = Hash::merge($this->_runtimeConfig, $options);
445 $this->_runtimeConfig['map'] = $options + $this->_runtimeConfig['map'];
447 if (!isset($this->_runtimeConfig['map']['lat']) || !isset($this->_runtimeConfig['map']['lng'])) {
448 $this->_runtimeConfig['map']['lat'] = $this->_runtimeConfig['map']['defaultLat'];
449 $this->_runtimeConfig['map']['lng'] = $this->_runtimeConfig['map']['defaultLng'];
451 if (!isset($this->_runtimeConfig['map']['zoom'])) {
452 $this->_runtimeConfig['map']['zoom'] = $this->_runtimeConfig['map']['defaultZoom'];
458 if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) {
459 $res = $this->Html->script($this->apiUrl(), ['block' => $this->_runtimeConfig['block']]);
460 $this->_apiIncluded = true;
462 if (!$this->_runtimeConfig['block']) {
463 $result .= $res . PHP_EOL;
465 // usually already included
466 //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
468 // still not very common: http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/basics.html
469 if (false && !empty($this->_runtimeConfig['autoScript']) && !$this->_gearsIncluded) {
470 $res = $this->Html->script($this->gearsUrl(), ['block' => $this->_runtimeConfig['block']]);
471 if (!$this->_runtimeConfig['block']) {
472 $result .= $res . PHP_EOL;
477 var initialLocation = " . $this->_initialLocation() . ";
478 var browserSupportFlag = new Boolean();
479 var myOptions = " . $this->_mapOptions() . ";
482 gMarkers" . static::$mapCount . " = new Array();
483 gInfoWindows" . static::$mapCount . " = new Array();
484 gWindowContents" . static::$mapCount . " = new Array();
487 #rename "map_canvas" to "map_canvas1", ... if multiple maps on one page
488 while (in_array($this->_runtimeConfig['div']['id'], $this->_mapIds)) {
489 $this->_runtimeConfig['div']['id'] .= '-1'; //TODO: improve
491 $this->_mapIds[] = $this->_runtimeConfig['div']['id'];
494 var " . $this->name() . ' = new google.maps.Map(document.getElementById("' . $this->_runtimeConfig['div']['id'] . "\"), myOptions);
498 $this->_runtimeConfig['div']['style'] = '';
499 if (is_numeric($this->_runtimeConfig['div']['width'])) {
500 $this->_runtimeConfig['div']['width'] .= 'px';
502 if (is_numeric($this->_runtimeConfig['div']['height'])) {
503 $this->_runtimeConfig['div']['height'] .= 'px';
506 $this->_runtimeConfig['div']['style'] .= 'width: ' . $this->_runtimeConfig['div']['width'] . ';';
507 $this->_runtimeConfig['div']['style'] .= 'height: ' . $this->_runtimeConfig['div']['height'] . ';';
508 unset($this->_runtimeConfig['div']['width']);
509 unset($this->_runtimeConfig['div']['height']);
511 $defaultText = isset($this->_runtimeConfig['content']) ? $this->_runtimeConfig['content'] : __('Map cannot be displayed!');
512 $result .= $this->Html->tag('div', $defaultText, $this->_runtimeConfig['div']);
518 * Generate a new LatLng object with the current lat and lng.
522 protected function _initialLocation() {
523 if ($this->_runtimeConfig['map']['lat'] && $this->_runtimeConfig['map']['lng']) {
524 return 'new google.maps.LatLng(' . $this->_runtimeConfig['map']['lat'] . ', ' . $this->_runtimeConfig['map']['lng'] . ')';
526 $this->_runtimeConfig['autoCenter'] = true;
531 * Add a marker to the map.
534 * - lat and lng or address (to geocode on demand, not recommended, though)
535 * - title, content, icon, directions, maxWidth, open (optional)
537 * Note, that you can only set one marker to "open" for single window mode.
538 * If you declare multiple ones, the last one will be the one shown as open.
540 * @param array $options
541 * @return mixed Integer marker count or boolean false on failure
542 * @throws \Cake\Core\Exception\Exception
544 public function addMarker($options) {
545 $defaults = $this->_runtimeConfig['marker'];
546 if (isset($options['icon']) && is_array($options['icon'])) {
547 $defaults = $options['icon'] + $defaults;
548 unset($options['icon']);
550 $options += $defaults;
553 $params['map'] = $this->name();
555 if (isset($options['title'])) {
556 $params['title'] = json_encode($options['title']);
558 if (isset($options['icon'])) {
559 $params['icon'] = $options['icon'];
560 if (is_int($params['icon'])) {
561 $params['icon'] = 'gIcons' . static::$mapCount . '[' . $params['icon'] . ']';
563 $params['icon'] = json_encode($params['icon']);
566 if (isset($options['shadow'])) {
567 $params['shadow'] = $options['shadow'];
568 if (is_int($params['shadow'])) {
569 $params['shadow'] = 'gIcons' . static::$mapCount . '[' . $params['shadow'] . ']';
571 $params['shadow'] = json_encode($params['shadow']);
574 if (isset($options['shape'])) {
575 $params['shape'] = $options['shape'];
577 if (isset($options['zIndex'])) {
578 $params['zIndex'] = $options['zIndex'];
580 if (isset($options['animation'])) {
581 $params['animation'] = 'google.maps.Animation.' . strtoupper($options['animation']);
584 // geocode if necessary
585 if (!isset($options['lat']) || !isset($options['lng'])) {
587 var geocoder = new google.maps.Geocoder();
589 function geocodeAddress(address) {
590 geocoder.geocode({'address': address}, function(results, status) {
591 if (status == google.maps.GeocoderStatus.OK) {
593 x" . static::$markerCount . " = new google.maps.Marker({
594 position: results[0].geometry.location,
595 " . $this->_toObjectParams($params, false, false) . "
597 gMarkers" . static::$mapCount . " .push(
598 x" . static::$markerCount . "
600 return results[0].geometry.location;
602 //alert('Geocoding was not successful for the following reason: ' + status);
607 if (!isset($options['address'])) {
608 throw new Exception('Either use lat/lng or address to add a marker');
610 $position = 'geocodeAddress("' . h($options['address']) . '")';
612 $position = 'new google.maps.LatLng(' . $options['lat'] . ',' . $options['lng'] . ')';
616 var x" . static::$markerCount . " = new google.maps.Marker({
617 position: " . $position . ",
618 " . $this->_toObjectParams($params, false, false) . "
620 gMarkers" . static::$mapCount . " .push(
621 x" . static::$markerCount . "
624 $this->map .= $marker;
626 if (!empty($options['directions'])) {
627 $options['content'] .= $this->_directions($options['directions'], $options);
630 // Fill popup windows
631 if (!empty($options['content']) && $this->_runtimeConfig['infoWindow']['useMultiple']) {
632 $x = $this->addInfoWindow(['content' => $options['content']]);
633 $this->addEvent(static::$markerCount, $x, $options['open']);
635 } elseif (!empty($options['content'])) {
636 if (!isset($this->_runtimeConfig['marker']['infoWindow'])) {
637 $this->_runtimeConfig['marker']['infoWindow'] = $this->addInfoWindow();
640 $x = $this->addInfoContent($options['content']);
642 gInfoWindows" . static::$mapCount . '[' . $this->_runtimeConfig['marker']['infoWindow'] . ']. setContent(gWindowContents' . static::$mapCount . '[' . $x . "]);
643 gInfoWindows" . static::$mapCount . '[' . $this->_runtimeConfig['marker']['infoWindow'] . '].open(' . $this->name() . ', gMarkers' . static::$mapCount . '[' . $x . "]);
645 $this->addCustomEvent(static::$markerCount, $event);
647 if (!empty($options['open'])) {
648 $this->addCustom($event);
652 // Custom matching event?
653 if (isset($options['id'])) {
654 $this->matching[$options['id']] = static::$markerCount;
657 return static::$markerCount++;
661 * Build directions form (type get) for directions inside infoWindows
663 * Options for directions (if array)
666 * - escape: defaults to true
668 * @param mixed $directions
669 * - bool TRUE for autoDirections (using lat/lng)
670 * @param array $markerOptions
671 * - options array of marker for autoDirections etc (optional)
672 * @return string HTML
674 protected function _directions($directions, array $markerOptions = []) {
678 'label' => __('Enter your address'),
679 'submit' => __('Get directions'),
681 'zoom' => null, // auto
683 if ($directions === true) {
684 $options['to'] = $markerOptions['lat'] . ',' . $markerOptions['lng'];
685 } elseif (is_array($directions)) {
686 $options = $directions + $options;
688 if (empty($options['to']) && empty($options['from'])) {
691 $form = '<form action="http://maps.google.com/maps" method="get" target="_blank">';
692 $form .= $options['escape'] ? h($options['label']) : $options['label'];
693 if (!empty($options['from'])) {
694 $form .= '<input type="hidden" name="saddr" value="' . $options['from'] . '" />';
696 $form .= '<input type="text" name="saddr" />';
698 if (!empty($options['to'])) {
699 $form .= '<input type="hidden" name="daddr" value="' . $options['to'] . '" />';
701 $form .= '<input type="text" name="daddr" />';
703 if (isset($options['zoom'])) {
704 $form .= '<input type="hidden" name="z" value="' . $options['zoom'] . '" />';
706 $form .= '<input type="submit" value="' . $options['submit'] . '" />';
709 return '<div class="directions">' . $form . '</div>';
713 * @param string $content
714 * @return int Current marker counter
716 public function addInfoContent($content) {
717 $this->infoContents[static::$markerCount] = $this->escapeString($content);
719 gWindowContents" . static::$mapCount . '.push(' . $this->escapeString($content) . ");
721 $this->addCustom($event);
724 return static::$markerCount;
731 'color' => 'http://www.google.com/mapfiles/marker%s.png',
732 'alpha' => 'http://www.google.com/mapfiles/marker%s%s.png',
733 'numeric' => 'http://google-maps-icons.googlecode.com/files/%s%s.png',
734 'special' => 'http://google-maps-icons.googlecode.com/files/%s.png'
738 * Get a custom icon set
740 * @param string $color Color: green, red, purple, ... or some special ones like "home", ...
741 * @param string|null $char Char: A...Z or 0...20/100 (defaults to none)
742 * @param string $size Size: s, m, l (defaults to medium)
743 * NOTE: for special ones only first parameter counts!
744 * @return array Array(icon, shadow, shape, ...)
746 public function iconSet($color, $char = null, $size = 'm') {
747 $colors = ['red', 'green', 'yellow', 'blue', 'purple', 'white', 'black'];
748 if (!in_array($color, $colors)) {
752 if (!empty($this->_runtimeConfig['localImages'])) {
753 $this->setIcons['color'] = $this->_runtimeConfig['localImages'] . 'marker%s.png';
754 $this->setIcons['alpha'] = $this->_runtimeConfig['localImages'] . 'marker%s%s.png';
755 $this->setIcons['numeric'] = $this->_runtimeConfig['localImages'] . '%s%s.png';
756 $this->setIcons['special'] = $this->_runtimeConfig['localImages'] . '%s.png';
760 if ($color === 'red') {
763 $color = '_' . $color;
765 $url = sprintf($this->setIcons['alpha'], $color, $char);
767 if ($color === 'red') {
770 $color = '_' . $color;
772 $url = sprintf($this->setIcons['color'], $color);
776 var iconImage = new google.maps.MarkerImage('images/' + images[0] + ' .png',
777 new google.maps.Size(iconData[images[0]].width, iconData[images[0]].height),
778 new google.maps.Point(0,0),
779 new google.maps.Point(0, 32)
782 var iconShadow = new google.maps.MarkerImage('images/' + images[1] + ' .png',
783 new google.maps.Size(iconData[images[1]].width, iconData[images[1]].height),
784 new google.maps.Point(0,0),
785 new google.maps.Point(0, 32)
789 coord: [1, 1, 1, 32, 32, 32, 32, 1],
794 $shadow = 'http://www.google.com/mapfiles/shadow50.png';
797 'icon' => $this->icon($url, ['size' => ['width' => 20, 'height' => 34]]),
798 'shadow' => $this->icon($shadow, ['size' => ['width' => 37, 'height' => 34], 'shadow' => ['width' => 10, 'height' => 34]])
804 * Generate icon array.
806 * custom icon: http://thydzik.com/thydzikGoogleMap/markerlink.php?text=?&color=FFFFFF
807 * custom icons: http://code.google.com/p/google-maps-icons/wiki/NumericIcons#Lettered_Balloons_from_A_to_Z,_in_10_Colors
808 * custom shadows: http://www.cycloloco.com/shadowmaker/shadowmaker.htm
810 * @param string $image Image Url (http://...)
811 * @param string|null $shadow ShadowImage Url (http://...)
812 * @param array $imageOptions Image options
813 * @param array $shadowOptions Shadow image options
814 * @return array Resulting array
816 public function addIcon($image, $shadow = null, array $imageOptions = [], array $shadowOptions = []) {
817 $res = ['url' => $image];
818 $res['icon'] = $this->icon($image, $imageOptions);
820 $last = $this->_iconRemember[$res['icon']];
821 if (!isset($shadowOptions['anchor'])) {
822 $shadowOptions['anchor'] = [];
824 $shadowOptions['anchor'] = $last['options']['anchor'] + $shadowOptions['anchor'];
826 $res['shadow'] = $this->icon($shadow, $shadowOptions);
834 protected $_iconRemember = [];
837 * Generate icon object
839 * @param string $url (required)
840 * @param array $options (optional):
841 * - size: array(width=>x, height=>y)
842 * - origin: array(width=>x, height=>y)
843 * - anchor: array(width=>x, height=>y)
844 * @return int Icon count
846 public function icon($url, array $options = []) {
847 // The shadow image is larger in the horizontal dimension
848 // while the position and offset are the same as for the main image.
849 if (empty($options['size'])) {
850 if (substr($url, 0, 1) === '/') {
851 // patch local paths to use the document root. otherwise getimagesize fails filesystem lookup.
852 // paths with http or other protocol in front will be handled more simply in 'else' below.
853 $canonicalPath = realpath(WWW_ROOT . $url);
854 if (! $canonicalPath) {
855 // failed to resolve the path, so just fall back to the url provided.
856 $canonicalPath = "$url";
858 $data = getimagesize($canonicalPath);
860 $data = getimagesize($url);
863 $options['size']['width'] = $data[0];
864 $options['size']['height'] = $data[1];
866 $options['size']['width'] = $options['size']['height'] = 0;
869 if (empty($options['anchor'])) {
870 $options['anchor']['width'] = (int)($options['size']['width'] / 2);
871 $options['anchor']['height'] = $options['size']['height'];
873 if (empty($options['origin'])) {
874 $options['origin']['width'] = $options['origin']['height'] = 0;
876 if (isset($options['shadow'])) {
877 $options['anchor'] = $options['shadow'];
880 $icon = 'new google.maps.MarkerImage("' . $url . '",
881 new google.maps.Size(' . $options['size']['width'] . ', ' . $options['size']['height'] . '),
882 new google.maps.Point(' . $options['origin']['width'] . ', ' . $options['origin']['height'] . '),
883 new google.maps.Point(' . $options['anchor']['width'] . ', ' . $options['anchor']['height'] . ')
885 $this->icons[static::$iconCount] = $icon;
886 $this->_iconRemember[static::$iconCount] = ['url' => $url, 'options' => $options, 'id' => static::$iconCount];
887 return static::$iconCount++;
891 * Creates a new InfoWindow.
893 * @param array $options
894 * - lat, lng, content, maxWidth, pixelOffset, zIndex
895 * @return int windowCount
897 public function addInfoWindow(array $options = []) {
898 $defaults = $this->_runtimeConfig['infoWindow'];
899 $options += $defaults;
901 if (!empty($options['lat']) && !empty($options['lng'])) {
902 $position = 'new google.maps.LatLng(' . $options['lat'] . ', ' . $options['lng'] . ')';
904 $position = ' ' . $this->name() . ' .getCenter()';
908 gInfoWindows" . static::$mapCount . ".push(new google.maps.InfoWindow({
909 position: {$position},
910 content: " . $this->escapeString($options['content']) . ",
911 maxWidth: {$options['maxWidth']},
912 pixelOffset: {$options['pixelOffset']}
913 /*zIndex: {$options['zIndex']},*/
916 $this->map .= $windows;
917 return static::$infoWindowCount++;
921 * Add event to open marker on click.
924 * @param int $infoWindow
925 * @param bool $open Also open it right away.
928 public function addEvent($marker, $infoWindow, $open = false) {
930 google.maps.event.addListener(gMarkers" . static::$mapCount . "[{$marker}], 'click', function() {
931 gInfoWindows" . static::$mapCount . "[$infoWindow].open(" . $this->name() . ", this);
935 $event = 'gInfoWindows' . static::$mapCount . "[$infoWindow].open(" . $this->name() .
936 ', gMarkers' . static::$mapCount . '[' . $marker . ']);';
937 $this->addCustom($event);
942 * Add a custom event for a marker on click.
945 * @param string $event (js)
948 public function addCustomEvent($marker, $event) {
950 google.maps.event.addListener(gMarkers" . static::$mapCount . "[{$marker}], 'click', function() {
959 * @param string $js Custom JS
962 public function addCustom($js) {
967 * Add directions to the map.
969 * @param array|string $from Location as array(fixed lat/lng pair) or string (to be geocoded at runtime)
970 * @param array|string $to Location as array(fixed lat/lng pair) or string (to be geocoded at runtime)
971 * @param array $options
972 * - directionsDiv: Div to place directions in text form
973 * - travelMode: TravelMode,
974 * - transitOptions: TransitOptions,
975 * - unitSystem: UnitSystem (IMPERIAL, METRIC, AUTO),
976 * - waypoints[]: DirectionsWaypoint,
977 * - optimizeWaypoints: Boolean,
978 * - provideRouteAlternatives: Boolean,
979 * - avoidHighways: Boolean,
980 * - avoidTolls: Boolean
982 * @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsRequest
985 public function addDirections($from, $to, array $options = []) {
986 $id = 'd' . static::$markerCount++;
987 $defaults = $this->_runtimeConfig['directions'];
988 $options += $defaults;
989 $travelMode = $this->travelModes[$options['travelMode']];
992 var {$id}Service = new google.maps.DirectionsService();
994 {$id}Display = new google.maps.DirectionsRenderer();
995 {$id}Display. setMap(" . $this->name() . ");
998 if (!empty($options['directionsDiv'])) {
999 $directions .= "{$id}Display. setPanel(document.getElementById('" . $options['directionsDiv'] . "'));";
1002 if (is_array($from)) {
1003 $from = 'new google.maps.LatLng(' . (float)$from['lat'] . ', ' . (float)$from['lng'] . ')';
1005 $from = '"' . h($from) . '"';
1007 if (is_array($to)) {
1008 $to = 'new google.maps.LatLng(' . (float)$to['lat'] . ', ' . (float)$to['lng'] . ')';
1010 $to = '"' . h($to) . '"';
1017 unitSystem: google.maps.UnitSystem." . $options['unitSystem'] . ",
1018 travelMode: google.maps.TravelMode. $travelMode
1020 {$id}Service.route(request, function(result, status) {
1021 if (status == google.maps.DirectionsStatus.OK) {
1022 {$id}Display. setDirections(result);
1026 $this->map .= $directions;
1032 * This method adds a line between 2 points
1034 * @param array|string $from Location as array(fixed lat/lng pair) or string (to be geocoded at runtime)
1035 * @param array|string $to Location as array(fixed lat/lng pair) or string (to be geocoded at runtime)
1036 * @param array $options
1037 * - color (#FFFFFF ... #000000)
1038 * - opacity (0.1 ... 1, defaults to 1)
1039 * - weight in pixels (defaults to 2)
1040 * @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#Polyline
1043 public function addPolyline($from, $to, array $options = []) {
1044 if (is_array($from)) {
1045 $from = 'new google.maps.LatLng(' . (float)$from['lat'] . ', ' . (float)$from['lng'] . ')';
1047 throw new Exception('not implemented yet, use array of lat/lng');
1048 //$from = '\'' . h($from) . '\'';
1050 if (is_array($to)) {
1051 $to = 'new google.maps.LatLng(' . (float)$to['lat'] . ', ' . (float)$to['lng'] . ')';
1053 throw new Exception('not implemented yet, use array of lat/lng');
1054 //$to = '\'' . h($to) . '\'';
1057 $defaults = $this->_runtimeConfig['polyline'];
1058 $options += $defaults;
1060 $id = 'p' . static::$markerCount++;
1062 $polyline = "var start = $from;";
1063 $polyline .= "var end = $to;";
1069 var {$id}Polyline = new google.maps.Polyline({
1071 strokeColor: '" . $options['color'] . "',
1072 strokeOpacity: " . $options['opacity'] . ",
1073 strokeWeight: " . $options['weight'] . "
1075 {$id}Polyline.setMap(" . $this->name() . ");
1077 $this->map .= $polyline;
1081 * @param string $content (html/text)
1082 * @param int $index infoWindowCount
1085 public function setContentInfoWindow($content, $index) {
1087 gInfoWindows" . static::$mapCount . "[$index]. setContent(" . $this->escapeString($content) . ');';
1091 * Json encode string
1093 * @param mixed $content
1094 * @return string JSON
1096 public function escapeString($content) {
1097 return json_encode($content);
1101 * This method returns the javascript for the current map container.
1102 * Including script tags.
1103 * Just echo it below the map container. New: Alternativly, use finalize() directly.
1107 public function script() {
1109 ' . $this->finalize(true) . '
1115 * Finalize the map and write the javascript to the buffer.
1116 * Make sure that your view does also output the buffer at some place!
1118 * @param bool $return If the output should be returned instead
1119 * @return null|string Javascript if $return is true
1121 public function finalize($return = false) {
1122 $script = $this->_arrayToObject('matching', $this->matching, false, true) . PHP_EOL;
1123 $script .= $this->_arrayToObject('gIcons' . static::$mapCount, $this->icons, false, false) . '
1125 jQuery(document).ready(function() {
1128 $script .= $this->map;
1129 if ($this->_runtimeConfig['geolocate']) {
1130 $script .= $this->_geolocate();
1133 if ($this->_runtimeConfig['showMarker'] && !empty($this->markers) && is_array($this->markers)) {
1134 $script .= implode($this->markers, ' ');
1137 if ($this->_runtimeConfig['autoCenter']) {
1138 $script .= $this->_autoCenter();
1143 static::$mapCount++;
1147 $this->Html->scriptBlock($script, ['block' => true]);
1151 * Set a custom geolocate callback
1153 * @param string|bool $js Custom JS
1154 * false: no callback at all
1157 public function geolocateCallback($js) {
1158 if ($js === false) {
1159 $this->_runtimeConfig['callbacks']['geolocate'] = false;
1162 $this->_runtimeConfig['callbacks']['geolocate'] = $js;
1166 * Experimental - works in cutting edge browsers like chrome10
1170 protected function _geolocate() {
1172 // Try W3C Geolocation (Preferred)
1173 if (navigator.geolocation) {
1174 browserSupportFlag = true;
1175 navigator.geolocation.getCurrentPosition(function(position) {
1176 geolocationCallback(position.coords.latitude, position.coords.longitude);
1178 handleNoGeolocation(browserSupportFlag);
1180 // Try Google Gears Geolocation
1181 } else if (google.gears) {
1182 browserSupportFlag = true;
1183 var geo = google.gears.factory.create("beta.geolocation");
1184 geo.getCurrentPosition(function(position) {
1185 geolocationCallback(position.latitude, position.longitude);
1187 handleNoGeoLocation(browserSupportFlag);
1189 // Browser doesn\'t support Geolocation
1191 browserSupportFlag = false;
1192 handleNoGeolocation(browserSupportFlag);
1195 function geolocationCallback(lat, lng) {
1196 ' . $this->_geolocationCallback() . '
1199 function handleNoGeolocation(errorFlag) {
1200 if (errorFlag == true) {
1201 //alert("Geolocation service failed.");
1203 //alert("Your browser doesn\'t support geolocation. We\'ve placed you in Siberia.");
1205 //' . $this->name() . ' . setCenter(initialLocation);
1213 protected function _geolocationCallback() {
1214 if (($js = $this->_runtimeConfig['callbacks']['geolocate']) === false) {
1218 $js = 'initialLocation = new google.maps.LatLng(lat, lng);
1219 ' . $this->name() . ' . setCenter(initialLocation);
1227 * careful: with only one marker this can result in too high zoom values!
1229 * @return string autoCenterCommands
1231 protected function _autoCenter() {
1233 var bounds = new google.maps.LatLngBounds();
1234 $.each(gMarkers' . static::$mapCount . ',function (index, marker) { bounds.extend(marker.position);});
1235 ' . $this->name() . ' .fitBounds(bounds);
1240 * @return string JSON like js string
1242 protected function _mapOptions() {
1243 $options = $this->_runtimeConfig['map'] + $this->_runtimeConfig;
1245 $mapOptions = array_intersect_key($options, [
1246 'streetViewControl' => null,
1247 'navigationControl' => null,
1248 'mapTypeControl' => null,
1249 'scaleControl' => null,
1250 'scrollwheel' => null,
1252 'keyboardShortcuts' => null,
1256 foreach ($mapOptions as $key => $mapOption) {
1257 $res[] = $key . ': ' . $this->value($mapOption);
1259 if (empty($options['autoCenter'])) {
1260 $res[] = 'center: initialLocation';
1262 if (!empty($options['navOptions'])) {
1263 $res[] = 'navigationControlOptions: ' . $this->_controlOptions('nav', $options['navOptions']);
1265 if (!empty($options['typeOptions'])) {
1266 $res[] = 'mapTypeControlOptions: ' . $this->_controlOptions('type', $options['typeOptions']);
1268 if (!empty($options['scaleOptions'])) {
1269 $res[] = 'scaleControlOptions: ' . $this->_controlOptions('scale', $options['scaleOptions']);
1272 if (array_key_exists($options['type'], $this->types)) {
1273 $type = $this->types[$options['type']];
1275 $type = $options['type'];
1277 $res[] = 'mapTypeId: google.maps.MapTypeId.' . $type;
1279 return '{' . implode(', ', $res) . '}';
1283 * @param string $type
1284 * @param array $options
1285 * @return string JSON like js string
1287 protected function _controlOptions($type, $options) {
1289 'nav' => 'NavigationControlStyle',
1290 'type' => 'MapTypeControlStyle',
1294 if (!empty($options['style']) && ($m = $mapping[$type])) {
1295 $res[] = 'style: google.maps.' . $m . '.' . $options['style'];
1297 if (!empty($options['pos'])) {
1298 $res[] = 'position: google.maps.ControlPosition.' . $options['pos'];
1301 return '{' . implode(', ', $res) . '}';
1305 * Returns a maps.google link
1307 * @param string $title Link title
1308 * @param array $mapOptions
1309 * @param array $linkOptions
1310 * @return string HTML link
1312 public function mapLink($title, $mapOptions = [], $linkOptions = []) {
1313 return $this->Html->link($title, $this->mapUrl($mapOptions + ['escape' => false]), $linkOptions);
1317 * Returns a maps.google url
1320 * - from: necessary (address or lat,lng)
1321 * - to: 1x necessary (address or lat,lng - can be an array of multiple destinations: array('dest1', 'dest2'))
1322 * - zoom: optional (defaults to none)
1323 * - query: Additional query strings as array
1324 * - escape: defaults to true
1326 * @param array $options Options
1327 * @return string link: http://...
1329 public function mapUrl(array $options = []) {
1330 $url = $this->_protocol() . 'maps.google.com/maps?';
1332 $urlArray = !empty($options['query']) ? $options['query'] : [];
1333 if (!empty($options['from'])) {
1334 $urlArray['saddr'] = $options['from'];
1337 if (!empty($options['to']) && is_array($options['to'])) {
1338 $to = array_shift($options['to']);
1339 foreach ($options['to'] as $key => $value) {
1340 $to .= '+to:' . $value;
1342 $urlArray['daddr'] = $to;
1343 } elseif (!empty($options['to'])) {
1344 $urlArray['daddr'] = $options['to'];
1347 if (isset($options['zoom']) && $options['zoom'] !== false) {
1348 $urlArray['z'] = (int)$options['zoom'];
1350 //$urlArray[] = 'f=d';
1351 //$urlArray[] = 'hl=de';
1352 //$urlArray[] = 'ie=UTF8';
1358 $query = http_build_query($urlArray);
1359 if ($options['escape']) {
1363 return $url . $query;
1367 * Creates a plain image map.
1369 * @link http://code.google.com/intl/de-DE/apis/maps/documentation/staticmaps
1370 * @param array $options Options
1371 * - string $size [necessary: VALxVAL, e.g. 500x400 - max 640x640]
1372 * - string $center: x,y or address [necessary, if no markers are given; else tries to take defaults if available] or TRUE/FALSE
1373 * - int $zoom [optional; if no markers are given, default value is used; if set to "auto" and ]*
1374 * - array $markers [optional, @see staticPaths() method]
1375 * - string $type [optional: roadmap/hybrid, ...; default:roadmap]
1376 * - string $mobile TRUE/FALSE
1377 * - string $visible: $area (x|y|...)
1378 * - array $paths [optional, @see staticPaths() method]
1379 * - string $language [optional]
1380 * @param array $attributes HTML attributes for the image
1382 * - alt (defaults to 'Map')
1383 * - url (tip: you can pass $this->link(...) and it will create a link to maps.google.com)
1384 * @return string imageTag
1386 public function staticMap(array $options = [], array $attributes = []) {
1387 $defaultAttributes = ['alt' => __d('tools', 'Map')];
1388 $attributes += $defaultAttributes;
1390 // This was fixed in 3.5.1 to auto-escape URL query strings for security reasons
1391 $escape = version_compare(Configure::version(), '3.5.1') < 0 ? true : false;
1392 return $this->Html->image($this->staticMapUrl($options + ['escape' => $escape]), $attributes);
1396 * Create a link to a plain image map
1398 * @param string $title Link title
1399 * @param array $mapOptions
1400 * @param array $linkOptions
1401 * @return string HTML link
1403 public function staticMapLink($title, array $mapOptions = [], array $linkOptions = []) {
1404 return $this->Html->link($title, $this->staticMapUrl($mapOptions + ['escape' => false]), $linkOptions);
1408 * Creates a URL to a plain image map.
1411 * - escape: defaults to true (Deprecated as of CakePHP 3.5.1 and now has to be always false)
1413 * @param array $options
1414 * - see staticMap() for details
1415 * @return string urlOfImage: http://...
1417 public function staticMapUrl(array $options = []) {
1418 $mapUrl = $this->_protocol() . static::STATIC_API;
1421 'mobile' => 'false',
1426 if (!empty($options['mobile'])) {
1427 $params['mobile'] = 'true';
1431 $defaults = $this->_config['staticMap'] + $this->_config;
1433 $mapOptions = $options + $defaults;
1435 $params = array_intersect_key($mapOptions, [
1442 //'visible' => null,
1446 // add API key to parameters.
1447 if ($this->_runtimeConfig['key']) {
1448 $params['key'] = $this->_runtimeConfig['key'];
1451 // do we want zoom to auto-correct itself?
1452 if (!isset($options['zoom']) && !empty($mapOptions['markers']) || !empty($mapOptions['paths']) || !empty($mapOptions['visible'])) {
1453 $options['zoom'] = 'auto';
1456 // a position on the map that is supposed to stay visible at all cost
1457 if (!empty($mapOptions['visible'])) {
1458 $params['visible'] = urlencode($mapOptions['visible']);
1461 // center and zoom are not necessary if path, visible or markers are given
1462 if (!isset($options['center']) || $options['center'] === false) {
1464 } elseif ($options['center'] === true && $mapOptions['lat'] !== null && $mapOptions['lng'] !== null) {
1465 $params['center'] = urlencode((string)$mapOptions['lat'] . ',' . (string)$mapOptions['lng']);
1466 } elseif (!empty($options['center'])) {
1467 $params['center'] = urlencode($options['center']);
1469 // try to read from markers array???
1470 if (isset($options['markers']) && count($options['markers']) == 1) {
1471 //pr ($options['markers']);
1475 if (!isset($options['zoom']) || $options['zoom'] === false) {
1478 if ($options['zoom'] === 'auto') {
1479 if (!empty($options['markers']) && strpos($options['zoom'], '|') !== false) {
1480 // let google find the best zoom value itself
1482 // do something here?
1485 $params['zoom'] = $options['zoom'];
1489 if (array_key_exists($mapOptions['type'], $this->types)) {
1490 $params['maptype'] = $this->types[$mapOptions['type']];
1492 $params['maptype'] = $mapOptions['type'];
1494 $params['maptype'] = strtolower($params['maptype']);
1496 // old: {latitude},{longitude},{color}{alpha-character}
1497 // new: @see staticMarkers()
1498 if (!empty($options['markers'])) {
1499 $params['markers'] = $options['markers'];
1502 if (!empty($options['paths'])) {
1503 $params['path'] = $options['paths'];
1507 if (!empty($options['size'])) {
1508 $params['size'] = $options['size'];
1512 foreach ($params as $key => $value) {
1513 if (is_array($value)) {
1514 $value = implode('&' . $key . '=', $value);
1515 } elseif ($value === true) {
1517 } elseif ($value === false) {
1519 } elseif ($value === null) {
1522 $pieces[] = $key . '=' . $value;
1528 $query = implode('&', $pieces);
1529 if ($options['escape']) {
1533 return $mapUrl . '?' . $query;
1537 * Prepare paths for staticMap
1539 * @param array $pos PathElementArrays
1540 * - elements: [required] (multiple array(lat=>x, lng=>y) or just a address strings)
1541 * - color: red/blue/green (optional, default blue)
1542 * - weight: numeric (optional, default: 5)
1543 * @return array Array of paths: e.g: color:0x0000FF80|weight:5|37.40303,-122.08334|37.39471,-122.07201|37.40589,-122.06171{|...}
1545 public function staticPaths(array $pos = []) {
1548 'weight' => 5 // pixel
1551 // not a 2-level array? make it one
1552 if (!isset($pos[0])) {
1557 foreach ($pos as $p) {
1558 $options = $p + $defaults;
1560 $markers = $options['path'];
1561 unset($options['path']);
1564 if (!empty($options['color'])) {
1565 $options['color'] = $this->_prepColor($options['color']);
1569 foreach ($options as $key => $value) {
1570 $path[] = $key . ':' . urlencode($value);
1572 foreach ($markers as $key => $pos) {
1573 if (is_array($pos)) {
1575 $pos = $pos['lat'] . ',' . $pos['lng'];
1579 $res[] = implode('|', $path);
1585 * Prepare markers for staticMap
1587 * @param array $pos markerArrays
1588 * - lat: xx.xxxxxx (necessary)
1589 * - lng: xx.xxxxxx (necessary)
1590 * - address: (instead of lat/lng)
1591 * - color: red/blue/green (optional, default blue)
1592 * - label: a-z or numbers (optional, default: s)
1593 * - icon: custom icon (png, gif, jpg - max 64x64 - max 5 different icons per image)
1594 * - shadow: TRUE/FALSE
1595 * @param array $style (global) (overridden by custom marker styles)
1600 * @return array markers: color:green|label:Z|48,11|Berlin
1602 * NEW: size:mid|color:red|label:E|37.400465,-122.073003|37.437328,-122.159928&markers=size:small|color:blue|37.369110,-122.096034
1603 * OLD: 40.702147,-74.015794,blueS|40.711614,-74.012318,greenG{|...}
1605 public function staticMarkers(array $pos = [], array $style = []) {
1617 // not a 2-level array? make it one
1618 if (!isset($pos[0])) {
1622 // new in staticV2: separate styles! right now just merged
1623 foreach ($pos as $p) {
1624 $p += $style + $defaults;
1626 // adress or lat/lng?
1627 if (!empty($p['lat']) && !empty($p['lng'])) {
1628 $p['address'] = $p['lat'] . ',' . $p['lng'];
1630 $p['address'] = urlencode($p['address']);
1635 if (!empty($p['color'])) {
1636 $p['color'] = $this->_prepColor($p['color']);
1637 $values[] = 'color:' . $p['color'];
1640 if (!empty($p['label'])) {
1641 $values[] = 'label:' . strtoupper($p['label']);
1643 if (!empty($p['size'])) {
1644 $values[] = 'size:' . $p['size'];
1646 if (!empty($p['shadow'])) {
1647 $values[] = 'shadow:' . $p['shadow'];
1649 if (!empty($p['icon'])) {
1650 $values[] = 'icon:' . urlencode($p['icon']);
1652 $values[] = $p['address'];
1655 $markers[] = implode('|', $values);
1658 //TODO: shortcut? only possible if no custom params!
1662 // long: markers=styles1|address1&markers=styles2|address2&...
1663 // short: markers=styles,address1|address2|address3|...
1669 * Ensure that we stay on the appropriate protocol
1671 * @return string protocol base (including ://)
1673 protected function _protocol() {
1674 $https = $this->_runtimeConfig['https'];
1675 if ($https === null) {
1676 $https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
1678 return ($https ? 'https' : 'http') . '://';
1685 * @param string $color Color: FFFFFF, #FFFFFF, 0xFFFFFF or blue
1686 * @return string Color
1688 protected function _prepColor($color) {
1689 if (strpos($color, '#') !== false) {
1690 return str_replace('#', '0x', $color);
1692 if (is_numeric($color)) {
1693 return '0x' . $color;
1699 * @param string $name
1700 * @param array $array
1701 * @param bool $asString
1702 * @param bool $keyAsString
1705 protected function _arrayToObject($name, $array, $asString = true, $keyAsString = false) {
1706 $res = 'var ' . $name . ' = {' . PHP_EOL;
1707 $res .= $this->_toObjectParams($array, $asString, $keyAsString);
1713 * @param array $array
1714 * @param bool $asString
1715 * @param bool $keyAsString
1718 protected function _toObjectParams($array, $asString = true, $keyAsString = false) {
1720 foreach ($array as $key => $value) {
1721 $e = ($asString && strpos($value, 'new ') !== 0 ? '"' : '');
1722 $ke = ($keyAsString ? '"' : '');
1723 $pieces[] = $ke . $key . $ke . ': ' . $e . $value . $e;
1725 return implode(',' . PHP_EOL, $pieces);