stefanocudini / leaflet-search

Search stuff in a Leaflet map
https://opengeo.tech/maps/leaflet-search/
MIT License
772 stars 334 forks source link

Is there a way to limit the search result displayed in the search bar when searching multiple properties? #332

Open muppetdance opened 5 months ago

muppetdance commented 5 months ago

I want to search multiple properties in my geoJSON, namely 'name' and 'address'. I can achieve this using

layer.feature.properties.searchTerm =layer.feature.properties.name + layer.feature.properties.address;

in the onEachFeature function, and then in the controlSearch,

var controlSearch = new L.Control.Search({ position:'topright', layer: featuresLayer, propertyName: 'searchTerm', initial: false, marker: false, minLength: 3, zoom: 16 });

The problem is that both the name and address properties appear in the results and the search bar when a result is clicked. Is there a way to restrict the text in the search bar to just the name property?

If I add:

buildTip: customTip,

to the controlSearch and create the following function:

function customTip(text,val) { return '<a href="#">'+text+'. adjust</a>'; }

then the search results are restricted to displaying just the name (though I can't work out why this might be the case).