I have a DC.JSc Charts which are filtering my icons on my Leaflet Map. Basically, when I’m filtering I want my map to zoom on my selected icons.
JavaScript
x
24
24
1
var onFilt = function(chart, filter) {
2
updateMap(locations.top(Infinity));
3
};
4
5
// Updates the displayed map markers to reflect the crossfilter dimension passed in
6
var updateMap = function(locs) {
7
// clear the existing markers from the map
8
markersLayer.clearLayers();
9
clusterLayer.clearLayers();
10
11
locs.forEach(function(d, i) {
12
13
if (d.latitude != null && d.latitude != undefined) {
14
// add a Leaflet marker for the lat lng and insert the application's stated purpose in popup
15
16
var mark = L.marker([d.latitude, d.longitude]);
17
markersLayer.addLayer(mark);
18
clusterLayer.addLayer(mark);
19
20
map.getBounds();
21
}
22
});
23
};
24
I’ve tried:
JavaScript
1
6
1
map.getBounds(); //No response
2
3
L.markersLayer.getBounds(); //SCRIPT5007: Unable to get property 'getBounds' of undefined or null reference
4
5
map.fitBounds(markersLayer.getBounds()); // Object doesn't support property or method 'getBounds'
6
Also tried :
JavaScript
1
10
10
1
if (d.latitude != null && d.latitude != undefined) {
2
d.ll = L.latLng(d.latitude, d.longitude);
3
var mark = L.marker([d.latitude, d.longitude]);
4
markersLayer.addLayer(mark);
5
clusterLayer.addLayer(mark);
6
};
7
map.addLayer(markersLayer);
8
map.fitBounds(markersLayer.getBounds());
9
});
10
Error: Object doesn’t support property or method ‘getBounds’
Any ideas?
Found my own solution: map.fitBounds(clusterLayer.getBounds());
Advertisement
Answer
You’re almost there, but have several mistakes: you’re trying to getBounds
within forEach
loop, you’re trying to getBounds
from the wrong object.
Please see and run the snippet below click FILTER
button, read comments in JS
code.
I omitted the filtering part, left zooming only:
JavaScript
1
62
62
1
// add a map
2
var map = L.map('mapid').setView([51.505, -0.09], 12);
3
L.tileLayer('https://api.mapbox.com/styles/v1/{id}/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoibWFwYm94IiwiYSI6ImNpejY4NXVycTA2emYycXBndHRqcmZ3N3gifQ.rJcFIG214AriISLbB6B5aw', {
4
maxZoom: 18,
5
attribution: 'Map data © <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors, ' +
6
'Imagery © <a href="https://www.mapbox.com/">Mapbox</a>',
7
id: 'mapbox/streets-v11',
8
tileSize: 512,
9
zoomOffset: -1
10
}).addTo(map);
11
12
// Assuming that locations are filtered already:
13
var locations = [
14
{latitude: 51.5, longitude: -0.09},
15
{latitude: 51.53, longitude: -0.19},
16
{latitude: 51.45, longitude: 0},
17
{latitude: 51.56, longitude: 0.09}
18
];
19
20
// Updates the displayed map markers to reflect the crossfilter dimension passed in
21
var updateMap = function(locs) {
22
// clear the existing markers from the map
23
//markersLayer.clearLayers();
24
//clusterLayer.clearLayers();
25
26
var minlat = 200, minlon = 200, maxlat = -200, maxlon = -200;
27
28
locs.forEach(function(d, i) {
29
30
if (d.latitude != null && d.latitude != undefined) {
31
// add a Leaflet marker for the lat lng and insert the application's stated purpose in popup
32
//var mark = L.marker([d.latitude, d.longitude]);
33
//markersLayer.addLayer(mark);
34
//clusterLayer.addLayer(mark);
35
36
// find corners
37
if (minlat > d.latitude) minlat = d.latitude;
38
if (minlon > d.longitude) minlon = d.longitude;
39
if (maxlat < d.latitude) maxlat = d.latitude;
40
if (maxlon < d.longitude) maxlon = d.longitude;
41
42
// set markers
43
L.marker([d.latitude, d.longitude]).addTo(map);
44
}
45
});
46
47
c1 = L.latLng(minlat, minlon);
48
c2 = L.latLng(maxlat, maxlon);
49
50
// fit bounds
51
map.fitBounds(L.latLngBounds(c1, c2));
52
53
// correct zoom to fit markers
54
setTimeout(function() {
55
map.setZoom(map.getZoom() - 1);
56
}, 500);
57
58
};
59
60
function filtr() {
61
updateMap(locations);
62
};
JavaScript
1
3
1
#mapid {
2
height: 180px;
3
}
JavaScript
1
7
1
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.3.4/dist/leaflet.css" integrity="sha512-puBpdR0798OZvTTbP4A8Ix/l+A4dHDD0DGqYW6RQ+9jxkRFclaxxQb/SJAWZfWAkuyeQUytO7+7N4QKrDh+drA==" crossorigin="" />
2
3
<script src="https://unpkg.com/leaflet@1.3.4/dist/leaflet.js" integrity="sha512-nMMmRyTVoLYqjP9hrbed9S+FzjZHW5gY1TWCHA5ckwXZBadntCNs8kEqAWdrb9O7rxbCaA4lKTIWjDXZxflOcA==" crossorigin=""></script>
4
5
<button onclick="filtr()">FILTER</button>
6
7
<div id="mapid"></div>