Skip to content

Commit d6c7df6

Browse files
committed
Merge pull request BetterBrief#30 from TimeZoneOne/master
Save bounds to database
2 parents 6e2a3e3 + 512f9d7 commit d6c7df6

File tree

3 files changed

+35
-5
lines changed

3 files changed

+35
-5
lines changed

_config/googlemapfield.yml

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ GoogleMapField:
99
Latitude: 'Latitude'
1010
Longitude: 'Longitude'
1111
Zoom: 'Zoom'
12+
Bounds: 'Bounds'
1213
map:
1314
zoom: 14
1415
default_field_values:

code/GoogleMapField.php

Lines changed: 16 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,11 @@ class GoogleMapField extends FormField {
2727
* @var FormField
2828
*/
2929
protected $zoomField;
30+
31+
/**
32+
* @var FormField
33+
*/
34+
protected $boundsField;
3035

3136
/**
3237
* The merged version of the default and user specified options
@@ -104,11 +109,16 @@ public function setupChildren() {
104109
'Zoom',
105110
$this->recordFieldData('Zoom')
106111
)->addExtraClass('googlemapfield-zoomfield');
107-
112+
$this->boundsField = HiddenField::create(
113+
$name.'[Bounds]',
114+
'Bounds',
115+
$this->recordFieldData('Bounds')
116+
)->addExtraClass('googlemapfield-boundsfield');
108117
$this->children = new FieldList(
109118
$this->latField,
110119
$this->lngField,
111-
$this->zoomField
120+
$this->zoomField,
121+
$this->boundsField
112122
);
113123

114124
if($this->options['show_search_box']) {
@@ -174,6 +184,9 @@ public function setValue($record) {
174184
$this->zoomField->setValue(
175185
$record['Zoom']
176186
);
187+
$this->boundsField->setValue(
188+
$record['Bounds']
189+
);
177190
return $this;
178191
}
179192

@@ -185,6 +198,7 @@ public function saveInto(DataObjectInterface $record) {
185198
$record->setCastedField($this->childFieldName('Latitude'), $this->latField->dataValue());
186199
$record->setCastedField($this->childFieldName('Longitude'), $this->lngField->dataValue());
187200
$record->setCastedField($this->childFieldName('Zoom'), $this->zoomField->dataValue());
201+
$record->setCastedField($this->childFieldName('Bounds'), $this->boundsField->dataValue());
188202
return $this;
189203
}
190204

javascript/GoogleMapField.js

Lines changed: 18 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -32,6 +32,7 @@
3232
latField = field.find('.googlemapfield-latfield'),
3333
lngField = field.find('.googlemapfield-lngfield'),
3434
zoomField = field.find('.googlemapfield-zoomfield'),
35+
boundsField = field.find('.googlemapfield-boundsfield'),
3536
search = field.find('.googlemapfield-searchfield');
3637

3738
// Update the hidden fields and mark as changed
@@ -43,6 +44,7 @@
4344

4445
latField.val(latCoord);
4546
lngField.val(lngCoord);
47+
updateBounds();
4648

4749
if (!init) {
4850
// Mark as changed(?)
@@ -53,6 +55,16 @@
5355
function updateZoom() {
5456
zoomField.val(map.getZoom());
5557
}
58+
59+
function updateBounds() {
60+
var bounds = JSON.stringify(map.getBounds().toJSON());
61+
boundsField.val(bounds);
62+
}
63+
64+
function zoomChanged() {
65+
updateZoom();
66+
updateBounds();
67+
}
5668

5769
function centreOnMarker() {
5870
var center = marker.getPosition();
@@ -63,7 +75,7 @@
6375
function mapClicked(ev) {
6476
var center = ev.latLng;
6577
marker.setPosition(center);
66-
updateField(center);
78+
centreOnMarker();
6779
}
6880

6981
function geoSearchComplete(result, status) {
@@ -87,13 +99,16 @@
8799
}
88100

89101
// Populate the fields to the current centre
90-
updateField(map.getCenter(), true);
102+
google.maps.event.addListenerOnce(map, 'idle', function(){
103+
updateField(map.getCenter(), true);
104+
updateZoom();
105+
});
91106

92107
google.maps.event.addListener(marker, 'dragend', centreOnMarker);
93108

94109
google.maps.event.addListener(map, 'click', mapClicked);
95110

96-
google.maps.event.addListener(map, 'zoom_changed', updateZoom);
111+
google.maps.event.addListener(map, 'zoom_changed', zoomChanged);
97112

98113
search.on({
99114
'change': searchReady,

0 commit comments

Comments
 (0)