【发布时间】: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]);
}
}
});
【问题讨论】: