Skip to content

Commit 047d388

Browse files
author
André Gaul
committed
add MapView
1 parent d7f79bf commit 047d388

File tree

4 files changed

+64
-21
lines changed

4 files changed

+64
-21
lines changed

views/coarse.js

Lines changed: 10 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -7,9 +7,11 @@ var common = require('couchmap-common');
77
module.exports = Backbone.View.extend({
88
CoarseMarkerView: require('./coarseMarker'),
99
initialize: function(options) {
10-
this.mapView = options.mapView;
11-
this.bbox = common.bbox(this.mapView.map.getBounds());
12-
this.zoom = common.validate_zoom(this.mapView.map.getZoom()+1);
10+
this.proxyView = options.proxyView;
11+
var map = this.proxyView.mapView.map;
12+
13+
this.bbox = common.bbox(map.getBounds());
14+
this.zoom = common.validate_zoom(map.getZoom()+1);
1315
this.layer = L.markerClusterGroup(
1416
{
1517
showCoverageOnHover: false,
@@ -23,10 +25,10 @@ module.exports = Backbone.View.extend({
2325
return this.iconCreate(count);
2426
}.bind(this)
2527
})
26-
.addTo(this.mapView.map)
28+
.addTo(map)
2729
.on('click', function(a) {
2830
var model = a.layer.options.view.model;
29-
this.mapView.map.fitBounds([
31+
map.fitBounds([
3032
[model.get('bbox_south'), model.get('bbox_west')],
3133
[model.get('bbox_north'), model.get('bbox_east')]
3234
]);
@@ -39,7 +41,7 @@ module.exports = Backbone.View.extend({
3941
var east = _.max(_.map(models, function(model) { return model.get('bbox_east'); }));
4042
var south = _.min(_.map(models, function(model) { return model.get('bbox_south'); }));
4143
var north = _.max(_.map(models, function(model) { return model.get('bbox_north'); }));
42-
this.mapView.map.fitBounds([
44+
map.fitBounds([
4345
[south, west],
4446
[north, east]
4547
]);
@@ -48,7 +50,7 @@ module.exports = Backbone.View.extend({
4850

4951
this.markers = {};
5052

51-
this.listenTo(this.mapView, 'bbox', function(bbox, zoom) {
53+
this.listenTo(this.proxyView, 'bbox', function(bbox, zoom) {
5254
this.bbox = bbox;
5355
if (this.zoom!=zoom) {
5456
this.zoom = zoom;
@@ -103,6 +105,6 @@ module.exports = Backbone.View.extend({
103105
marker.remove();
104106
});
105107
this.markers = {};
106-
this.mapView.map.removeLayer(this.layer);
108+
this.proxyView.mapView.map.removeLayer(this.layer);
107109
}
108110
});

views/fine.js

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -6,8 +6,8 @@ var Lmarkercluster = require('leaflet-markercluster');
66
module.exports = Backbone.View.extend({
77
FineMarkerView: require('./fineMarker'),
88
initialize: function(options) {
9-
this.mapView = options.mapView;
10-
this.layer = L.markerClusterGroup().addTo(this.mapView.map);
9+
this.proxyView = options.proxyView;
10+
this.layer = L.markerClusterGroup().addTo(this.proxyView.mapView.map);
1111
this.subviews = {};
1212
this.listenTo(this.collection, 'sync', this.render);
1313
this.listenTo(this.collection, 'add', this.addModel);
@@ -32,7 +32,7 @@ module.exports = Backbone.View.extend({
3232
},
3333
remove: function() {
3434
_.each(this.subviews, this.removeModel, this);
35-
this.mapView.map.removeLayer(this.layer);
35+
this.proxyView.mapView.map.removeLayer(this.layer);
3636
}
3737
});
3838

views/map.js

Lines changed: 41 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,41 @@
1+
var Backbone = require('backbone');
2+
_ = require('underscore');
3+
var L = require('leaflet');
4+
5+
/* options:
6+
addDefaultLayer: optional (default: true)
7+
zoomTo: optional (default: 'world'), possible values:
8+
undefined: no zoom,
9+
'world': zoom to world bounds
10+
a bbox object can also be supplied
11+
*/
12+
module.exports = Backbone.View.extend({
13+
initialize: function(options) {
14+
this.map = L.map(this.el);
15+
options = _.extend({
16+
addDefaultLayer: true,
17+
zoomTo: 'world'
18+
}, options);
19+
// add default OpenStreetMap tile layer
20+
if (options.addDefaultLayer) {
21+
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
22+
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
23+
}).addTo(this.map);
24+
}
25+
// zoom
26+
if (options.zoomTo=='world') {
27+
this.map.fitWorld();
28+
}
29+
if (options.zoomTo) {
30+
if (_.has(options.zoomTo, 'toLeaflet')){
31+
this.map.fitBounds(options.zoom.toLeaflet());
32+
}
33+
if (_.isArray(options.zoomTo)) {
34+
this.map.fitBounds(options.zoomTo);
35+
}
36+
}
37+
},
38+
remove: function() {
39+
this.map.remove();
40+
}
41+
});

views/proxy.js

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -3,17 +3,17 @@ _ = require('underscore');
33
var L = require('leaflet');
44
var common = require('couchmap-common');
55

6+
/* options:
7+
mapView: mandatory, the mapView (mapView.map has to be the leaflet object)
8+
*/
69
module.exports = Backbone.View.extend({
710
initialize: function(options) {
11+
this.options = options;
12+
this.mapView = options.mapView;
813
this.CoarseView = options.CoarseView || require('./coarse');
914
this.FineView = options.FineView || require('./fine');
1015

11-
// create map and add OpenStreetMap tile layer
12-
this.map = L.map(this.el, {center: [10, 0], zoom: 2} );
13-
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
14-
attribution: '&copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
15-
}).addTo(this.map);
16-
this.map.on('moveend', this.update_bbox, this);
16+
this.mapView.map.on('moveend', this.update_bbox, this);
1717

1818
this.listenTo(this.model, {
1919
'fine': this.render.bind(this, 'fine'),
@@ -22,8 +22,8 @@ module.exports = Backbone.View.extend({
2222
this.update_bbox();
2323
},
2424
update_bbox: function() {
25-
var bbox = common.bbox(this.map.getBounds());
26-
var zoom = common.validate_zoom(this.map.getZoom()+1);
25+
var bbox = common.bbox(this.mapView.map.getBounds());
26+
var zoom = common.validate_zoom(this.mapView.map.getZoom()+1);
2727
this.trigger('bbox', bbox, zoom);
2828
this.model.update(bbox, zoom);
2929
},
@@ -35,12 +35,12 @@ module.exports = Backbone.View.extend({
3535
}
3636
if (this.mode=='coarse') {
3737
this.subview = new this.CoarseView(_.extend(this.coarse_options || {}, {
38-
mapView: this,
38+
proxyView: this,
3939
collection: this.model.get('coarse_coll')
4040
}));
4141
} else {
4242
this.subview = new this.FineView(_.extend(this.fine_options || {}, {
43-
mapView: this,
43+
proxyView: this,
4444
collection: this.model.get('fine_coll')
4545
}));
4646
}

0 commit comments

Comments
 (0)