首页 > 解决方案 > 如何拟合标记

问题描述

Fit.Bounds在我的传单代码中,不起作用。

我有代码可以在 Leaflet 创建的地图上显示标记,但它看起来Fit.Bounds不起作用。我不知道为什么,以及如何解决它。

<!-- INFA O WYDARZENIU MAPA OPENSTREET -->

<div id="mapid" style="width: 685px; max-width: 100%; height: 200px;"></div>
<script>
  var mymap = L.map("mapid").setView([50.0287132, 19.953625399999964], 5);
  L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png?{foo}", {
    foo: "bar",
    attribution:
      'Map data &copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors, <a href="https://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'
  }).addTo(mymap);
  var fg = L.featureGroup().addTo(mymap);

  L.marker([51.7790165, 19.443519100000003]).addTo(fg);

  L.marker([50.0287132, 19.953625399999964]).addTo(fg);

  // Fit all markers
  setTimeout(function() {
    map.fitBounds(fg.getBounds());
  }, 1000);
</script>
<!-- INFA O WYDARZENIU KONIEC MAPY OPEN STREET -->

标签: javascriptleaflet

解决方案


推荐阅读