diff --git a/src/view.map.js b/src/view.map.js
index cd69bf34..c43aaaa8 100644
--- a/src/view.map.js
+++ b/src/view.map.js
@@ -53,12 +53,22 @@ my.Map = Backbone.View.extend({
geomField: null,
lonField: null,
latField: null,
- autoZoom: true
+ autoZoom: true,
+ cluster: true
},
options.state
);
this.state = new recline.Model.ObjectState(stateData);
+ this._clusterOptions = {
+ zoomToBoundsOnClick: true,
+ //disableClusteringAtZoom: 10,
+ maxClusterRadius: 80,
+ singleMarkerMode: false,
+ skipDuplicateAddTesting: true,
+ animateAddingMarkers: false
+ };
+
// Listen to changes in the fields
this.model.fields.bind('change', function() {
self._setupGeometryField();
@@ -93,7 +103,7 @@ my.Map = Backbone.View.extend({
// ### infobox
//
// Function to create infoboxes used in popups. The default behaviour is very simple and just lists all attributes.
- //
+ //
// Users should override this function to customize behaviour i.e.
//
// view = new View({...});
@@ -146,8 +156,11 @@ my.Map = Backbone.View.extend({
}
if (this._geomReady() && this.mapReady){
- if (action == 'reset' || action == 'refresh'){
+ if (action == 'refresh' || action == 'reset') {
this.features.clearLayers();
+ // recreate cluster group because of issues with clearLayer
+ this.map.removeLayer(this.markers);
+ this.markers = new L.MarkerClusterGroup(this._clusterOptions);
this._add(this.model.records.models);
} else if (action == 'add' && doc){
this._add(doc);
@@ -161,6 +174,13 @@ my.Map = Backbone.View.extend({
this._zoomPending = true;
}
}
+ this.map.removeLayer(this.features);
+ this.map.removeLayer(this.markers);
+ if (this.state.get('cluster')) {
+ this.map.addLayer(this.markers);
+ } else {
+ this.map.addLayer(this.features);
+ }
}
},
@@ -198,6 +218,9 @@ my.Map = Backbone.View.extend({
if (!(docs instanceof Array)) docs = [docs];
+ // removing ad re-adding the layer enables faster bulk loading
+ self.map.removeLayer(self.markers);
+
var count = 0;
var wrongSoFar = 0;
_.every(docs, function(doc){
@@ -236,9 +259,11 @@ my.Map = Backbone.View.extend({
}
return true;
});
+
+ self.map.addLayer(self.markers);
},
- // Private: Remove one or n features to the map
+ // Private: Remove one or n features from the map
//
_remove: function(docs){
@@ -347,7 +372,7 @@ my.Map = Backbone.View.extend({
//
_zoomToFeatures: function(){
var bounds = this.features.getBounds();
- if (bounds.getNorthEast()){
+ if (bounds && bounds.getNorthEast() && bounds.getSouthWest()){
this.map.fitBounds(bounds);
} else {
this.map.setView([0, 0], 2);
@@ -360,15 +385,27 @@ my.Map = Backbone.View.extend({
// on [OpenStreetMap](http://openstreetmap.org).
//
_setupMap: function(){
+ var self = this;
this.map = new L.Map(this.$map.get(0));
var mapUrl = "http://otile{s}.mqcdn.com/tiles/1.0.0/osm/{z}/{x}/{y}.png";
var osmAttribution = 'Map data © 2011 OpenStreetMap contributors, Tiles Courtesy of MapQuest
';
- var bg = new L.TileLayer(mapUrl, {maxZoom: 18, attribution: osmAttribution ,subdomains: '1234'});
- this.map.addLayer(bg);
+ var bg = new L.TileLayer(mapUrl, {maxZoom: 18, attribution: osmAttribution ,subdomains: '1234'}).addTo(this.map);
- this.features = new L.GeoJSON();
- this.map.addLayer(this.features);
+ this.markers = new L.MarkerClusterGroup(this._clusterOptions);
+
+ m = this.map;
+ l = this.markers;
+
+ this.features = new L.GeoJSON(null,{
+ pointToLayer: function (feature, latlng) {
+ var marker = new L.marker(latlng);
+ self.markers.addLayer(marker);
+ return marker;
+ }
+ });
+ //this.map.addLayer(this.features);
+ //this.map.addLayer(this.markers);
this.map.setView([0, 0], 2);
@@ -441,19 +478,23 @@ my.MapMenu = Backbone.View.extend({
\