Skip to content

Commit c48603f

Browse files
committed
Use SubGroup to handle multiple clustered realtime layers
1 parent 81c23fd commit c48603f

File tree

2 files changed

+27
-13
lines changed

2 files changed

+27
-13
lines changed

examples/earthquakes.html

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919

2020
<script src="https://unpkg.com/[email protected]/dist/leaflet-src.js"></script>
2121
<script src="https://unpkg.com/[email protected]/dist/leaflet.markercluster-src.js"></script>
22+
<script src="https://unpkg.com/leaflet.featuregroup.subgroup"></script>
2223
<script src="../dist/leaflet-realtime.js"></script>
2324
<script src="earthquakes.js"></script>
2425
</body>

examples/earthquakes.js

+26-13
Original file line numberDiff line numberDiff line change
@@ -1,25 +1,38 @@
1-
var map = L.map('map'),
2-
realtime = L.realtime('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson', {
1+
function createRealtimeLayer(url, container) {
2+
return L.realtime(url, {
33
interval: 60 * 1000,
44
getFeatureId: function(f) {
5-
return f.properties.url;
5+
return f.properties.url;
66
},
77
cache: true,
8-
container: L.markerClusterGroup(),
8+
container: container,
99
onEachFeature(f, l) {
10-
l.bindPopup(function() {
11-
return '<h3>' + f.properties.place + '</h3>' +
12-
'<p>' + new Date(f.properties.time) +
13-
'<br/>Magnitude: <strong>' + f.properties.mag + '</strong></p>' +
14-
'<p><a href="' + f.properties.url + '">More information</a></p>';
15-
});
10+
l.bindPopup(function() {
11+
return '<h3>' + f.properties.place + '</h3>' +
12+
'<p>' + new Date(f.properties.time) +
13+
'<br/>Magnitude: <strong>' + f.properties.mag + '</strong></p>' +
14+
'<p><a href="' + f.properties.url + '">More information</a></p>';
15+
});
1616
}
17-
}).addTo(map);
17+
});
18+
}
19+
20+
var map = L.map('map'),
21+
clusterGroup = L.markerClusterGroup().addTo(map),
22+
subgroup1 = L.featureGroup.subGroup(clusterGroup),
23+
subgroup2 = L.featureGroup.subGroup(clusterGroup),
24+
realtime1 = createRealtimeLayer('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson', subgroup1).addTo(map),
25+
realtime2 = createRealtimeLayer('https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/all_hour.geojson', subgroup2);
1826

1927
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png', {
2028
attribution: '&copy; <a href="https://earthquake.usgs.gov/earthquakes/feed/v1.0/geojson.php">USGS Earthquake Hazards Program</a>, &copy; <a href="http://osm.org/copyright">OpenStreetMap</a> contributors'
2129
}).addTo(map);
2230

23-
realtime.once('update', function() {
24-
map.fitBounds(realtime.getBounds(), {maxZoom: 3});
31+
L.control.layers(null, {
32+
'Earthquakes 2.5+': realtime1,
33+
'All Earthquakes': realtime2
34+
}).addTo(map);
35+
36+
realtime1.once('update', function() {
37+
map.fitBounds(realtime1.getBounds(), {maxZoom: 3});
2538
});

0 commit comments

Comments
 (0)