0

我正在使用 Leaflet Search 和 Fuse.js 来搜索多个传单层和每个层中的多个 feature.properties。我终于让代码工作了(但它真的很乱)。唯一的问题是,因为我使用的是 filterData,所以当我搜索任何内容而不是完全匹配时,它会显示所有标记。

有什么办法只显示完全匹配?

var fuseOptions = {
  shouldSort: true,
  tokenize: true,
  threshold: 0,
  location: 0,
  maxPatternLength: 32,
  minMatchCharLength: 2,
  keys: ['properties.myKey']
};

    var fuse = new Fuse(city.features.concat(village.features.concat(gateway.features.concat(trade.features.concat(danger.features.concat(connect.features.concat(forest.features.concat(mine.features.concat(office.features.concat(farm.features.concat(fish.features.concat(annex.features)))))))))), fuseOptions), {
        keys: [
            'properties.name',
            'properties.prod1',
            'properties.extraprod1',
            'properties.extraprod2',
            'properties.extraprod3',
            'properties.extraprod4',
            'properties.extraprod5',
            'properties.buff',
            //'properties.operator'
        ]
    });
    L.control.search({
           initial: false,
           layer: allLayers,
           collapsed: false,
           casesensitive: false,
           propertyName: 'name',
           container: 'findbox',
       filterData: function(text, records) {
            var jsons = fuse.search(text),
                ret = {}, key;
            
            for(var i in jsons) {
                key = jsons[i].properties.name;
                ret[ key ]= records[key];
            }

            console.log(jsons,ret);
            return ret;
        }
    })
    .on('search:locationfound', function(e) {
        e.layer.openPopup();
    })
    .addTo(map);
4

1 回答 1

0

我不确定这是否有效,但请尝试:

filterData: function(text, records) {
            var jsons = fuse.search(text),
                ret = {}, key;
            
            for(var i in jsons) {
                key = jsons[i].properties.name;
                if(key.startsWith(text)){
                    ret[ key ]= records[key];
                }
                // Try with and without this line
                else { rect[key] = null; }
            }

            console.log(jsons,ret);
            return ret;
        }
于 2020-07-13T06:35:05.790 回答