Debian Codegraph @

example/old-bugs/disappearing-marker-from-spider.html

raw
1 <!DOCTYPE html>
2 <html>
3 <head>
4 <title>Leaflet debug page</title>
5
6 <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" integrity="sha512-07I2e+7D8p6he1SIM+1twR5TIrhUQn9+I6yjqD53JQjFiMf8EtC93ty0/5vJTZGF8aAocvHYNEDJajGdNx1IsQ==" crossorigin="" />
7 <script src="https://unpkg.com/[email protected]/dist/leaflet-src.js" integrity="sha512-WXoSHqw/t26DszhdMhOXOkI7qCiv5QWXhH9R7CgvgZMHz1ImlkVQ3uNsiQKu5wwbbxtPzFXd1hK4tzno2VqhpA==" crossorigin=""></script>
8 <meta name="viewport" content="width=device-width, initial-scale=1.0">
9 <link rel="stylesheet" href="../screen.css" />
10
11 <link rel="stylesheet" href="../../dist/MarkerCluster.css" />
12 <link rel="stylesheet" href="../../dist/MarkerCluster.Default.css" />
13 <script src="../../dist/leaflet.markercluster-src.js"></script>
14 </head>
15 <body style="font-family: verdana; font-size: 80%;">
16
17 <div id="map"></div>
18 Click on the cluster to <strong>spiderfy</strong> and then <button id="moveTrain">move train</button><br/>
19 <br/>
20 <div style="color: #888;"><strong>Note:</strong> The marker on the old cluster position comes back on next move or on map scrolling.</div>
21
22 <script type="text/javascript">
23
24 var stationJson = {
25 "type":"FeatureCollection",
26 "features":[
27 {"type":"Feature","id":1,"properties":{"type":"station","name":"Appenzell"},"geometry":{"type":"Point","coordinates":[9.40991,47.32849]}},
28 {"type":"Feature","id":2,"properties":{"type":"station","name":"Gais"},"geometry":{"type":"Point","coordinates":[9.45107,47.36073]}},
29 {"type":"Feature","id":3,"properties":{"type":"station","name":"St. Gallen"},"geometry":{"type":"Point","coordinates":[9.36901,47.42208]}},
30 {"type":"Feature","id":4,"properties":{"type":"station","name":"Teufen"},"geometry":{"type":"Point","coordinates":[9.390178,47.390157]}}
31 ]};
32
33 var trainJson = [{
34 "type":"FeatureCollection",
35 "features":[
36 {"type":"Feature","id":10,"properties":{"type":"train","name":"Testtrain"},"geometry":{"type":"Point","coordinates":[9.36901,47.42208]}}
37 ]},{
38 "type":"FeatureCollection",
39 "features":[
40 {"type":"Feature","id":10,"properties":{"type":"train","name":"Testtrain"},"geometry":{"type":"Point","coordinates":[9.390178,47.390157]}}
41 ]},{
42 "type":"FeatureCollection",
43 "features":[
44 {"type":"Feature","id":10,"properties":{"type":"train","name":"Testtrain"},"geometry":{"type":"Point","coordinates":[9.45107,47.36073]}}
45 ]},{
46 "type":"FeatureCollection",
47 "features":[
48 {"type":"Feature","id":10,"properties":{"type":"train","name":"Testtrain"},"geometry":{"type":"Point","coordinates":[9.40991,47.32849]}}
49 ]}];
50
51 var trainPosition = 0,
52 trainDirection = 'up';
53
54 var tiles = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
55 maxZoom: 18,
56 attribution: '&copy; <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
57 }),
58 map = new L.Map('map', {zoom: 15, layers: [tiles]}),
59 markers = new L.MarkerClusterGroup({ spiderfyOnMaxZoom: false, showCoverageOnHover: false, zoomToBoundsOnClick: false });
60
61 var stationGeoJsonLayer = L.geoJson(stationJson, {
62 onEachFeature: function (feature, layer) {
63 layer.bindPopup(feature.properties.name);
64 }
65 }),
66 trainGeoJsonLayer = L.geoJson(trainJson[trainPosition], {
67 onEachFeature: function (feature, layer) {
68 layer.bindPopup(feature.properties.name);
69 }
70 });
71
72 // initial load
73 markers.addLayer(stationGeoJsonLayer);
74 markers.addLayer(trainGeoJsonLayer);
75 map.fitBounds(markers.getBounds());
76
77 markers.on('clusterclick', function (a) {
78 a.layer.spiderfy();
79 });
80
81 map.addLayer(markers);
82
83 /**
84 * Demonstration method that simulates that we got a new geoJson object with updated train positions.
85 */
86 function moveTrain() {
87 if (trainDirection == 'up') trainPosition++;
88 else if (trainDirection == 'down') trainPosition--;
89 if (trainPosition == trainJson.length-1) trainDirection = 'down';
90 else if (trainPosition == 0) trainDirection = 'up';
91
92 // build a new geoJson layer with the new train information
93 trainGeoJsonLayer = L.geoJson(trainJson[trainPosition], {});
94
95 // update the cluster markers with both layers (stations first so that the station marker is always the first on spider.
96 markers.clearLayers();
97 markers.addLayer(stationGeoJsonLayer);
98 markers.addLayer(trainGeoJsonLayer);
99 }
100
101 L.DomUtil.get('moveTrain').onclick = function () {
102 moveTrain();
103 };
104 </script>
105 </body>
106 </html>