Merge branch 'release-2.140.101'
[feisty_meow.git] / production / sites / cakelampvm.com / goog_maps_helper_mod / GoogleMapHelper.php
1 <?php
2 namespace Geo\View\Helper;
3
4 use Cake\Core\Configure;
5 use Cake\Core\Exception\Exception;
6 use Cake\Routing\Router;
7 use Cake\Utility\Hash;
8 use Cake\View\Helper;
9 use Cake\View\View;
10 use Geo\View\Helper\JsBaseEngineTrait;
11
12 /**
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.
15  *
16  * Capable of resetting itself (full or partly) for multiple maps on a single view.
17  *
18  * CodeAPI: http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/basics.html
19  * Icons/Images: http://gmapicons.googlepages.com/home
20  *
21  * @author Rajib Ahmed
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
26  */
27 class GoogleMapHelper extends Helper {
28
29         use JsBaseEngineTrait;
30
31         const API = 'maps.google.com/maps/api/js';
32
33         const STATIC_API = 'maps.google.com/maps/api/staticmap';
34
35         /**
36          * @var int
37          */
38         public static $mapCount = 0;
39
40         /**
41          * @var int
42          */
43         public static $markerCount = 0;
44
45         /**
46          * @var int
47          */
48         public static $iconCount = 0;
49
50         /**
51          * @var int
52          */
53         public static $infoWindowCount = 0;
54
55         /**
56          * @var int
57          */
58         public static $infoContentCount = 0;
59
60         const TYPE_ROADMAP = 'R';
61
62         const TYPE_HYBRID = 'H';
63
64         const TYPE_SATELLITE = 'S';
65
66         const TYPE_TERRAIN = 'T';
67
68         /**
69          * @var array
70          */
71         public $types = [
72                 self::TYPE_ROADMAP => 'ROADMAP',
73                 self::TYPE_HYBRID => 'HYBRID',
74                 self::TYPE_SATELLITE => 'SATELLITE',
75                 self::TYPE_TERRAIN => 'TERRAIN'
76         ];
77
78         const TRAVEL_MODE_DRIVING = 'D';
79
80         const TRAVEL_MODE_BICYCLING = 'B';
81
82         const TRAVEL_MODE_TRANSIT = 'T';
83
84         const TRAVEL_MODE_WALKING = 'W';
85
86         /**
87          * @var array
88          */
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'
94         ];
95
96         /**
97          * Needed helpers
98          *
99          * @var array
100          */
101         public $helpers = ['Html'];
102
103         /**
104          * Google maker config instance variable
105          *
106          * @var array
107          */
108         public $markers = [];
109
110         /**
111          * @var array
112          */
113         public $infoWindows = [];
114
115         /**
116          * @var array
117          */
118         public $infoContents = [];
119
120         /**
121          * @var array
122          */
123         public $icons = [];
124
125         /**
126          * @var array
127          */
128         public $matching = [];
129
130         /**
131          * @var string
132          */
133         public $map = '';
134
135         /**
136          * @var array
137          */
138         protected $_mapIds = []; // Remember already used ones (valid xhtml contains ids not more than once)
139
140         /**
141          * Default settings
142          *
143          * @var array
144          */
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
149                 'api' => '3',
150                 'type' => self::TYPE_ROADMAP,
151                 'map' => [
152                         'api' => null,
153                         'zoom' => null,
154                         'lat' => null,
155                         'lng' => null,
156                         'type' => null,
157                         'streetViewControl' => false,
158                         'navigationControl' => true,
159                         'mapTypeControl' => true,
160                         'scaleControl' => true,
161                         'scrollwheel' => false,
162                         'keyboardShortcuts' => true,
163                         'typeOptions' => [],
164                         'navOptions' => [],
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
168                         'defaultZoom' => 5,
169                 ],
170                 'staticMap' => [
171                         'size' => '300x300',
172                         'format' => 'png',
173                         'mobile' => false,
174                         //'shadow' => true // for icons
175                 ],
176                 'geolocate' => false,
177                 'language' => null,
178                 'region' => null,
179                 'showMarker' => true,
180                 //'showInfoWindow' => true,
181                 'infoWindow' => [
182                         'content' => '',
183                         'useMultiple' => false, // Using single infowindow object for all
184                         'maxWidth' => 300,
185                         'lat' => null,
186                         'lng' => null,
187                         'pixelOffset' => 0,
188                         'zIndex' => 200,
189                         'disableAutoPan' => false
190                 ],
191                 'marker' => [
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
195                         'title' => null,
196                         'shadow' => null,
197                         'shape' => null,
198                         'zIndex' => null,
199                         'draggable' => false,
200                         'cursor' => null,
201                         'directions' => false, // add form with directions
202                         'open' => false, // New in 1.5
203                 ],
204                 'div' => [
205                         'id' => 'map_canvas',
206                         'width' => '100%',
207                         'height' => '400px',
208                         'class' => 'map',
209                         'escape' => true
210                 ],
211                 'event' => [
212                 ],
213                 'animation' => [
214                         //TODO
215                 ],
216                 'polyline' => [
217                         'color' => '#FF0000',
218                         'opacity' => 1.0,
219                         'weight' => 2,
220                 ],
221                 'directions' => [
222                         'travelMode' => self::TRAVEL_MODE_DRIVING,
223                         'unitSystem' => 'METRIC',
224                         'directionsDiv' => null,
225                 ],
226                 'callbacks' => [
227                         'geolocate' => null //TODO
228                 ],
229                 'plugins' => [
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/
233                 ],
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
239                 'key' => null,
240         ];
241
242         /**
243          * @var array
244          */
245         protected $_runtimeConfig = [];
246
247         /**
248          * @var bool
249          */
250         protected $_apiIncluded = false;
251
252         /**
253          * @var bool
254          */
255         protected $_gearsIncluded = false;
256
257         /**
258          * @var bool
259          */
260         protected $_located = false;
261
262         /**
263          * @param \Cake\View\View|null $View
264          * @param array $config
265          */
266         public function __construct(View $View, array $config = []) {
267                 parent::__construct($View, $config);
268         }
269
270         /**
271          * @param array $config
272          * @return void
273          */
274         public function initialize(array $config) {
275                 parent::initialize($config);
276
277                 $defaultConfig = Hash::merge($this->_defaultConfig, (array)Configure::read('GoogleMap'));
278                 $config = Hash::merge($defaultConfig, $config);
279
280                 if (isset($config['api']) && !isset($config['map']['api'])) {
281                         $config['map']['api'] = $config['api'];
282                 }
283                 if (isset($config['zoom']) && !isset($config['map']['zoom'])) {
284                         $config['map']['zoom'] = $config['zoom'];
285                 }
286                 if (isset($config['lat']) && !isset($config['map']['lat'])) {
287                         $config['map']['lat'] = $config['lat'];
288                 }
289                 if (isset($config['lng']) && !isset($config['map']['lng'])) {
290                         $config['map']['lng'] = $config['lng'];
291                 }
292                 if (isset($config['type']) && !isset($config['map']['type'])) {
293                         $config['map']['type'] = $config['type'];
294                 }
295                 if (isset($config['size'])) {
296                         $config['div']['width'] = $config['size']['width'];
297                         $config['div']['height'] = $config['size']['height'];
298                 }
299                 if (isset($config['staticSize'])) {
300                         $config['staticMap']['size'] = $config['staticSize'];
301                 }
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'];
305                 }
306                 if (isset($config['staticLat'])) {
307                         $config['staticMap']['lat'] = $config['staticLat'];
308                 }
309                 if (isset($config['staticLng'])) {
310                         $config['staticMap']['lng'] = $config['staticLng'];
311                 }
312                 if (isset($config['localImages'])) {
313                         if ($config['localImages'] === true) {
314                                 $config['localImages'] = Router::url('/img/google_map/', true);
315                         }
316                 }
317
318                 // BC
319                 if (!empty($config['inline'])) {
320                         trigger_error('Deprecated inline option, use block instead.', E_USER_DEPRECATED);
321                         $config['block'] = null;
322                 }
323
324                 $this->_config = $config;
325                 $this->_runtimeConfig = $this->_config;
326         }
327
328         /**
329          * JS maps.google API url.
330          *
331          * Options read via configs
332          * - key
333          * - api
334          * - language (iso2: en, de, ja, ...)
335          *
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)
338          *
339          * @param array $query
340          * @return string Full URL
341          */
342         public function apiUrl(array $query = []) {
343                 $url = $this->_protocol() . static::API;
344
345                 if ($this->_runtimeConfig['map']['api']) {
346                          $query['v'] = $this->_runtimeConfig['map']['api'];
347                 }
348                 if ($this->_runtimeConfig['key']) {
349                         $query['key'] = $this->_runtimeConfig['key'];
350                 }
351
352                 if ($this->_runtimeConfig['language']) {
353                         $query['language'] = $this->_runtimeConfig['language'];
354                 }
355
356                 if ($query) {
357                         $query = http_build_query($query);
358
359                         $url .= '?' . $query;
360                 }
361
362                 return $url;
363         }
364
365         /**
366          * @deprecated
367          * @return string
368          */
369         public function gearsUrl() {
370                 $this->_gearsIncluded = true;
371                 $url = $this->_protocol() . 'code.google.com/apis/gears/gears_init.js';
372                 return $url;
373         }
374
375         /**
376          * @return string currentMapObject
377          */
378         public function name() {
379                 return 'map' . static::$mapCount;
380         }
381
382         /**
383          * @return string currentContainerId
384          */
385         public function id() {
386                 return $this->_runtimeConfig['div']['id'];
387         }
388
389         /**
390          * Make it possible to include multiple maps per page
391          * resets markers, infoWindows etc
392          *
393          * @param bool $full true=optionsAsWell
394          * @return void
395          */
396         public function reset($full = true) {
397                 static::$markerCount = static::$infoWindowCount = 0;
398                 $this->markers = $this->infoWindows = [];
399                 if ($full) {
400                         $this->_runtimeConfig = $this->_config;
401                 }
402         }
403
404         /**
405          * Set the controls of current map
406          *
407          * Control options
408          * - zoom, scale, overview: TRUE/FALSE
409          *
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")
413          *
414          * @param array $options
415          * @return void
416          */
417         public function setControls(array $options = []) {
418                 if (isset($options['streetView'])) {
419                         $this->_runtimeConfig['map']['streetViewControl'] = $options['streetView'];
420                 }
421                 if (isset($options['zoom'])) {
422                         $this->_runtimeConfig['map']['scaleControl'] = $options['zoom'];
423                 }
424                 if (isset($options['scrollwheel'])) {
425                         $this->_runtimeConfig['map']['scrollwheel'] = $options['scrollwheel'];
426                 }
427                 if (isset($options['keyboardShortcuts'])) {
428                         $this->_runtimeConfig['map']['keyboardShortcuts'] = $options['keyboardShortcuts'];
429                 }
430                 if (isset($options['type'])) {
431                         $this->_runtimeConfig['map']['type'] = $options['type'];
432                 }
433         }
434
435         /**
436          * This the initialization point of the script
437          * Returns the div container you can echo on the website
438          *
439          * @param array $options associative array of settings are passed
440          * @return string divContainer
441          */
442         public function map(array $options = []) {
443                 $this->reset();
444                 $this->_runtimeConfig = Hash::merge($this->_runtimeConfig, $options);
445                 $this->_runtimeConfig['map'] = $options + $this->_runtimeConfig['map'];
446
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'];
450                 }
451                 if (!isset($this->_runtimeConfig['map']['zoom'])) {
452                         $this->_runtimeConfig['map']['zoom'] = $this->_runtimeConfig['map']['defaultZoom'];
453                 }
454
455                 $result = '';
456
457                 // autoinclude js?
458                 if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) {
459                         $res = $this->Html->script($this->apiUrl(), ['block' => $this->_runtimeConfig['block']]);
460                         $this->_apiIncluded = true;
461
462                         if (!$this->_runtimeConfig['block']) {
463                                 $result .= $res . PHP_EOL;
464                         }
465                         // usually already included
466                         //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js
467                 }
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;
473                         }
474                 }
475
476                 $map = "
477                         var initialLocation = " . $this->_initialLocation() . ";
478                         var browserSupportFlag = new Boolean();
479                         var myOptions = " . $this->_mapOptions() . ";
480
481                         // deprecated
482                         gMarkers" . static::$mapCount . " = new Array();
483                         gInfoWindows" . static::$mapCount . " = new Array();
484                         gWindowContents" . static::$mapCount . " = new Array();
485                 ";
486
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
490                 }
491                 $this->_mapIds[] = $this->_runtimeConfig['div']['id'];
492
493                 $map .= "
494                         var " . $this->name() . ' = new google.maps.Map(document.getElementById("' . $this->_runtimeConfig['div']['id'] . "\"), myOptions);
495                         ";
496                 $this->map = $map;
497
498                 $this->_runtimeConfig['div']['style'] = '';
499                 if (is_numeric($this->_runtimeConfig['div']['width'])) {
500                         $this->_runtimeConfig['div']['width'] .= 'px';
501                 }
502                 if (is_numeric($this->_runtimeConfig['div']['height'])) {
503                         $this->_runtimeConfig['div']['height'] .= 'px';
504                 }
505
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']);
510
511                 $defaultText = isset($this->_runtimeConfig['content']) ? $this->_runtimeConfig['content'] : __('Map cannot be displayed!');
512                 $result .= $this->Html->tag('div', $defaultText, $this->_runtimeConfig['div']);
513
514                 return $result;
515         }
516
517         /**
518          * Generate a new LatLng object with the current lat and lng.
519          *
520          * @return string
521          */
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'] . ')';
525                 }
526                 $this->_runtimeConfig['autoCenter'] = true;
527                 return 'false';
528         }
529
530         /**
531          * Add a marker to the map.
532          *
533          * Options:
534          * - lat and lng or address (to geocode on demand, not recommended, though)
535          * - title, content, icon, directions, maxWidth, open (optional)
536          *
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.
539          *
540          * @param array $options
541          * @return mixed Integer marker count or boolean false on failure
542          * @throws \Cake\Core\Exception\Exception
543          */
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']);
549                 }
550                 $options += $defaults;
551
552                 $params = [];
553                 $params['map'] = $this->name();
554
555                 if (isset($options['title'])) {
556                         $params['title'] = json_encode($options['title']);
557                 }
558                 if (isset($options['icon'])) {
559                         $params['icon'] = $options['icon'];
560                         if (is_int($params['icon'])) {
561                                 $params['icon'] = 'gIcons' . static::$mapCount . '[' . $params['icon'] . ']';
562                         } else {
563                                 $params['icon'] = json_encode($params['icon']);
564                         }
565                 }
566                 if (isset($options['shadow'])) {
567                         $params['shadow'] = $options['shadow'];
568                         if (is_int($params['shadow'])) {
569                                 $params['shadow'] = 'gIcons' . static::$mapCount . '[' . $params['shadow'] . ']';
570                         } else {
571                                 $params['shadow'] = json_encode($params['shadow']);
572                         }
573                 }
574                 if (isset($options['shape'])) {
575                         $params['shape'] = $options['shape'];
576                 }
577                 if (isset($options['zIndex'])) {
578                         $params['zIndex'] = $options['zIndex'];
579                 }
580                 if (isset($options['animation'])) {
581                         $params['animation'] = 'google.maps.Animation.' . strtoupper($options['animation']);
582                 }
583
584                 // geocode if necessary
585                 if (!isset($options['lat']) || !isset($options['lng'])) {
586                         $this->map .= "
587 var geocoder = new google.maps.Geocoder();
588
589 function geocodeAddress(address) {
590         geocoder.geocode({'address': address}, function(results, status) {
591                 if (status == google.maps.GeocoderStatus.OK) {
592
593                         x" . static::$markerCount . " = new google.maps.Marker({
594                                 position: results[0].geometry.location,
595                                 " . $this->_toObjectParams($params, false, false) . "
596                         });
597                         gMarkers" . static::$mapCount . " .push(
598                                 x" . static::$markerCount . "
599                         );
600                         return results[0].geometry.location;
601                 } else {
602                         //alert('Geocoding was not successful for the following reason: ' + status);
603                         return null;
604                 }
605         });
606 }";
607                         if (!isset($options['address'])) {
608                                 throw new Exception('Either use lat/lng or address to add a marker');
609                         }
610                         $position = 'geocodeAddress("' . h($options['address']) . '")';
611                 } else {
612                         $position = 'new google.maps.LatLng(' . $options['lat'] . ',' . $options['lng'] . ')';
613                 }
614
615                 $marker = "
616                         var x" . static::$markerCount . " = new google.maps.Marker({
617                                 position: " . $position . ",
618                                 " . $this->_toObjectParams($params, false, false) . "
619                         });
620                         gMarkers" . static::$mapCount . " .push(
621                                 x" . static::$markerCount . "
622                         );
623                 ";
624                 $this->map .= $marker;
625
626                 if (!empty($options['directions'])) {
627                         $options['content'] .= $this->_directions($options['directions'], $options);
628                 }
629
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']);
634
635                 } elseif (!empty($options['content'])) {
636                         if (!isset($this->_runtimeConfig['marker']['infoWindow'])) {
637                                 $this->_runtimeConfig['marker']['infoWindow'] = $this->addInfoWindow();
638                         }
639
640                         $x = $this->addInfoContent($options['content']);
641                         $event = "
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 . "]);
644                         ";
645                         $this->addCustomEvent(static::$markerCount, $event);
646
647                         if (!empty($options['open'])) {
648                                 $this->addCustom($event);
649                         }
650                 }
651
652                 // Custom matching event?
653                 if (isset($options['id'])) {
654                         $this->matching[$options['id']] = static::$markerCount;
655                 }
656
657                 return static::$markerCount++;
658         }
659
660         /**
661          * Build directions form (type get) for directions inside infoWindows
662          *
663          * Options for directions (if array)
664          * - label
665          * - submit
666          * - escape: defaults to true
667          *
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
673          */
674         protected function _directions($directions, array $markerOptions = []) {
675                 $options = [
676                         'from' => null,
677                         'to' => null,
678                         'label' => __('Enter your address'),
679                         'submit' => __('Get directions'),
680                         'escape' => true,
681                         'zoom' => null, // auto
682                 ];
683                 if ($directions === true) {
684                         $options['to'] = $markerOptions['lat'] . ',' . $markerOptions['lng'];
685                 } elseif (is_array($directions)) {
686                         $options = $directions + $options;
687                 }
688                 if (empty($options['to']) && empty($options['from'])) {
689                         return '';
690                 }
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'] . '" />';
695                 } else {
696                         $form .= '<input type="text" name="saddr" />';
697                 }
698                 if (!empty($options['to'])) {
699                         $form .= '<input type="hidden" name="daddr" value="' . $options['to'] . '" />';
700                 } else {
701                         $form .= '<input type="text" name="daddr" />';
702                 }
703                 if (isset($options['zoom'])) {
704                         $form .= '<input type="hidden" name="z" value="' . $options['zoom'] . '" />';
705                 }
706                 $form .= '<input type="submit" value="' . $options['submit'] . '" />';
707                 $form .= '</form>';
708
709                 return '<div class="directions">' . $form . '</div>';
710         }
711
712         /**
713          * @param string $content
714          * @return int Current marker counter
715          */
716         public function addInfoContent($content) {
717                 $this->infoContents[static::$markerCount] = $this->escapeString($content);
718                 $event = "
719                         gWindowContents" . static::$mapCount . '.push(' . $this->escapeString($content) . ");
720                         ";
721                 $this->addCustom($event);
722
723                 //TODO: own count?
724                 return static::$markerCount;
725         }
726
727         /**
728          * @var array
729          */
730         public $setIcons = [
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'
735         ];
736
737         /**
738          * Get a custom icon set
739          *
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, ...)
745          */
746         public function iconSet($color, $char = null, $size = 'm') {
747                 $colors = ['red', 'green', 'yellow', 'blue', 'purple', 'white', 'black'];
748                 if (!in_array($color, $colors)) {
749                         $color = 'red';
750                 }
751
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';
757                 }
758
759                 if (!empty($char)) {
760                         if ($color === 'red') {
761                                 $color = '';
762                         } else {
763                                 $color = '_' . $color;
764                         }
765                         $url = sprintf($this->setIcons['alpha'], $color, $char);
766                 } else {
767                         if ($color === 'red') {
768                                 $color = '';
769                         } else {
770                                 $color = '_' . $color;
771                         }
772                         $url = sprintf($this->setIcons['color'], $color);
773                 }
774
775                 /*
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)
780         );
781
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)
786         );
787
788         var iconShape = {
789             coord: [1, 1, 1, 32, 32, 32, 32, 1],
790             type: 'poly'
791         };
792         */
793
794                 $shadow = 'http://www.google.com/mapfiles/shadow50.png';
795                 $res = [
796                         'url' => $url,
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]])
799                 ];
800                 return $res;
801         }
802
803         /**
804          * Generate icon array.
805          *
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
809          *
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
815          */
816         public function addIcon($image, $shadow = null, array $imageOptions = [], array $shadowOptions = []) {
817                 $res = ['url' => $image];
818                 $res['icon'] = $this->icon($image, $imageOptions);
819                 if ($shadow) {
820                         $last = $this->_iconRemember[$res['icon']];
821                         if (!isset($shadowOptions['anchor'])) {
822                                 $shadowOptions['anchor'] = [];
823                         }
824                         $shadowOptions['anchor'] = $last['options']['anchor'] + $shadowOptions['anchor'];
825
826                         $res['shadow'] = $this->icon($shadow, $shadowOptions);
827                 }
828                 return $res;
829         }
830
831         /**
832          * @var array
833          */
834         protected $_iconRemember = [];
835
836         /**
837          * Generate icon object
838          *
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
845          */
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";
857                                 }
858                                 $data = getimagesize($canonicalPath);
859                         } else {
860                                 $data = getimagesize($url);
861                         }
862                         if ($data) {
863                                 $options['size']['width'] = $data[0];
864                                 $options['size']['height'] = $data[1];
865                         } else {
866                                 $options['size']['width'] = $options['size']['height'] = 0;
867                         }
868                 }
869                 if (empty($options['anchor'])) {
870                         $options['anchor']['width'] = (int)($options['size']['width'] / 2);
871                         $options['anchor']['height'] = $options['size']['height'];
872                 }
873                 if (empty($options['origin'])) {
874                         $options['origin']['width'] = $options['origin']['height'] = 0;
875                 }
876                 if (isset($options['shadow'])) {
877                         $options['anchor'] = $options['shadow'];
878                 }
879
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'] . ')
884 )';
885                 $this->icons[static::$iconCount] = $icon;
886                 $this->_iconRemember[static::$iconCount] = ['url' => $url, 'options' => $options, 'id' => static::$iconCount];
887                 return static::$iconCount++;
888         }
889
890         /**
891          * Creates a new InfoWindow.
892          *
893          * @param array $options
894          * - lat, lng, content, maxWidth, pixelOffset, zIndex
895          * @return int windowCount
896          */
897         public function addInfoWindow(array $options = []) {
898                 $defaults = $this->_runtimeConfig['infoWindow'];
899                 $options += $defaults;
900
901                 if (!empty($options['lat']) && !empty($options['lng'])) {
902                         $position = 'new google.maps.LatLng(' . $options['lat'] . ', ' . $options['lng'] . ')';
903                 } else {
904                         $position = ' ' . $this->name() . ' .getCenter()';
905                 }
906
907                 $windows = "
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']},*/
914                         }));
915                         ";
916                 $this->map .= $windows;
917                 return static::$infoWindowCount++;
918         }
919
920         /**
921          * Add event to open marker on click.
922          *
923          * @param int $marker
924          * @param int $infoWindow
925          * @param bool $open Also open it right away.
926          * @return void
927          */
928         public function addEvent($marker, $infoWindow, $open = false) {
929                 $this->map .= "
930                         google.maps.event.addListener(gMarkers" . static::$mapCount . "[{$marker}], 'click', function() {
931                                 gInfoWindows" . static::$mapCount . "[$infoWindow].open(" . $this->name() . ", this);
932                         });
933                 ";
934                 if ($open) {
935                         $event = 'gInfoWindows' . static::$mapCount . "[$infoWindow].open(" . $this->name() .
936                                 ', gMarkers' . static::$mapCount . '[' . $marker . ']);';
937                         $this->addCustom($event);
938                 }
939         }
940
941         /**
942          * Add a custom event for a marker on click.
943          *
944          * @param int $marker
945          * @param string $event (js)
946          * @return void
947          */
948         public function addCustomEvent($marker, $event) {
949                 $this->map .= "
950                         google.maps.event.addListener(gMarkers" . static::$mapCount . "[{$marker}], 'click', function() {
951                                 $event
952                         });
953                 ";
954         }
955
956         /**
957          * Add custom JS.
958          *
959          * @param string $js Custom JS
960          * @return void
961          */
962         public function addCustom($js) {
963                 $this->map .= $js;
964         }
965
966         /**
967          * Add directions to the map.
968          *
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
981          * - region: String
982          * @see https://developers.google.com/maps/documentation/javascript/3.exp/reference#DirectionsRequest
983          * @return void
984          */
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']];
990
991                 $directions = "
992                         var {$id}Service = new google.maps.DirectionsService();
993                         var {$id}Display;
994                         {$id}Display = new google.maps.DirectionsRenderer();
995                         {$id}Display. setMap(" . $this->name() . ");
996                         ";
997
998                 if (!empty($options['directionsDiv'])) {
999                         $directions .= "{$id}Display. setPanel(document.getElementById('" . $options['directionsDiv'] . "'));";
1000                 }
1001
1002                 if (is_array($from)) {
1003                         $from = 'new google.maps.LatLng(' . (float)$from['lat'] . ', ' . (float)$from['lng'] . ')';
1004                 } else {
1005                         $from = '"' . h($from) . '"';
1006                 }
1007                 if (is_array($to)) {
1008                         $to = 'new google.maps.LatLng(' . (float)$to['lat'] . ', ' . (float)$to['lng'] . ')';
1009                 } else {
1010                         $to = '"' . h($to) . '"';
1011                 }
1012
1013                 $directions .= "
1014                         var request = {
1015                                 origin: $from,
1016                                 destination: $to,
1017                                 unitSystem: google.maps.UnitSystem." . $options['unitSystem'] . ",
1018                                 travelMode: google.maps.TravelMode. $travelMode
1019                         };
1020                         {$id}Service.route(request, function(result, status) {
1021                                 if (status == google.maps.DirectionsStatus.OK) {
1022                                         {$id}Display. setDirections(result);
1023                                 }
1024                         });
1025                 ";
1026                 $this->map .= $directions;
1027         }
1028
1029         /**
1030          * Add a polyline
1031          *
1032          * This method adds a line between 2 points
1033          *
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
1041          * @return void
1042          */
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'] . ')';
1046                 } else {
1047                         throw new Exception('not implemented yet, use array of lat/lng');
1048                         //$from = '\'' . h($from) . '\'';
1049                 }
1050                 if (is_array($to)) {
1051                         $to = 'new google.maps.LatLng(' . (float)$to['lat'] . ', ' . (float)$to['lng'] . ')';
1052                 } else {
1053                         throw new Exception('not implemented yet, use array of lat/lng');
1054                         //$to = '\'' . h($to) . '\'';
1055                 }
1056
1057                 $defaults = $this->_runtimeConfig['polyline'];
1058                 $options += $defaults;
1059
1060                 $id = 'p' . static::$markerCount++;
1061
1062                 $polyline = "var start = $from;";
1063                 $polyline .= "var end = $to;";
1064                 $polyline .= "
1065                                 var poly = [
1066                                         start,
1067                                         end
1068                                 ];
1069                                 var {$id}Polyline = new google.maps.Polyline({
1070                                         path: poly,
1071                                         strokeColor: '" . $options['color'] . "',
1072                                         strokeOpacity: " . $options['opacity'] . ",
1073                                         strokeWeight: " . $options['weight'] . "
1074                                 });
1075                                 {$id}Polyline.setMap(" . $this->name() . ");
1076                         ";
1077                 $this->map .= $polyline;
1078         }
1079
1080         /**
1081          * @param string $content (html/text)
1082          * @param int $index infoWindowCount
1083          * @return void
1084          */
1085         public function setContentInfoWindow($content, $index) {
1086                 $this->map .= "
1087                         gInfoWindows" . static::$mapCount . "[$index]. setContent(" . $this->escapeString($content) . ');';
1088         }
1089
1090         /**
1091          * Json encode string
1092          *
1093          * @param mixed $content
1094          * @return string JSON
1095          */
1096         public function escapeString($content) {
1097                 return json_encode($content);
1098         }
1099
1100         /**
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.
1104          *
1105          * @return string
1106          */
1107         public function script() {
1108                 $script = '<script>
1109                 ' . $this->finalize(true) . '
1110 </script>';
1111                 return $script;
1112         }
1113
1114         /**
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!
1117          *
1118          * @param bool $return If the output should be returned instead
1119          * @return null|string Javascript if $return is true
1120          */
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) . '
1124
1125         jQuery(document).ready(function() {
1126                 ';
1127
1128                 $script .= $this->map;
1129                 if ($this->_runtimeConfig['geolocate']) {
1130                         $script .= $this->_geolocate();
1131                 }
1132
1133                 if ($this->_runtimeConfig['showMarker'] && !empty($this->markers) && is_array($this->markers)) {
1134                         $script .= implode($this->markers, ' ');
1135                 }
1136
1137                 if ($this->_runtimeConfig['autoCenter']) {
1138                         $script .= $this->_autoCenter();
1139                 }
1140                 $script .= '
1141
1142         });';
1143                 static::$mapCount++;
1144                 if ($return) {
1145                         return $script;
1146                 }
1147                 $this->Html->scriptBlock($script, ['block' => true]);
1148         }
1149
1150         /**
1151          * Set a custom geolocate callback
1152          *
1153          * @param string|bool $js Custom JS
1154          * false: no callback at all
1155          * @return void
1156          */
1157         public function geolocateCallback($js) {
1158                 if ($js === false) {
1159                         $this->_runtimeConfig['callbacks']['geolocate'] = false;
1160                         return;
1161                 }
1162                 $this->_runtimeConfig['callbacks']['geolocate'] = $js;
1163         }
1164
1165         /**
1166          * Experimental - works in cutting edge browsers like chrome10
1167          *
1168          * @return string
1169          */
1170         protected function _geolocate() {
1171                 return '
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);
1177                 }, function() {
1178                         handleNoGeolocation(browserSupportFlag);
1179                 });
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);
1186                 }, function() {
1187                         handleNoGeoLocation(browserSupportFlag);
1188                 });
1189                 // Browser doesn\'t support Geolocation
1190         } else {
1191                 browserSupportFlag = false;
1192                 handleNoGeolocation(browserSupportFlag);
1193         }
1194
1195         function geolocationCallback(lat, lng) {
1196                 ' . $this->_geolocationCallback() . '
1197         }
1198
1199         function handleNoGeolocation(errorFlag) {
1200         if (errorFlag == true) {
1201                 //alert("Geolocation service failed.");
1202         } else {
1203                 //alert("Your browser doesn\'t support geolocation. We\'ve placed you in Siberia.");
1204         }
1205         //' . $this->name() . ' . setCenter(initialLocation);
1206         }
1207         ';
1208         }
1209
1210         /**
1211          * @return string
1212          */
1213         protected function _geolocationCallback() {
1214                 if (($js = $this->_runtimeConfig['callbacks']['geolocate']) === false) {
1215                         return '';
1216                 }
1217                 if ($js === null) {
1218                         $js = 'initialLocation = new google.maps.LatLng(lat, lng);
1219                 ' . $this->name() . ' . setCenter(initialLocation);
1220 ';
1221                 }
1222                 return $js;
1223         }
1224
1225         /**
1226          * Auto center map
1227          * careful: with only one marker this can result in too high zoom values!
1228          *
1229          * @return string autoCenterCommands
1230          */
1231         protected function _autoCenter() {
1232                 return '
1233                 var bounds = new google.maps.LatLngBounds();
1234                 $.each(gMarkers' . static::$mapCount . ',function (index, marker) { bounds.extend(marker.position);});
1235                 ' . $this->name() . ' .fitBounds(bounds);
1236                 ';
1237         }
1238
1239         /**
1240          * @return string JSON like js string
1241          */
1242         protected function _mapOptions() {
1243                 $options = $this->_runtimeConfig['map'] + $this->_runtimeConfig;
1244
1245                 $mapOptions = array_intersect_key($options, [
1246                         'streetViewControl' => null,
1247                         'navigationControl' => null,
1248                         'mapTypeControl' => null,
1249                         'scaleControl' => null,
1250                         'scrollwheel' => null,
1251                         'zoom' => null,
1252                         'keyboardShortcuts' => null,
1253                         'styles' => null,
1254                 ]);
1255                 $res = [];
1256                 foreach ($mapOptions as $key => $mapOption) {
1257                         $res[] = $key . ': ' . $this->value($mapOption);
1258                 }
1259                 if (empty($options['autoCenter'])) {
1260                         $res[] = 'center: initialLocation';
1261                 }
1262                 if (!empty($options['navOptions'])) {
1263                         $res[] = 'navigationControlOptions: ' . $this->_controlOptions('nav', $options['navOptions']);
1264                 }
1265                 if (!empty($options['typeOptions'])) {
1266                         $res[] = 'mapTypeControlOptions: ' . $this->_controlOptions('type', $options['typeOptions']);
1267                 }
1268                 if (!empty($options['scaleOptions'])) {
1269                         $res[] = 'scaleControlOptions: ' . $this->_controlOptions('scale', $options['scaleOptions']);
1270                 }
1271
1272                 if (array_key_exists($options['type'], $this->types)) {
1273                         $type = $this->types[$options['type']];
1274                 } else {
1275                         $type = $options['type'];
1276                 }
1277                 $res[] = 'mapTypeId: google.maps.MapTypeId.' . $type;
1278
1279                 return '{' . implode(', ', $res) . '}';
1280         }
1281
1282         /**
1283          * @param string $type
1284          * @param array $options
1285          * @return string JSON like js string
1286          */
1287         protected function _controlOptions($type, $options) {
1288                 $mapping = [
1289                         'nav' => 'NavigationControlStyle',
1290                         'type' => 'MapTypeControlStyle',
1291                         'scale' => ''
1292                 ];
1293                 $res = [];
1294                 if (!empty($options['style']) && ($m = $mapping[$type])) {
1295                         $res[] = 'style: google.maps.' . $m . '.' . $options['style'];
1296                 }
1297                 if (!empty($options['pos'])) {
1298                         $res[] = 'position: google.maps.ControlPosition.' . $options['pos'];
1299                 }
1300
1301                 return '{' . implode(', ', $res) . '}';
1302         }
1303
1304         /**
1305          * Returns a maps.google link
1306          *
1307          * @param string $title  Link title
1308          * @param array $mapOptions
1309          * @param array $linkOptions
1310          * @return string HTML link
1311          */
1312         public function mapLink($title, $mapOptions = [], $linkOptions = []) {
1313                 return $this->Html->link($title, $this->mapUrl($mapOptions + ['escape' => false]), $linkOptions);
1314         }
1315
1316         /**
1317          * Returns a maps.google url
1318          *
1319          * Options:
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
1325          *
1326          * @param array $options Options
1327          * @return string link: http://...
1328          */
1329         public function mapUrl(array $options = []) {
1330                 $url = $this->_protocol() . 'maps.google.com/maps?';
1331
1332                 $urlArray = !empty($options['query']) ? $options['query'] : [];
1333                 if (!empty($options['from'])) {
1334                         $urlArray['saddr'] = $options['from'];
1335                 }
1336
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;
1341                         }
1342                         $urlArray['daddr'] = $to;
1343                 } elseif (!empty($options['to'])) {
1344                         $urlArray['daddr'] = $options['to'];
1345                 }
1346
1347                 if (isset($options['zoom']) && $options['zoom'] !== false) {
1348                         $urlArray['z'] = (int)$options['zoom'];
1349                 }
1350                 //$urlArray[] = 'f=d';
1351                 //$urlArray[] = 'hl=de';
1352                 //$urlArray[] = 'ie=UTF8';
1353
1354                 $options += [
1355                         'escape' => true,
1356                 ];
1357
1358                 $query = http_build_query($urlArray);
1359                 if ($options['escape']) {
1360                         $query = h($query);
1361                 }
1362
1363                 return $url . $query;
1364         }
1365
1366         /**
1367          * Creates a plain image map.
1368          *
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
1381          * - title
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
1385          */
1386         public function staticMap(array $options = [], array $attributes = []) {
1387                 $defaultAttributes = ['alt' => __d('tools', 'Map')];
1388                 $attributes += $defaultAttributes;
1389
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);
1393         }
1394
1395         /**
1396          * Create a link to a plain image map
1397          *
1398          * @param string $title Link title
1399          * @param array $mapOptions
1400          * @param array $linkOptions
1401          * @return string HTML link
1402          */
1403         public function staticMapLink($title, array $mapOptions = [], array $linkOptions = []) {
1404                 return $this->Html->link($title, $this->staticMapUrl($mapOptions + ['escape' => false]), $linkOptions);
1405         }
1406
1407         /**
1408          * Creates a URL to a plain image map.
1409          *
1410          * Options:
1411          * - escape: defaults to true (Deprecated as of CakePHP 3.5.1 and now has to be always false)
1412          *
1413          * @param array $options
1414          * - see staticMap() for details
1415          * @return string urlOfImage: http://...
1416          */
1417         public function staticMapUrl(array $options = []) {
1418                 $mapUrl = $this->_protocol() . static::STATIC_API;
1419                 /*
1420                 $params = array(
1421                         'mobile' => 'false',
1422                         'format' => 'png',
1423                         //'center' => false
1424                 );
1425
1426                 if (!empty($options['mobile'])) {
1427                         $params['mobile'] = 'true';
1428                 }
1429                 */
1430
1431                 $defaults = $this->_config['staticMap'] + $this->_config;
1432
1433                 $mapOptions = $options + $defaults;
1434
1435                 $params = array_intersect_key($mapOptions, [
1436                         'mobile' => null,
1437                         'format' => null,
1438                         'size' => null,
1439                         //'zoom' => null,
1440                         //'lat' => null,
1441                         //'lng' => null,
1442                         //'visible' => null,
1443                         //'type' => null,
1444                 ]);
1445
1446                 // add API key to parameters.
1447                 if ($this->_runtimeConfig['key']) {
1448                         $params['key'] = $this->_runtimeConfig['key'];
1449                 }
1450
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';
1454                 }
1455
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']);
1459                 }
1460
1461                 // center and zoom are not necessary if path, visible or markers are given
1462                 if (!isset($options['center']) || $options['center'] === false) {
1463                         // dont use it
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']);
1468                 } /*else {
1469                         // try to read from markers array???
1470                         if (isset($options['markers']) && count($options['markers']) == 1) {
1471                                 //pr ($options['markers']);
1472                         }
1473                 }*/
1474
1475                 if (!isset($options['zoom']) || $options['zoom'] === false) {
1476                         // dont use it
1477                 } else {
1478                         if ($options['zoom'] === 'auto') {
1479                                 if (!empty($options['markers']) && strpos($options['zoom'], '|') !== false) {
1480                                         // let google find the best zoom value itself
1481                                 } else {
1482                                         // do something here?
1483                                 }
1484                         } else {
1485                                 $params['zoom'] = $options['zoom'];
1486                         }
1487                 }
1488
1489                 if (array_key_exists($mapOptions['type'], $this->types)) {
1490                         $params['maptype'] = $this->types[$mapOptions['type']];
1491                 } else {
1492                         $params['maptype'] = $mapOptions['type'];
1493                 }
1494                 $params['maptype'] = strtolower($params['maptype']);
1495
1496                 // old: {latitude},{longitude},{color}{alpha-character}
1497                 // new: @see staticMarkers()
1498                 if (!empty($options['markers'])) {
1499                         $params['markers'] = $options['markers'];
1500                 }
1501
1502                 if (!empty($options['paths'])) {
1503                         $params['path'] = $options['paths'];
1504                 }
1505
1506                 // valXval
1507                 if (!empty($options['size'])) {
1508                         $params['size'] = $options['size'];
1509                 }
1510
1511                 $pieces = [];
1512                 foreach ($params as $key => $value) {
1513                         if (is_array($value)) {
1514                                 $value = implode('&' . $key . '=', $value);
1515                         } elseif ($value === true) {
1516                                 $value = 'true';
1517                         } elseif ($value === false) {
1518                                 $value = 'false';
1519                         } elseif ($value === null) {
1520                                 continue;
1521                         }
1522                         $pieces[] = $key . '=' . $value;
1523                 }
1524
1525                 $options += [
1526                         'escape' => true,
1527                 ];
1528                 $query = implode('&', $pieces);
1529                 if ($options['escape']) {
1530                         $query = h($query);
1531                 }
1532
1533                 return $mapUrl . '?' . $query;
1534         }
1535
1536         /**
1537          * Prepare paths for staticMap
1538          *
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{|...}
1544          */
1545         public function staticPaths(array $pos = []) {
1546                 $defaults = [
1547                         'color' => 'blue',
1548                         'weight' => 5 // pixel
1549                 ];
1550
1551                 // not a 2-level array? make it one
1552                 if (!isset($pos[0])) {
1553                         $pos = [$pos];
1554                 }
1555
1556                 $res = [];
1557                 foreach ($pos as $p) {
1558                         $options = $p + $defaults;
1559
1560                         $markers = $options['path'];
1561                         unset($options['path']);
1562
1563                         // prepare color
1564                         if (!empty($options['color'])) {
1565                                 $options['color'] = $this->_prepColor($options['color']);
1566                         }
1567
1568                         $path = [];
1569                         foreach ($options as $key => $value) {
1570                                 $path[] = $key . ':' . urlencode($value);
1571                         }
1572                         foreach ($markers as $key => $pos) {
1573                                 if (is_array($pos)) {
1574                                         // lat/lng?
1575                                         $pos = $pos['lat'] . ',' . $pos['lng'];
1576                                 }
1577                                 $path[] = $pos;
1578                         }
1579                         $res[] = implode('|', $path);
1580                 }
1581                 return $res;
1582         }
1583
1584         /**
1585          * Prepare markers for staticMap
1586          *
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)
1596          * - color
1597          * - label
1598          * - icon
1599          * - shadow
1600          * @return array markers: color:green|label:Z|48,11|Berlin
1601          *
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{|...}
1604          */
1605         public function staticMarkers(array $pos = [], array $style = []) {
1606                 $markers = [];
1607                 $verbose = false;
1608
1609                 $defaults = [
1610                         'shadow' => 'true',
1611                         'color' => 'blue',
1612                         'label' => '',
1613                         'address' => '',
1614                         'size' => ''
1615                 ];
1616
1617                 // not a 2-level array? make it one
1618                 if (!isset($pos[0])) {
1619                         $pos = [$pos];
1620                 }
1621
1622                 // new in staticV2: separate styles! right now just merged
1623                 foreach ($pos as $p) {
1624                         $p += $style + $defaults;
1625
1626                         // adress or lat/lng?
1627                         if (!empty($p['lat']) && !empty($p['lng'])) {
1628                                 $p['address'] = $p['lat'] . ',' . $p['lng'];
1629                         }
1630                         $p['address'] = urlencode($p['address']);
1631
1632                         $values = [];
1633
1634                         // prepare color
1635                         if (!empty($p['color'])) {
1636                                 $p['color'] = $this->_prepColor($p['color']);
1637                                 $values[] = 'color:' . $p['color'];
1638                         }
1639                         // label? A-Z0-9
1640                         if (!empty($p['label'])) {
1641                                 $values[] = 'label:' . strtoupper($p['label']);
1642                         }
1643                         if (!empty($p['size'])) {
1644                                 $values[] = 'size:' . $p['size'];
1645                         }
1646                         if (!empty($p['shadow'])) {
1647                                 $values[] = 'shadow:' . $p['shadow'];
1648                         }
1649                         if (!empty($p['icon'])) {
1650                                 $values[] = 'icon:' . urlencode($p['icon']);
1651                         }
1652                         $values[] = $p['address'];
1653
1654                         //TODO: icons
1655                         $markers[] = implode('|', $values);
1656                 }
1657
1658                 //TODO: shortcut? only possible if no custom params!
1659                 if ($verbose) {
1660
1661                 }
1662                 // long: markers=styles1|address1&markers=styles2|address2&...
1663                 // short: markers=styles,address1|address2|address3|...
1664
1665                 return $markers;
1666         }
1667
1668         /**
1669          * Ensure that we stay on the appropriate protocol
1670          *
1671          * @return string protocol base (including ://)
1672          */
1673         protected function _protocol() {
1674                 $https = $this->_runtimeConfig['https'];
1675                 if ($https === null) {
1676                         $https = !empty($_SERVER['HTTPS']) && $_SERVER['HTTPS'] === 'on';
1677                 }
1678                 return ($https ? 'https' : 'http') . '://';
1679         }
1680
1681         /**
1682          * // to 0x
1683          * or // added
1684          *
1685          * @param string $color Color: FFFFFF, #FFFFFF, 0xFFFFFF or blue
1686          * @return string Color
1687          */
1688         protected function _prepColor($color) {
1689                 if (strpos($color, '#') !== false) {
1690                         return str_replace('#', '0x', $color);
1691                 }
1692                 if (is_numeric($color)) {
1693                         return '0x' . $color;
1694                 }
1695                 return $color;
1696         }
1697
1698         /**
1699          * @param string $name
1700          * @param array $array
1701          * @param bool $asString
1702          * @param bool $keyAsString
1703          * @return string
1704          */
1705         protected function _arrayToObject($name, $array, $asString = true, $keyAsString = false) {
1706                 $res = 'var ' . $name . ' = {' . PHP_EOL;
1707                 $res .= $this->_toObjectParams($array, $asString, $keyAsString);
1708                 $res .= '};';
1709                 return $res;
1710         }
1711
1712         /**
1713          * @param array $array
1714          * @param bool $asString
1715          * @param bool $keyAsString
1716          * @return string
1717          */
1718         protected function _toObjectParams($array, $asString = true, $keyAsString = false) {
1719                 $pieces = [];
1720                 foreach ($array as $key => $value) {
1721                         $e = ($asString && strpos($value, 'new ') !== 0 ? '"' : '');
1722                         $ke = ($keyAsString ? '"' : '');
1723                         $pieces[] = $ke . $key . $ke . ': ' . $e . $value . $e;
1724                 }
1725                 return implode(',' . PHP_EOL, $pieces);
1726         }
1727
1728 }