使用硬編碼的標記位置陣列構建Google地圖頁面。有一個搜索框使用敲除來過濾屏幕上的可見標記。這個想法是,當你在搜索框中輸入每個字母時,敲除過濾你在屏幕上看到的標記。這一切都正常,但如果我在搜索框中輸入幾個字母,然後清除它,所有標記都不會重新出現在屏幕上。我知道這應該是一個簡單的條件,但對於我的生活,我無法得到它的工作。 下面是相關代碼:knockoutjs Google Maps API顯示所有標記
var locations = [
{
title: "Location 1",
latlong: { lat: 25.55555, lng: -80.55555 },
street: "123 Main St",
city: "Anytown, USA 12345",
id: 0,
showLoc: ko.observable(true),
visible: ko.observable(true),
},
{
title: "Location 2",
latlong: { lat: 27.77777, lng: -81.11111 },
street: "456 Main St",
city: "Anytown, USA 12345",
id: 1,
showLoc: ko.observable(true),
visible: ko.observable(true),
}]
//loop to drop markers on map and add marker properties
function createMarkers() {
for (i = 0; i < locations.length; i++) {
locations[i].marker = new google.maps.Marker({
position: locations[i].latlong,
map: map,
title: locations[i].title,
animation: google.maps.Animation.DROP,
id: i
});
}
}
//sets marker visibility
function setMarkerVisibility() {
for (i = 0; i < locations.length; i++) {
locations[i].marker.setVisible(locations[i].visible);
}
}
//knockout viewmodel - handles search locations array and filtering
function AppViewModel() {
query = ko.observable('');
this.locations = ko.computed(function() {
var self = this;
var search = self.query().toLowerCase();
return ko.utils.arrayFilter(locations, function (loc) {
//I've tried adding an || query('') here, or an else if, but no luck
if (loc.title.toLowerCase().indexOf(search) >= 0 || (query(''))) {
loc.showLoc(true);
loc.visible = true;
} else {
loc.showLoc(false);
loc.visible = false;
setMarkerVisibility();
}
});
});
}
我在我的答案中提出了有關查詢變量的部分,但現在我看到了setMarkerVisibility()調用,我認爲這可能是主要問題。 – MUlferts