Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
974 views
in Technique[技术] by (71.8m points)

google maps - Set marker visible with knockout JS ko.utils.arrayFilter

Hello guys I am trying to create an app that sets the appropriate markers visible when a knockout search is being done.

Basically the app is. When someone does a search the list that is bellow it, filters the list and makes only the markers that are associated with the filter list visible on the map. I have created a ko.utils.arrayFilter and I am trying to set only the item.marker.setVisible(true)

My Github link is https://github.com/Aimpotis/map3

Thank you again and much respect to the community it is helping me learn a lot

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

All you need is to set the visibility of the marker to match whether it is found:

if (!filter) {
  // this is new
  ko.utils.arrayForEach(self.listLoc(), function (item) {
    item.marker.setVisible(true);
  });
  return self.listLoc();
} else {
  return ko.utils.arrayFilter(self.listLoc(), function(item) {
    var result = (item.title.toLowerCase().search(filter) >= 0)
    item.marker.setVisible(result); // this is a new line
    return result;
  });
}

Working fiddle.

Note: unless you're supporting particularly old browsers, you can use the Array filter method rather than Knockout's arrayFilter util, and .foreach instead of arrayForEach.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...