简体   繁体   English

Leaflet:标记图标和 markerCluster 一起显示在 map 上,带 fetch

[英]Leaflet : marker icons and markerCluster displaying together on the map with fetch

I am using Leaflet on my project.我在我的项目中使用 Leaflet。 I work with PHP / Symfony, I retrieve the data from the Controller via fetch to display the markers on the map.我与 PHP / Symfony 合作,我通过 fetch 从 Controller 中检索数据,以显示 Z1D78AEB7C8ED4FE2414E 上的标记

All markers are displayed on the map, but when using the markerCluster, markers that have close coordinates on the map (eg: same city) do not group together in clusters.所有标记都显示在 map 上,但是当使用 markerCluster 时,在 map 上具有接近坐标的标记(例如:同一城市)不会在群集中组合在一起。 The markers farther apart from each other group well together.彼此相距较远的标记很好地组合在一起。 Have you an idea why?你知道为什么吗? Thanks谢谢

I am adding screens to you so that you can better understand:)我正在为您添加屏幕,以便您更好地理解:)

屏幕图 1屏幕图2

map.js map.js

let iconPicture = L.icon({
  iconUrl: "/assets/images/cycling.png",
  iconSize: [20, 20],
  popupAnchor: [0, -10],
});

function initMap() {
  var map = L.map("mapId").setView([48.833, 2.333], 12);

  var osmLayer = L.tileLayer("http://{s}.tile.osm.org/{z}/{x}/{y}.png", {
    attribution: "© OpenStreetMap contributors",
    minZoom: 3,
    maxZoom: 19,
  });

  var markersGroup = L.markerClusterGroup({
    disableClusteringAtZoom: 13,
    spiderfyOnMaxZoom: false,
    removeOutsideVisibleBounds: true,
  });

  fetch("/api/map")
    .then((response) => {
      return response.json();
    })
    .then((result) => {
      result.forEach((element) => {
        var cluster = L.marker([element.latitude, element.longitude], {
          icon: iconPicture,
        })
          // Nous ajoutons la popup. A noter que son contenu peut être du HTMl
          .bindPopup(
            function (layer) {
              return (
                "<span>" +
                element.name +
                "</span>" +
                "<br>" +
                "<div class='img-hover-zoom'>" +
                "<a href=" +
                "/collection50_60/" +
                element.id +
                ">" +
                "<img class='picturePopup' src=/assets/images/uploads/" +
                element.pictureFront +
                ">" +
                "</a>" +
                "</div>" +
                "<br>" +
                element.city +
                "<br>" +
                "<a href=" +
                "/collection50_60" +
                ">" +
                element.years +
                "</a>"
              );
            },
            { className: "pop-up-leaflet", direction: "top" } //then add your options
          )
          .addTo(map);
        markersGroup.addLayer(cluster);
      });
      map.addLayer(markersGroup);
    })
    .catch(() => console.error("error"));
}
window.onload = function () {
  initMap();
};

That is just because you add your Markers (variable cluster ) to both the map and the MarkerClusterGroup.那只是因为您将标记(变量cluster )添加到 map 和 MarkerClusterGroup。

Simply add them only to MCG and let the latter handle everything.只需将它们仅添加到 MCG 并让后者处理所有事情。

var cluster = L.marker(latLng, options)
  .bindPopup()
  //.addTo(map); <= do not add your Marker directly to the map
markersGroup.addLayer(cluster); // <= let the MCG handle everything 

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM