From 512f9d7d6d856bb0fc809ad4a715715d64426554 Mon Sep 17 00:00:00 2001 From: Shawn Lin Date: Fri, 4 Mar 2016 14:08:39 +1300 Subject: [PATCH] Save map bounds --- _config/googlemapfield.yml | 1 + code/GoogleMapField.php | 18 ++++++++++++++++-- javascript/GoogleMapField.js | 21 ++++++++++++++++++--- 3 files changed, 35 insertions(+), 5 deletions(-) diff --git a/_config/googlemapfield.yml b/_config/googlemapfield.yml index 85b570a..4bdcbe1 100644 --- a/_config/googlemapfield.yml +++ b/_config/googlemapfield.yml @@ -9,6 +9,7 @@ GoogleMapField: Latitude: 'Latitude' Longitude: 'Longitude' Zoom: 'Zoom' + Bounds: 'Bounds' map: zoom: 14 default_field_values: diff --git a/code/GoogleMapField.php b/code/GoogleMapField.php index 58c0759..1f597d9 100644 --- a/code/GoogleMapField.php +++ b/code/GoogleMapField.php @@ -27,6 +27,11 @@ class GoogleMapField extends FormField { * @var FormField */ protected $zoomField; + + /** + * @var FormField + */ + protected $boundsField; /** * The merged version of the default and user specified options @@ -104,11 +109,16 @@ public function setupChildren() { 'Zoom', $this->recordFieldData('Zoom') )->addExtraClass('googlemapfield-zoomfield'); - + $this->boundsField = HiddenField::create( + $name.'[Bounds]', + 'Bounds', + $this->recordFieldData('Bounds') + )->addExtraClass('googlemapfield-boundsfield'); $this->children = new FieldList( $this->latField, $this->lngField, - $this->zoomField + $this->zoomField, + $this->boundsField ); if($this->options['show_search_box']) { @@ -174,6 +184,9 @@ public function setValue($record) { $this->zoomField->setValue( $record['Zoom'] ); + $this->boundsField->setValue( + $record['Bounds'] + ); return $this; } @@ -185,6 +198,7 @@ public function saveInto(DataObjectInterface $record) { $record->setCastedField($this->childFieldName('Latitude'), $this->latField->dataValue()); $record->setCastedField($this->childFieldName('Longitude'), $this->lngField->dataValue()); $record->setCastedField($this->childFieldName('Zoom'), $this->zoomField->dataValue()); + $record->setCastedField($this->childFieldName('Bounds'), $this->boundsField->dataValue()); return $this; } diff --git a/javascript/GoogleMapField.js b/javascript/GoogleMapField.js index ac745d8..40b1dc1 100644 --- a/javascript/GoogleMapField.js +++ b/javascript/GoogleMapField.js @@ -32,6 +32,7 @@ latField = field.find('.googlemapfield-latfield'), lngField = field.find('.googlemapfield-lngfield'), zoomField = field.find('.googlemapfield-zoomfield'), + boundsField = field.find('.googlemapfield-boundsfield'), search = field.find('.googlemapfield-searchfield'); // Update the hidden fields and mark as changed @@ -43,6 +44,7 @@ latField.val(latCoord); lngField.val(lngCoord); + updateBounds(); if (!init) { // Mark as changed(?) @@ -53,6 +55,16 @@ function updateZoom() { zoomField.val(map.getZoom()); } + + function updateBounds() { + var bounds = JSON.stringify(map.getBounds().toJSON()); + boundsField.val(bounds); + } + + function zoomChanged() { + updateZoom(); + updateBounds(); + } function centreOnMarker() { var center = marker.getPosition(); @@ -63,7 +75,7 @@ function mapClicked(ev) { var center = ev.latLng; marker.setPosition(center); - updateField(center); + centreOnMarker(); } function geoSearchComplete(result, status) { @@ -87,13 +99,16 @@ } // Populate the fields to the current centre - updateField(map.getCenter(), true); + google.maps.event.addListenerOnce(map, 'idle', function(){ + updateField(map.getCenter(), true); + updateZoom(); + }); google.maps.event.addListener(marker, 'dragend', centreOnMarker); google.maps.event.addListener(map, 'click', mapClicked); - google.maps.event.addListener(map, 'zoom_changed', updateZoom); + google.maps.event.addListener(map, 'zoom_changed', zoomChanged); search.on({ 'change': searchReady,