File: add_remove_layers.html

package info (click to toggle)
leaflet 1.4.0~dfsg-5
  • links: PTS, VCS
  • area: main
  • in suites: buster
  • size: 19,000 kB
  • sloc: sh: 26; makefile: 24; xml: 23
file content (70 lines) | stat: -rw-r--r-- 2,218 bytes parent folder | download | duplicates (3)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
<!DOCTYPE html>
<html>
<head>
    <title>Leaflet debug page</title>

    <meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=no">

    <link rel="stylesheet" href="../../dist/leaflet.css" />

    <link rel="stylesheet" href="../css/screen.css" />

    <script src="../leaflet-include.js"></script>
</head>
<body>
    <div id="map"></div>
    <div id="buttons">
        <button type="button" id="b1"> Add Layer</button>
        <button type="button" id="b2"> Remove Layer</button>
    </div>
    <script>
        var map;
        var myLayerGroup = new L.LayerGroup();

        // set up the map
        map = new L.Map('map', {preferCanvas: true});

        // create the tile layer with correct attribution
        var osmUrl = 'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png';
        var osmAttrib = 'Map data © OpenStreetMap contributors';
        var osm = new L.TileLayer(osmUrl, { minZoom: 1, maxZoom: 17, attribution: osmAttrib, detectRetina: true });
        map.addLayer(osm);
        map.fitBounds(new L.LatLngBounds([51,7],[51,7]));
        drawTestLine();

        function drawTestLine() {
            var lat = 51;
            var long = 7;
            for (var i = 0; i < 50; i++) {

                var myCircle = new L.Circle(new L.LatLng(lat, long),3);
                myCircle.on('click',
                    function (e) {
                        popup = new L.Popup();
                        popup.setLatLng(this.getLatLng());

                        var popuptxt = "Hello!";
                        alert("I am the click function");
                                popup.setContent(popuptxt);

                                map.openPopup(popup);


                    });
                myLayerGroup.addLayer(myCircle);
                    lat = lat + 0.0001;
                    long = long + 0.0001;

            }
            map.addLayer(myLayerGroup);
        };

        L.DomEvent.on(L.DomUtil.get('b1'), 'click', function () {
          map.addLayer(myLayerGroup);
        });
        L.DomEvent.on(L.DomUtil.get('b2'), 'click', function () {
          map.removeLayer(myLayerGroup);
        });
    </script>
</body>
</html>