diff --git a/src/View/Helper/GoogleMapHelper.php b/src/View/Helper/GoogleMapHelper.php index 41455e43..c652ef7e 100644 --- a/src/View/Helper/GoogleMapHelper.php +++ b/src/View/Helper/GoogleMapHelper.php @@ -237,6 +237,7 @@ class GoogleMapHelper extends Helper { 'localImages' => false, 'https' => null, // auto detect 'key' => null, + 'libraries' => null, ]; /** @@ -332,6 +333,7 @@ public function initialize(array $config) { * - key * - api * - language (iso2: en, de, ja, ...) + * - libraries * * You can adds more after the URL like "&key=value&..." via * - query string array: additional query strings (e.g. callback for deferred execution - not supported yet by this helper) @@ -342,6 +344,10 @@ public function initialize(array $config) { public function apiUrl(array $query = []) { $url = $this->_protocol() . static::API; + if ($this->_runtimeConfig['libraries']) { + $libraries = is_array( $this->_runtimeConfig['libraries'] ) ? implode( ',', $this->_runtimeConfig['libraries'] ) : $this->_runtimeConfig['libraries']; + $query['libraries'] = $libraries; + } if ($this->_runtimeConfig['map']['api']) { $query['v'] = $this->_runtimeConfig['map']['api']; } diff --git a/src/View/Helper/GooglePlacesHelper.php b/src/View/Helper/GooglePlacesHelper.php new file mode 100644 index 00000000..182e6306 --- /dev/null +++ b/src/View/Helper/GooglePlacesHelper.php @@ -0,0 +1,114 @@ +input + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function input($fieldName, array $fieldOptions = [], array $googleOptions = []) { + return $this->control($fieldName, $fieldOptions, $googleOptions); + } + + /** + * This the initialization point of the script + * Returns the div container you can echo on the website + * + * @param string $fieldName name of input field + * @param array $fieldOptions associative array of settings are passed. Should be the same as uses on Form->control + * @param array $googleOptions associative array of settings for places.Autocomplete + * + * @return string divContainer + */ + public function control($fieldName, array $fieldOptions = [], array $googleOptions = []) { + $id = isset($fieldOptions['id']) && $fieldOptions['id'] != '' ? $fieldOptions['id'] : $fieldName; + + $html = $this->Form->control($fieldName, $fieldOptions); + $html .= $this->Form->hidden("{$fieldName}_lat", ['id' => "{$id}_lat"]); + $html .= $this->Form->hidden("{$fieldName}_lon", ['id' => "{$id}_lon"]); + + $html = $this->_script($id, $googleOptions) . $html; + + return $html; + } + + /** + * Inserts the required javascript code + * + * @param string $id the id of the input field + * @param array $options associative array of settings for places.Autocomplete + * + * @return string the scriptBlock for api + */ + protected function _script($id, $options = []) { + $api = ''; + // autoinclude js? + if ($this->_runtimeConfig['autoScript'] && !$this->_apiIncluded) { + $res = $this->Html->script($this->apiUrl(), ['block' => $this->_runtimeConfig['block']]); + $this->_apiIncluded = true; + + if (!$this->_runtimeConfig['block']) { + $api = $res . PHP_EOL; + } + // usually already included + //http://ajax.googleapis.com/ajax/libs/jquery/1/jquery.min.js + } + + $js = " + function initialize() { + var options = " . json_encode($options) . "; + var input = document.getElementById('" . $id . "'); + var hidden_lat = document.getElementById('" . $id . "_lat'); + var hidden_lon = document.getElementById('" . $id . "_lon'); + var autocomplete = new google.maps.places.Autocomplete(input, options); + + google.maps.event.addDomListener(input, 'keydown', function(event) { + if (event.keyCode === 13 && $('.pac-container:visible').length ) { + event.preventDefault(); + } + }); + autocomplete.addListener('place_changed', function() { + var place = autocomplete.getPlace(); + hidden_lat.value = place.geometry.location.lat() + hidden_lon.value = place.geometry.location.lng() + }); + + } + initialize(); + "; + + $script = 'jQuery(document).ready(function() {' . $js . '});'; + + $this->Html->scriptBlock($script, ['block' => true]); + + return $api; + } + +}