Skip to content

Commit 33ab739

Browse files
committed
Add markercluster library
Signed-off-by: Cintia Sánchez García <[email protected]>
1 parent dc92201 commit 33ab739

File tree

2 files changed

+21
-13
lines changed

2 files changed

+21
-13
lines changed

ocg-server/static/js/community/explore/map.js

Lines changed: 18 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -17,11 +17,21 @@ export class Map {
1717
script.src = "https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.9.4/leaflet.js";
1818
document.getElementsByTagName("head")[0].appendChild(script);
1919

20+
// Prepare markercluster script
21+
let markerClusterScript = document.createElement("script");
22+
markerClusterScript.type = "text/javascript";
23+
markerClusterScript.src = "https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.5.3/leaflet.markercluster.js";
24+
2025
this.entity = entity;
2126
this.enabledMoveEnd = false;
2227

23-
// Setup map after script is loaded
28+
// Load markercluster library after LeafletJS is loaded
2429
script.onload = () => {
30+
document.getElementsByTagName("head")[0].appendChild(markerClusterScript);
31+
};
32+
33+
// Setup map after scripts are loaded
34+
markerClusterScript.onload = () => {
2535
this.setup(data);
2636
};
2737

@@ -37,9 +47,6 @@ export class Map {
3747
zoomControl: false,
3848
});
3949

40-
// Create a layer group to add markers
41-
this.layerGroup = L.layerGroup();
42-
4350
// Add zoom control to the map on the top right
4451
L.control
4552
.zoom({
@@ -95,11 +102,6 @@ export class Map {
95102
data = await this.fetchData(overwriteBounds);
96103
}
97104

98-
// Clear previous markers for the layer group
99-
if (this.map.hasLayer(this.layerGroup)) {
100-
this.layerGroup.clearLayers();
101-
}
102-
103105
if (data) {
104106
// Get items from data
105107
let items = [];
@@ -166,6 +168,9 @@ export class Map {
166168
popupAnchor: [0, -25],
167169
};
168170

171+
// Create marker cluster group
172+
const markers = window.L.markerClusterGroup();
173+
169174
// Add markers
170175
items.forEach((item) => {
171176
// Skip items without coordinates
@@ -209,11 +214,11 @@ export class Map {
209214
});
210215
}
211216

212-
// Add marker to layer group
213-
this.layerGroup.addLayer(marker);
217+
// Add marker to the marker cluster group
218+
markers.addLayer(marker);
214219

215-
// Add layer group to the map
216-
this.map.addLayer(this.layerGroup);
220+
// Add marker cluster group to the map
221+
this.map.addLayer(markers);
217222

218223
// TODO - Open details on click
219224
marker.on("click", () => {

ocg-server/templates/community/base.html

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,9 @@
1919
href="https://unpkg.com/[email protected]/dist/leaflet.css"
2020
integrity="sha256-p4NxAoJBhIIN+hmNHrzRCf9tD/miZyoHS5obTRR9BMY="
2121
crossorigin="" />
22+
<link rel="stylesheet"
23+
href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.markercluster/1.5.3/MarkerCluster.Default.min.css"
24+
crossorigin="" />
2225
<link rel="stylesheet" href="/static/styles/index.css" />
2326

2427
<script type="text/javascript"

0 commit comments

Comments
 (0)