Removing Map Pin with Search

后端 未结 3 1803
-上瘾入骨i
-上瘾入骨i 2021-01-19 02:43

I\'m trying to create a search bar that filters out items from a list if they do not match the search query. The additional functionality that I\'m trying to add is that if

3条回答
  •  失恋的感觉
    2021-01-19 03:22

    I can't simulate this, but after read your code I assume this code will work.. First add marker as your pin property:

    self.mapPin = function (name, lat, lon, text) {
        this.name = ko.observable(name);
        this.lat = ko.observable(lat);
        this.lon = ko.observable(lon);
        this.text = ko.observable(text);
        this.marker = new google.maps.Marker({
            position: new google.maps.LatLng(lat, lon),
            map: map,
            animation: google.maps.Animation.DROP
        });
    
        // removed for clarity
    };
    

    then set pin marker visibility while filtering your pins

    self.filterPins = ko.computed(function () {
        var search = self.query().toLowerCase();
    
        return ko.utils.arrayFilter(self.pins(), function (pin) {
            var match = pin.name().toLowerCase().indexOf(search) >= 0;
    
            pin.marker.setVisible(match); // maps API hide call
            return match;
        });
    });
    

    Google Maps Marker Documentation

提交回复
热议问题