Skip to content

add google places helper #36

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions src/View/Helper/GoogleMapHelper.php
Original file line number Diff line number Diff line change
Expand Up @@ -237,6 +237,7 @@ class GoogleMapHelper extends Helper {
'localImages' => false,
'https' => null, // auto detect
'key' => null,
'libraries' => null,
];

/**
Expand Down Expand Up @@ -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)
Expand All @@ -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'];
}
Expand Down
114 changes: 114 additions & 0 deletions src/View/Helper/GooglePlacesHelper.php
Original file line number Diff line number Diff line change
@@ -0,0 +1,114 @@
<?php

namespace Geo\View\Helper;

/**
* This is a CakePHP helper that helps users to integrate Google Places
* into their application by only writing PHP code. This helper depends on jQuery.
*
* CodeAPI: http://code.google.com/intl/de-DE/apis/maps/documentation/javascript/basics.html
* Icons/Images: http://gmapicons.googlepages.com/home
*
* @author Michael Heit
* @link http://www.dereuromark.de/2010/12/21/googlemapsv3-cakephp-helper/
* @license MIT License (http://www.opensource.org/licenses/mit-license.php)
* @property \Cake\View\Helper\HtmlHelper $Html
* @property \Cake\View\Helper\FormHelper $Form
*/
class GooglePlacesHelper extends GoogleMapHelper {

/**
* Needed helpers
*
* @var array
*/
public $helpers = ['Form', 'Html'];

/**
* Wrapper function for control like Form->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;
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

!empty(fieldOptions['id']) is shorter than both checks. 0 is not a valid id anyway right?


$html = $this->Form->control($fieldName, $fieldOptions);
$html .= $this->Form->hidden("{$fieldName}_lat", ['id' => "{$id}_lat"]);
$html .= $this->Form->hidden("{$fieldName}_lon", ['id' => "{$id}_lon"]);
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

lng is usually used for longitude shortform in this plugin.


$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;
}

}