【问题标题】:Selecting multiple leaflet markers and generating a download link选择多个传单标记并生成下载链接
【发布时间】:2020-01-27 01:09:58
【问题描述】:

编辑: 我当前的部分解决方案代码在这里:https://jsfiddle.net/crashvector/xczj76om/26/

感谢@zakariah1 为我指明了正确的方向。

我还没有用于下载的弹出窗口,但我正在从缩放框中输出样本的名称。


我有一张传单地图,它从 json 中引入数据,并动态生成图层和集群。每个标记都有一个下载按钮,该按钮将该标记的名称传递给后续 php 脚本,该脚本随后访问数据库并向用户提供示例数据。

接下来我要做的是能够选择多个标记并将这些名称传递给同一个 php 脚本。 (该脚本将输入解析为一个数组,因此在传递一个名称或一个长列表时它会起作用。)

我试图使用此代码进行批量选择,但由于 lat 未定义而引发错误。

map.on("boxzoomend", function(e) {
  for (var i = 0; i < markers.length; i++) {
    if (e.boxZoomBounds.contains(markers[i])) {
      console.log(markers[i]);
    }
  }
});

我一直在尝试整理一些旧的解决方案,这些解决方案可以像结合传单绘制或其他插件一样工作。但我在那个部门没有取得太大进展。

但我想,我不仅需要一种选择多个标记的方法(通过绘制一个框,或者某种 ctrl/shift 单击排列),而且我还需要一个提示以弹出下载数据。 (因为我当前的下载链接是在标记本身中生成的,并且仅适用于该特定标记。)

这是我的地图示例:https://jsfiddle.net/crashvector/xczj76om/19/

目标:最终结果将允许用户选择多个标记,然后生成按钮或弹出提示以下载相关数据。该链接类似于:

`http://server/download.php?sample_name='sample_1 sample_2 sample_3 sample_4'>Download Microbial Community Data</button>'

非常感谢任何见解!


编辑 当前尝试在这里:https://jsfiddle.net/crashvector/xczj76om/24/

我创建了一个数组来保存所有点,而不管层。然后在生成每个标记时,我将名称和 latlng 推送到该数组中。

然后我将数组转储到控制台以确保它在那里,然后将 boxzoomed 函数指向数组中的 latlngs。当只有数组中的数据是 latlngs 时,我能够让它工作。但是现在我已经合并了示例名称​​,它不起作用。 (我确定这是因为我不理解存储或检索值所需的符号。)

我走对了吗?

//adding an array to hold all marker locations and names
var bulk_list = [];


for (var i = 0; i < markers.length; ++i) {
  var popup = '<br/><b>Sample Name:</b> ' + markers[i]["Sample Name"] +
    '<br/><b>Location Description:</b> ' + markers[i]["Location Description"] +
    '<br/><b>Date Taken:</b> ' + markers[i].Date +
    '<br/><b>Classification:</b> ' + markers[i].Classification +
    '<br/><button onclick="window.location=`zip_download.php?sample_name_list=' + markers[i]["Sample Name"] + '`" download="' + markers[i]["Sample Name"] + ' community data.csv">Download Microbial Community Data</button>'

  //define markers  
  var m = L.marker([markers[i].Lattitude, markers[i].Longitude], {
      icon: icons[markers[i].Classification]
    })
    .bindPopup(popup);
  category = markers[i].Classification;


  //pushing each markers name and latlng to the bulklist array
  var data = {};
  var name = markers[i]["Sample Name"];
  var latlng = L.latLng([markers[i].Lattitude, markers[i].Longitude]);
  data.name = name;
  data.latlng = latlng;
  bulk_list.push(data);


  // Initialize the category array if not already set.
  if (typeof categories[category] === "undefined") {
    categories[category] = L.featureGroup.subGroup(parentGroup, m).addTo(map);
    layersControl.addOverlay(categories[category], category);
  }
  categories[category].addLayer(m);




}


//output bulklist to show it worked
console.log(bulk_list);


//output sample names of points in the zoom box
map.on("boxzoomend", function(e) {
  for (var i = 0; i < bulk_list.length; i++) {
    if (e.boxZoomBounds.contains(bulk_list.latlng[i])) {
      console.log(bulk_list.name[i]);
    }
  }
});

【问题讨论】:

    标签: jquery leaflet


    【解决方案1】:

    看看这些链接。问题在于“(e.boxZoomBounds.contains(markers[i]))”

    https://github.com/tombatossals/angular-leaflet-directive/issues/863

    Determine if point is within bounding box

    【讨论】:

    • 我不太明白(因为我在 JS 中相当无能。)但这两个页面似乎都表明它需要是 LatLng。这是一堂课还是什么?我试图在 Stormish 的 7 月 28 日的回答中强求,但我不确定 $scope.getKarte 是什么,或者它在我的代码中是等价的。我也喜欢 psychicsaint 的边界框答案,但在尝试使用它时会获得非法回报。我想我的大脑已经干了一天了,也许明天新鲜的眼睛会有所帮助。
    • 另一个我一直在寻找的答案:stackoverflow.com/questions/44261122/…
    • 用我目前的方向更新我的问题。
    • 这让我走到了我最想去的地方。用我当前的代码更新了我的问题,并将开始一个新问题。谢谢!
    猜你喜欢
    • 1970-01-01
    • 2012-08-05
    • 2012-01-11
    • 2013-08-26
    • 2014-06-19
    • 2015-09-09
    • 1970-01-01
    • 2017-10-29
    • 1970-01-01
    相关资源
    最近更新 更多