Skip to content

Commit dc70973

Browse files
committed
Add places autocomplete
1 parent a67e22a commit dc70973

File tree

1 file changed

+45
-24
lines changed

1 file changed

+45
-24
lines changed

Map.php

Lines changed: 45 additions & 24 deletions
Original file line numberDiff line numberDiff line change
@@ -543,7 +543,7 @@ class Map {
543543
/**
544544
* Clustering javascript
545545
*
546-
* This will hold the location of the file
546+
* This will hold the location of the clustering file
547547
*
548548
* @var string
549549
*/
@@ -557,24 +557,25 @@ class Map {
557557
private $clustering_options = array();
558558

559559
/**
560-
* Places flag, places required for Autocomplete
560+
* Places library flag
561+
*
561562
* @var bool
562563
*/
563564
private $places = false;
564565

565566
/**
566-
* Places input id
567+
* Autocomplete input id
567568
*
568-
* This will hold the id of the input to bind the Autocomplete to
569+
* This is the id of the text input that will be used to search places
569570
*
570571
* @var string
571572
*/
572-
private $autocomplete_id;
573+
private $autocomplete_input_id = null;
573574

574575
/**
575-
* AUtocomplete options
576+
* Autocomplete options
576577
*
577-
* array of options to be passed to the Autocomplete constructor
578+
* Array of options to be passed to the autocomplete constructor
578579
*
579580
* @var array
580581
*/
@@ -686,8 +687,8 @@ public function __construct( array $options=null ) {
686687
case 'compress_output':
687688
$option_val ? $this->enableCompressedOutput() : $this->disableCompressedOutput();
688689
break;
689-
case 'autocomplete':
690-
$this->enablePlaces($option_val);
690+
case 'places_autocomplete':
691+
$this->enablePlacesAutocomplete( $option_val );
691692
break;
692693

693694
}
@@ -2162,13 +2163,14 @@ function getMapJS() {
21622163
);
21632164
}
21642165

2165-
if ( $this->autocomplete_id )
2166+
if ( $this->autocomplete_input_id )
21662167
{
2167-
$output .= <<<EEE
2168-
this.ac_input = document.getElementById('{$this->autocomplete_id}');
2169-
this.ac_options = {$this->phpToJs($this->autocomplete_options)};
2170-
this.autocomplete = new google.maps.places.Autocomplete(this.ac_input, this.ac_options);
2171-
EEE;
2168+
$output .= sprintf(
2169+
"\tthis.autocomplete_input = document.getElementById('%s');\n\tthis.autocomplete_options = %s;\n\tthis.autocomplete = new google.maps.places.Autocomplete(this.autocomplete_input, this.autocomplete_options);\n",
2170+
$this->autocomplete_input_id,
2171+
$this->phpToJs( $this->autocomplete_options )
2172+
);
2173+
21722174
}
21732175

21742176
if ( $this->traffic_layer ) {
@@ -2424,20 +2426,39 @@ function enableAdsense( $publisher_id, $format = null, $position = null, $visibl
24242426
$this->adsense_visible = $visible;
24252427
}
24262428

2427-
function enableClustering( $clustering_js_file, $options = null ) {
2429+
/**
2430+
* Enable clusering
2431+
*
2432+
* Enables marker clustering
2433+
*
2434+
* @link https://developers.google.com/maps/articles/toomanymarkers
2435+
*
2436+
* Verified to work with marker clusterer
2437+
* @link http://google-maps-utility-library-v3.googlecode.com/svn/trunk/markerclusterer/
2438+
*
2439+
* @param string $clustering_js_file Location to the clustering file
2440+
* @param array $options Clustering options
2441+
*/
2442+
function enableClustering( $clustering_js_file, array $options = null ) {
24282443
$this->clustering_js = $clustering_js_file;
24292444
$this->clustering_options = $options;
24302445
}
24312446

2432-
function enablePlaces($options = array())
2433-
{
2434-
if (!$this->places) $this->libraries[] = 'places';
2447+
/**
2448+
* Enable Places
2449+
*
2450+
* Add an input on the page
2451+
*
2452+
* autocomplete_input_id is a required key if you want to use an autocomplete
2453+
*
2454+
* @param array $options array of places options
2455+
*/
2456+
function enablePlacesAutocomplete( array $options = array() ) {
2457+
$this->libraries[] = 'places';
24352458
$this->places = true;
2436-
2437-
if (isset($options["id"]))
2438-
{
2439-
$this->autocomplete_id = $options["id"];
2440-
unset($options["id"]);
2459+
if ( isset( $options["autocomplete_input_id"] ) ) {
2460+
$this->autocomplete_input_id = $options["autocomplete_input_id"];
2461+
unset( $options["autocomplete_input_id"] );
24412462
$this->autocomplete_options = $options;
24422463
}
24432464
}

0 commit comments

Comments
 (0)