淘汰赛 - Google地图标记过滤器

问题描述:

我正在创建一个默认显示几个标记的谷歌地图。当用户进入特定位置时,只有该位置的标记必须保留,其余的作品必须消失。虽然标记和映射工作,过滤器的功能是不是working.My JS代码如下:淘汰赛 - Google地图标记过滤器

function inintMap() 
{ 

var map; 
var bounds = new google.maps.LatLngBounds(); 
var mapOptions = { 
    mapTypeId : 'roadmap' 
}; 

map = new google.maps.Map(document.getElementById('map'),mapOptions); 
map.setTilt(45); 

var locations = [ 
     ['London Eye, London', 51.503454,-0.119562], 
     ['Palace of Westminster, London', 51.499633,-0.124755] 
    ]; 



for (i=0; i < locations.length; i++) 
{ var position = new google.maps.LatLng(locations [i][1],locations [i][2]); 
bounds.extend (position); 
marker = new google.maps.Marker({ 
    position : position, 
    map : map, 
    title : locations [i][0] 
}); 


     map.fitBounds(bounds); 


} 



var vm = { 
    locations: ko.observableArray(locations) 
}; 


vm.query = ko.observable('') 
vm.search = function(value){ 

    vm.locations.removeAll() 

    for(var x in locations) { 
     if(locations[x].name.toLowerCase().indexOf(value.toLowerCase()) >= 0) { 
     viewModel.locations.push(locations[x]); 
     } 
    } 
    } 


vm.query.subscribe(vm.search); 
ko.applyBindings(vm); 
} 
+0

http://*.com/questions/500504/why-is-using-for-in-with-array-iteration-a-bad-idea –

+0

在控制台中是否有任何错误?它以什么方式不起作用? –

vm.locations.removeAll()。从该代码中,您甚至可以在搜索之前删除所有位置。先搜索并将搜索到的位置放入另一个数组,然后您可以用新数组替换位置。

vm.search = function(value){ 
    var tempLocations = []; 
    for(var x in vm.locations()) { 
     if(vm.locations()[x].name.toLowerCase().indexOf(value.toLowerCase()) >= 0) { 
     tempLocations.push(vm.locations()[x]); 
     } 
    } 
    vm.locations(tempLocations); 
} 

另外,还要注意的是指observablearrays的时候,你需要把它作为调用的函数,不要忘记你的括号。