Leaflet 中的 overlayMaps 和标记

overlayMaps and markers in Leaflet

我正在使用 Leaflet 在地图上显示两组标记。我们称它们为 "trip 1" 和 "trip 2"。 标记信息(纬度、经度、描述等)存储在两个单独的 geojson 文件中。

我想单独显示每个行程,使用两个 L.layerGroup,每个行程一个。

下面的代码是我写的页面:到目前为止this是我的。

问题是在选择每个行程之前地图上已经显示了所有标记(右上角 - 见附图)。 我希望在 选择后 显示标记。

    <!DOCTYPE html>
    <html>
     <head>
     </head>
     <body>

      <center>
       <div id="map" class="embed-container"></div>
      </center>

    <script>
    var viaggio1 = L.layerGroup([
    <?php
    $url = 'docs/guardini.geojson'; // path to your JSON file
    $dati = file_get_contents($url); // put the contents of the file into a variable
    $result = json_decode($dati, true); // decode the JSON feed
    $data = $result['features'];
    foreach($data as $key => $row) {
        $numero = $row['id'];
        $nome = $row['name'];
        $lat = $row['lat'];
        $lon = $row['lon'];
        $text = $row['text'];
        $pic = $row['pic'];
        $link = $row['link'];
    ?>
    L.marker(['<?=$lat;?>', '<?=$lon;?>']),
    <?php
    }
    ?>
    ]);
    </script>

    <script>
    var viaggio2 = L.layerGroup([
    <?php
    $url2 = 'docs/guardini2.geojson'; // path to your JSON file
    $dati2 = file_get_contents($url2); // put the contents of the file into a variable
    $result2 = json_decode($dati2, true); // decode the JSON feed
    $data2 = $result2['features'];
    foreach($data2 as $key2 => $row2) {
        $numero2 = $row2['id'];
        $nome2 = $row2['name'];
        $lat2 = $row2['lat'];
        $lon2 = $row2['lon'];
        $text2 = $row2['text'];
        $pic2 = $row2['pic'];
        $link2 = $row2['link'];
    ?>
    L.marker(['<?=$lat2;?>', '<?=$lon2;?>']),
    <?php
    }
    ?>
    ]);
    </script>

    <script>
    var overlayMaps = {
        "viaggio 1": viaggio1,
        "viaggio 2": viaggio2
    };

    var map = L.map('map').setView([48.3585, 10.86135], 6);
      L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
        {
          attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
          maxZoom: 19,
          minZoom: 4
        }).addTo(map);
    L.control.scale({imperial: false}).addTo(map);

    var pol = L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
      {
        attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
        minZoom: 4,
        maxZoom: 19
      }).addTo(map);

    var sat = L.tileLayer('http://server.arcgisonline.com/ArcGIS/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}',
      {
        attribution: 'Tiles &copy; Esri &mdash; Source: Esri, IGN, IGP, UPR-EGP, and the GIS User Community',
        minZoom: 4,
        maxZoom: 19
      });

    var arte = L.tileLayer('http://stamen-tiles-{s}.a.ssl.fastly.net/watercolor/{z}/{x}/{y}.{ext}',
      {
        attribution: 'Map tiles by <a href="http://stamen.com">Stamen Design</a>, <a href="http://creativecommons.org/licenses/by/3.0">CC BY 3.0</a> &mdash; Map data &copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>',
        subdomains: 'abcd',
        minZoom: 4,
        maxZoom: 19,
        ext: 'png'
    });

    var baseMaps = {
        "politica": pol,
        "satellitare": sat,
        "artistica": arte
    };
    //L.control.layers(baseMaps).addTo(map);
    L.control.layers(baseMaps, overlayMaps).addTo(map);

    var stile = {
        "color": "#ff3385",
        "weight": 4,
        "opacity": 0.65
    };
    </script>

    <?php
    $url = 'docs/guardini.geojson'; // path to your JSON file
    $dati = file_get_contents($url); // put the contents of the file into a variable
    $result = json_decode($dati, true); // decode the JSON feed
    $data = $result['features'];
    foreach($data as $key => $row) {
        $numero = $row['id'];
        $nome = $row['name'];
        $lat = $row['lat'];
        $lon = $row['lon'];
        $text = $row['text'];
        $pic = $row['pic'];
        $link = $row['link'];
    ?>
    <div id="sidebar<?=$numero;?>" align="left"></div>
    <script>
            var sidebar<?=$numero;?> = L.control.sidebar('sidebar<?=$numero;?>', {
                closeButton: true,
                position: 'left'
            });
            sidebar<?=$numero;?>.setContent('<br>luogo nr. <?=$numero;?><br><br><b><?=$nome;?></b><br><br><?=$text;?><br><br><img src="<?=$pic;?>"><br><br><a href="<?=$link;?>" target="_blank"><?=$link;?></a><br>');
            map.addControl(sidebar<?=$numero;?>);
            setTimeout(function () {
                sidebar<?=$numero;?>.hide();
            }, 700);

            var marker<?=$numero;?> = L.marker(['<?=$lat;?>', '<?=$lon;?>']).addTo(map).on('click', function () {
                sidebar<?=$numero;?>.toggle();
            });
    </script>
    <?php
    }
    ?>
    <?php
    $url2 = 'docs/guardini2.geojson'; // path to your JSON file
    $dati2 = file_get_contents($url2); // put the contents of the file into a variable
    $result2 = json_decode($dati2, true); // decode the JSON feed
    $data2 = $result2['features'];
    foreach($data2 as $key2 => $row2) {
        $numero2 = $row2['id'];
        $nome2 = $row2['name'];
        $lat2 = $row2['lat'];
        $lon2 = $row2['lon'];
        $text2 = $row2['text'];
        $pic2 = $row2['pic'];
        $link2 = $row2['link'];
    ?>
    <div id="sidebar<?=$numero2;?>" align="left"></div>
    <script>
            var sidebar<?=$numero2;?> = L.control.sidebar('sidebar<?=$numero2;?>', {
                closeButton: true,
                position: 'left'
            });
            sidebar<?=$numero2;?>.setContent('<br>luogo nr. <?=$numero2;?><br><br><b><?=$nome2;?></b><br><br><?=$text2;?><br><br><img src="<?=$pic2;?>"><br><br><a href="<?=$link2;?>" target="_blank"><?=$link2;?></a><br>');
            map.addControl(sidebar<?=$numero2;?>);
            setTimeout(function () {
                sidebar<?=$numero2;?>.hide();
            }, 700);

            var markerr<?=$numero2;?> = L.marker(['<?=$lat2;?>', '<?=$lon2;?>']).addTo(map).on('click', function () {
                sidebar<?=$numero2;?>.toggle();
            });
    </script>
    <?php
    }
    ?>
    </div>
    </div>

    </body>
    </html>

您似乎正在向地图添加标记:

L.marker(['<?=$lat2;?>', '<?=$lon2;?>']).addTo(map)

我想你要做的是将标记添加到 layerGroups:

L.marker(['<?=$lat2;?>', '<?=$lon2;?>']).addTo(viaggio2)