简体   繁体   English

传单:带有图层组的 .bindPopup 在所有功能的同一位置打开弹出窗口

[英]Leaflet: .bindPopup with layer groups opens popup in the same spot for all features

I'm building a Leaflet map of recent earthquakes that pulls from three APIs to create three layer groups on a map.我正在构建最近地震的 Leaflet 地图,该地图从三个 API 中提取以在地图上创建三个图层组。 I'm also building a sidebar with a list of earthquakes from just one of the three API links.我还在构建一个侧边栏,其中包含来自三个 API 链接之一的地震列表。 When a user clicks on a map marker, a popup appears with more information about the quake.当用户单击地图标记时,会出现一个弹出窗口,其中包含有关地震的更多信息。 And when they click on a list item in the sidebar, I'd like for the popup for the corresponding quake to open, the same as if the user had clicked on the marker.当他们单击侧边栏中的列表项时,我希望打开相应地震的弹出窗口,就像用户单击标记一样。

The popups work fine when the user clicks on the map markers.当用户点击地图标记时,弹出窗口工作正常。 However, when I click on the list item, the popups with the correct information do open, but they all appear in the same place, and not at the quake's correct lat/lng.但是,当我单击列表项时,具有正确信息的弹出窗口会打开,但它们都出现在同一个位置,而不是地震的正确纬度/经度。

I tried using FeatureGroup instead of LayerGroup, as suggested here , but the same thing happens.我尝试按照此处的建议使用 FeatureGroup 而不是 LayerGroup,但发生了同样的事情。 I also tried adding the popup to a specific layer with addTo(layers[key]);, but this changes nothing, either.我还尝试使用 addTo(layers[key]); 将弹出窗口添加到特定图层,但这也没有任何改变。

Here is the relevant code:这是相关的代码:

URLs = {
  PAST_MONTH: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_month.geojson",
  PAST_WEEK: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_week.geojson", 
  PAST_DAY: "https://earthquake.usgs.gov/earthquakes/feed/v1.0/summary/2.5_day.geojson",
};

Object.entries(URLs).forEach(([key, url]) => {

  d3.json(url).then(function (response){


    //Add features to the map.
    response.features.forEach(feature => {
    var calif = feature.properties.title.split(",");
    if (calif[1] === " CA") {
      console.log(feature.properties.title);
      quake = L.geoJSON(feature, {
        pointToLayer: function(feature, latlng) {
          return L.circleMarker(latlng, {
            radius: getRadius(feature.properties.mag),
            fillColor: getColor(feature.properties.mag),
            color: getColor(feature.properties.mag),
            weight: 1,
            opacity: 1,
            fillOpacity: 0.8
          });
        },
      });


    //populate the sidebar with JUST the list of earthquakes in the past week
    if (url === URLs.PAST_WEEK) {
      var list = document.getElementsByClassName("quake-list");
      var listItem = document.createElement("li");
      listItem.innerHTML = feature.properties.title + " " + moment(feature.properties.time).startOf('day').fromNow(); 
      list[0].appendChild(listItem);
      listItem.addEventListener('click', function() {
          quake.bindPopup("<h3>" + feature.properties.title + "</h3><hr>" + 
              "<strong>Magnitude: </strong>" + feature.properties.mag + 
              "<br><strong>Date and time: </strong>" + convertUnixTime(feature.properties.time))
          quake.openPopup();
      })
    }

      // Add popup with info, when user clicks on map markers
      quake.bindPopup("<h3>" + feature.properties.title + "</h3><hr>" + 
      "<strong>Magnitude: </strong>" + feature.properties.mag + 
      "<br><strong>Date and time: </strong>" + convertUnixTime(feature.properties.time));

      // Add to specific layer
      quake.addTo(layers[key]);

    }; 

    });
  });
});

And this is the specific part where I can't figure out how to bind the popups to the list items so that they appear at the correct lat/lng:这是我无法弄清楚如何将弹出窗口绑定到列表项以便它们出现在正确的纬度/经度的特定部分:

//populate the sidebar with JUST the list of earthquakes in the past week
    if (url === URLs.PAST_WEEK) {
      var list = document.getElementsByClassName("quake-list");
      var listItem = document.createElement("li");
      listItem.innerHTML = feature.properties.title + " " + moment(feature.properties.time).startOf('day').fromNow(); 
      list[0].appendChild(listItem);
      listItem.addEventListener('click', function() {
          quake.bindPopup("<h3>" + feature.properties.title + "</h3><hr>" + 
              "<strong>Magnitude: </strong>" + feature.properties.mag + 
              "<br><strong>Date and time: </strong>" + convertUnixTime(feature.properties.time))
          quake.openPopup();
      })
    }

I'm new to Leaflet and would appreciate any help.我是 Leaflet 的新手,希望得到任何帮助。 I'm sure I'm missing something obvious but I've been racking my brain trying to see what I'm missing.我确定我遗漏了一些明显的东西,但我一直在绞尽脑汁想看看我遗漏了什么。 Thank you!谢谢!

Instead adding the popup to the layergroup bind the popup to each layer.而是将弹出窗口添加到图层组,将弹出窗口绑定到每个图层。

if (url === URLs.PAST_WEEK) {
      var list = document.getElementsByClassName("quake-list");
      var listItem = document.createElement("li");
      listItem.innerHTML = feature.properties.title + " " + moment(feature.properties.time).startOf('day').fromNow(); 
      list[0].appendChild(listItem);
      listItem.addEventListener('click', function() {
          quake.eachLayer(function(layer){
            layer.bindPopup("<h3>" + feature.properties.title + "</h3><hr>" + 
                "<strong>Magnitude: </strong>" + feature.properties.mag + 
                "<br><strong>Date and time: </strong>" + convertUnixTime(feature.properties.time),
{autoClose: false})
            layer.openPopup();
         });
      })
    }

The problem turns out to be an issue with scope for the "quake" variable.事实证明,问题是“地震”变量的范围问题。 This code works:此代码有效:

  var quake = L.geoJSON(feature, {
    pointToLayer: function(feature, latlng) {
      return L.circleMarker(latlng, {
        radius: getRadius(feature.properties.mag),
        fillColor: getColor(feature.properties.mag),
        color: getColor(feature.properties.mag),
        weight: 1,
        opacity: 1,
        fillOpacity: 0.8
      });
    },
  });  

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

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