From ac371773d0faf7e3f923924d85d93cda47eab968 Mon Sep 17 00:00:00 2001 From: amercader Date: Sun, 15 Apr 2012 23:49:48 +0200 Subject: [PATCH 1/2] [map/view] Zoom to current features when updating the map view --- src/view-map.js | 42 +++++++++++++++++++++++++++++++++++++++--- 1 file changed, 39 insertions(+), 3 deletions(-) diff --git a/src/view-map.js b/src/view-map.js index 93f9d039..412dee7c 100644 --- a/src/view-map.js +++ b/src/view-map.js @@ -116,10 +116,18 @@ my.Map = Backbone.View.extend({ this.model.currentDocuments.bind('remove', function(doc){self.redraw('remove',doc)}); this.model.currentDocuments.bind('reset', function(){self.redraw('reset')}); - // If the div was hidden, Leaflet needs to recalculate some sizes - // to display properly this.bind('view:show',function(){ - self.map.invalidateSize(); + // If the div was hidden, Leaflet needs to recalculate some sizes + // to display properly + self.map.invalidateSize(); + if (self._zoomPending) { + self._zoomToFeatures(); + self._zoomPending = false; + } + self.visible = true; + }); + this.bind('view:hide',function(){ + self.visible = false; }); this.mapReady = false; @@ -190,6 +198,11 @@ my.Map = Backbone.View.extend({ this.features.clearLayers(); this._add(this.model.currentDocuments.models); } + if (this.visible){ + this._zoomToFeatures(); + } else { + this._zoomPending = true; + } } }, @@ -348,6 +361,18 @@ my.Map = Backbone.View.extend({ return null; }, + // Private: Zoom to map to current features extent if any, or to the full + // extent if none. + // + _zoomToFeatures: function(){ + var bounds = this.features.getBounds(); + if (bounds){ + this.map.fitBounds(bounds); + } else { + this.map.setView(new L.LatLng(0, 0), 2); + } + }, + // Private: Sets up the Leaflet map control and the features layer. // // The map uses a base layer from [MapQuest](http://www.mapquest.com) based @@ -372,6 +397,17 @@ my.Map = Backbone.View.extend({ } }); + + // This will be available in the next Leaflet stable release. + // In the meantime we add it manually to our layer. + this.features.getBounds = function(){ + var bounds = new L.LatLngBounds(); + this._iterateLayers(function (layer) { + bounds.extend(layer instanceof L.Marker ? layer.getLatLng() : layer.getBounds()); + }, this); + return (typeof bounds.getNorthEast() !== 'undefined') ? bounds : null; + } + this.map.addLayer(this.features); this.map.setView(new L.LatLng(0, 0), 2); From 1dbd8bf9579329e01bd71810da81a7984e2c8ac6 Mon Sep 17 00:00:00 2001 From: amercader Date: Mon, 16 Apr 2012 11:04:05 +0200 Subject: [PATCH 2/2] [view/map] Make auto zoom configurable --- css/map.css | 7 ++++++- src/view-map.js | 25 +++++++++++++++++++------ 2 files changed, 25 insertions(+), 7 deletions(-) diff --git a/css/map.css b/css/map.css index c8adde77..ed5edbd7 100644 --- a/css/map.css +++ b/css/map.css @@ -18,6 +18,11 @@ } .data-map-container .editor select { - width: 100%; + width: 100%; } +.data-map-container .editor .editor-options { + margin-top: 10px; + border-top: 1px solid gray; + padding: 5px 0; +} diff --git a/src/view-map.js b/src/view-map.js index 412dee7c..32b11e6a 100644 --- a/src/view-map.js +++ b/src/view-map.js @@ -75,6 +75,11 @@ my.Map = Backbone.View.extend({
\ \
\ +
\ + \ +
\ \ \ \ @@ -92,7 +97,8 @@ my.Map = Backbone.View.extend({ // Define here events for UI elements events: { 'click .editor-update-map': 'onEditorSubmit', - 'change .editor-field-type': 'onFieldTypeChange' + 'change .editor-field-type': 'onFieldTypeChange', + 'change #editor-auto-zoom': 'onAutoZoomChange' }, @@ -120,7 +126,7 @@ my.Map = Backbone.View.extend({ // If the div was hidden, Leaflet needs to recalculate some sizes // to display properly self.map.invalidateSize(); - if (self._zoomPending) { + if (self._zoomPending && self.autoZoom) { self._zoomToFeatures(); self._zoomPending = false; } @@ -130,6 +136,7 @@ my.Map = Backbone.View.extend({ self.visible = false; }); + this.autoZoom = true; this.mapReady = false; this.render(); @@ -198,10 +205,12 @@ my.Map = Backbone.View.extend({ this.features.clearLayers(); this._add(this.model.currentDocuments.models); } - if (this.visible){ - this._zoomToFeatures(); - } else { - this._zoomPending = true; + if (action != 'reset' && this.autoZoom){ + if (this.visible){ + this._zoomToFeatures(); + } else { + this._zoomPending = true; + } } } }, @@ -244,6 +253,10 @@ my.Map = Backbone.View.extend({ } }, + onAutoZoomChange: function(e){ + this.autoZoom = !this.autoZoom; + }, + // Private: Add one or n features to the map // // For each document passed, a GeoJSON geometry will be extracted and added