【发布时间】:2021-12-17 16:07:25
【问题描述】:
我正在尝试根据下拉列表中的行业选择过滤我的地图标记图层,我在此处的 html 中手动引用了该列表 - 我想最初保留预加载的标记数据并提供一个选项通过从下拉列表中选择来过滤标记。
<select class="dropdown" id="selDataset">
<option value="" selected="selected">Industry</option>
<option value ="ind1">Accomodation and Food Services</option>
<option value ="ind2">Educational Services</option>
<option value ="ind3">Administrative and Support and Waste Management and Remediation Services</option>
<option value ="ind4">Professional, Scientific, and Technical Services</option>
<option value ="ind5">Manufacturing</option>
<option value ="ind6">Construction</option>
<option value ="ind7">Health Care and Social Assistance</option>
<option value ="ind8">Wholesale Trade</option>
<option value ="ind9">Information</option>
</select>
这是我的 .js 代码 - 我的猜测是我需要定义一个包含第一个 for 循环的函数,如果选择过滤该行业的 lat,lng,则设置一个 if else 语句,否则默认返回所有.我只是不确定如何引用 html 下拉选择来过滤标记。我已经通过 D3.js 实现了用于表格过滤的下拉菜单,并且之前使用过覆盖层,但是我找不到使用传单标记层的实现。
// Creating map object
var myMap = L.map("map", {
center: [40.73, -74.0059],
zoom: 11
});
// Adding tile layer to the map
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png", {
attribution: '© <a href="https://www.openstreetmap.org/copyright">OpenStreetMap</a> contributors'
}).addTo(myMap);
var data = [{
lat: 40.75,
lng: -73.99,
BusinessName: 'Bank LLC',
Industry: 'Banking'
},
{
lat: 40.73,
lng: -73.97,
BusinessName: 'Some Hotel',
Industry: 'Hospitality'
},
{
lat: 40.74,
lng: -74.00,
BusinessName: 'Other Hotel',
Industry: 'Hospitality'
}
];
// Create a new marker cluster group
var markers = L.layerGroup();
// Loop through data
for (var i = 0; i < data.length; i++) {
// Set the latitude, longitude property to a variable
var latitude = data[i].lat;
var longitude = data[i].lng;
var popText = ("<h3>" + data[i].BusinessName + "</h3><h3>Industry: " + data[i].Industry + "</h3>");
// var industry = data[i].Industry;
// var mySelector = $("#Industry").val();
// Check for location property
if (latitude) {
// Add a new marker to the cluster group and bind a pop-up
markers.addLayer(L.marker([latitude, longitude])
.bindPopup(popText));
};
}
// Add our marker cluster layer to the map
myMap.addLayer(markers);
#map {
height: 400px;
width: 600px;
}
<link rel="stylesheet" href="https://unpkg.com/leaflet@1.7.1/dist/leaflet.css"
integrity="sha512-xodZBNTC5n17Xt2atTPuE1HxjVMSvLVW9ocqUKLsCC5CXdbqCmblAshOMAS6/keqq/sMZMZ19scR4PsZChSR7A=="
crossorigin=""/>
<script src="https://cdnjs.cloudflare.com/ajax/libs/d3/6.2.0/d3.min.js"></script>
<script src="https://unpkg.com/leaflet@1.7.1/dist/leaflet-src.js"></script>
<div id="map"></div>
【问题讨论】:
-
如果您可以创建代码的minimal reproducible example,最好在runnable stack snippet 中使用一些样本/虚拟数据来帮助我们理解问题,这将非常有帮助。这样,我们可以更轻松地回答您的问题,您也更有可能得到一个好的答案!
-
@RubenHelsloot 嗨鲁本,谢谢你的回复。我不是 100% 确定如何在我的 js 脚本中补充数据以提供运行代码 sn-p 而不会显着改变我的 .js 文件以在不引用源文件的情况下考虑数据读入,我认为这是寻求的组成部分解决方案。但是,我确实有一个正在运行的 heroku 应用程序就足够了。
-
您能否提供几行来自 CSV 文件的示例数据?它们可以有虚拟值,这只是结构。
-
@RubenHelsloot ibb.co/m5SKxZq