gpt4 book ai didi

javascript - Google map 多个协同工作的下拉过滤器

转载 作者:行者123 更新时间:2023-12-03 04:36:13 25 4
gpt4 key购买 nike

我正在尝试使用两个或更多下拉菜单过滤谷歌地图上的标记。问题是下拉过滤器不会相互过滤。当我更改一个下拉列表的值并过滤掉标记时,第二个过滤器不会过滤掉第一个过滤器显示的标记,而是过滤掉所有标记。任何帮助,将不胜感激!

JS:

//global variables
var gmarkers1 = [];
var markers1 = [];
var infowindow = new google.maps.InfoWindow({
content: ''
});

//add the shipwreck data
markers1 = [
["Name","Latitude","Longitude","Year","Type","Type_of_Loss" ],
["Bermuda","46.46483333","-86.64683333","1870","Schooner","Storm" ],
["George","46.516","-86.52083333","1893","Schooner","Storm" ],
["Herman H. Hettler","46.48383333","-86.59966667","1926","Propeller","Storm" ],
["Kiowa","46.64516667","-86.22016667","1929","Propeller","Storm" ],

];


//initiate the map
function initMap() {
// Our markers


var center = new google.maps.LatLng(52.4357808, 4.991315699999973);
var mapOptions = {
zoom: 12,
center: center,
mapTypeId: google.maps.MapTypeId.TERRAIN
};



map = new google.maps.Map(document.getElementById('map'), mapOptions);
for (i = 0; i < markers1.length; i++) {
addMarker(markers1[i]);
}

}


//add markers to the map
function addMarker(marker) {
var category_type = marker[4];
var category_loss = marker[5];
var category_year = marker[0];
var title = marker[0];
var pos = new google.maps.LatLng(marker[1], marker[2]);
var content = marker[0];

marker1 = new google.maps.Marker({
title: title,
position: pos,
//setup the categories for the different mark types
category_type: category_type,
category_loss: category_loss,
category_year: category_year,
map: map
});

gmarkers1.push(marker1);

// Marker click listener
google.maps.event.addListener(marker1, 'click', (function (marker1, content) {
return function () {
console.log('Gmarker 1 gets pushed');
infowindow.setContent(content);
infowindow.open(map, marker1);
map.panTo(this.getPosition());
map.setZoom(15);
}
})(marker1, content));
}


//filter the markers by type of ship
filterMarkers = function (category_type) {
for (i = 0; i < markers1.length; i++) {
marker = gmarkers1[i];
// If is same category or category not picked
if (marker.category_type == category_type || category_type.length === 0) {
marker.setVisible(true);
}
// Categories don't match
else {
marker.setVisible(false);
}
}
}

//filter the markers by type of loss
filterMarkers2 = function (category_loss) {
for (i = 0; i < markers1.length; i++) {
marker = gmarkers1[i];
// If is same category or category not picked
if (marker.category_loss == category_loss || category_loss.length === 0) {
marker.setVisible(true);
}
// Categories don't match
else {
marker.setVisible(false);
}

}
}

// Init map
initMap();

HTML:

<div class="container-fluid">
<div class="row">
<div class="sidebar col-xs-3">
<h2>Filter Shipwrecks</h2>
<div class="filter-options">
<div class="filter-set" style="margin-top:0;">
<button id="load-btn" class="load-btn button is-success">Reload data</button>
</div>
<div class="filter-set">
<label for="ship-select">Ship Type:</label>
<select id="type" onchange="filterMarkers(this.value);">
<option value="">Any</option>
<option value="Barge">Barge</option>
<option value="Freighter">Freighter</option>
<option value="Propeller">Propeller</option>
<option value="Schooner">Schooner</option>
<option value="Steamer">Steamer</option>
</select>
</div>
<div class="filter-set">
<label for="ship-select">Type of Loss:</label>
<select id="type" onchange="filterMarkers2(this.value);">
<option value="">Any</option>
<option value="Collision">Collision</option>
<option value="Fire">Fire</option>
<option value="Intentional">Intentional</option>
<option value="Storm">Storm</option>
</select>
</div>

最佳答案

正如我上面评论的,您可以通过考虑 <select> 的值来解决过滤问题。元素。

您可以通过使用单个过滤函数来实现这一点,如下所示:

filterMarkers = function () {
var category_type = document.getElementById('category_type_filter').value;
var category_loss = document.getElementById('category_loss_filter').value;
for (i = 0; i < markers1.length; i++) {
marker = gmarkers1[i];
if ((
marker.category_type == category_type ||
category_type.length === 0
) && (
marker.category_loss == category_loss ||
category_loss.length === 0
)) {
marker.setVisible(true);
} else {
marker.setVisible(false);
}
}
};

如果将此与您的函数进行比较,您将看到将标记设置为“可见”的条件现在是两个过滤器的组合。

在 HTML 中,添加 id属性到每个<select> ,使用函数中的相同值:category_type_filtercategory_loss_filter .

这应该可以解决问题。

需要明确的是,您的问题与 Google map 无关,而是与 Javascript 相关。也许花一些时间阅读 DOM、事件和 Javascript 如何协同工作是个好主意。 :)

P.S.:为了简洁起见,您甚至可以替换整个 if/else通过单个函数调用阻止:

marker.setVisible((marker.category_type == category_type || category_type.length === 0) && (marker.category_loss == category_loss || category_loss.length === 0));

关于javascript - Google map 多个协同工作的下拉过滤器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43288605/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com