Skip to content
This repository has been archived by the owner on Jul 27, 2021. It is now read-only.

Commit

Permalink
Merge pull request #30 from TimeZoneOne/master
Browse files Browse the repository at this point in the history
Save bounds to database
  • Loading branch information
willmorgan committed Mar 20, 2016
2 parents 6e2a3e3 + 512f9d7 commit d6c7df6
Show file tree
Hide file tree
Showing 3 changed files with 35 additions and 5 deletions.
1 change: 1 addition & 0 deletions _config/googlemapfield.yml
Original file line number Diff line number Diff line change
Expand Up @@ -9,6 +9,7 @@ GoogleMapField:
Latitude: 'Latitude'
Longitude: 'Longitude'
Zoom: 'Zoom'
Bounds: 'Bounds'
map:
zoom: 14
default_field_values:
Expand Down
18 changes: 16 additions & 2 deletions code/GoogleMapField.php
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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']) {
Expand Down Expand Up @@ -174,6 +184,9 @@ public function setValue($record) {
$this->zoomField->setValue(
$record['Zoom']
);
$this->boundsField->setValue(
$record['Bounds']
);
return $this;
}

Expand All @@ -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;
}

Expand Down
21 changes: 18 additions & 3 deletions javascript/GoogleMapField.js
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -43,6 +44,7 @@

latField.val(latCoord);
lngField.val(lngCoord);
updateBounds();

if (!init) {
// Mark as changed(?)
Expand All @@ -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();
Expand All @@ -63,7 +75,7 @@
function mapClicked(ev) {
var center = ev.latLng;
marker.setPosition(center);
updateField(center);
centreOnMarker();
}

function geoSearchComplete(result, status) {
Expand All @@ -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,
Expand Down

0 comments on commit d6c7df6

Please sign in to comment.