2

Edit: I am working on a project where I want to search through a GeoJSON, based on different attributes. To do this I am using leaflet-search plugin.

I want to search through my single GeoJSON. That GeoJSON has multiple attributes. I'm wondering if we could pass an array through the propertyName option.

var controlSearch = new L.Control.Search({
 layer: univ,
 initial: false,
 collapsed: true,
 textPlaceholder: 'Search for University or Department',
 propertyName: ['Institutio','Department'] ,
 hideMarkerOnCollapse: true,
 moveToLocation:function(latlng, title, map) {
 latlng.layer.setIcon(theIcon())
 map.setView(latlng, 13); 
 }, 
 marker: false,
});

I've tried passing both my attributes as an array but that doesn't work. Does anyone have a solution?

TomazicM
27.3k25 gold badges33 silver badges43 bronze badges
asked Jun 26, 2022 at 16:30
8
  • I'm using this plugin: github.com/stefanocudini/leaflet-search Commented Jun 26, 2022 at 16:33
  • I've edited it. Do you have any idea how to tackle this issue? Commented Jun 26, 2022 at 16:53
  • There is a simple solution if you don't mind drop down list of candidates and result itself containing both properties. Commented Jun 26, 2022 at 17:02
  • This means the user decides which property to search through... Commented Jun 26, 2022 at 17:05
  • No both are displayed one besides the other, like "Some Institute, Some department". Commented Jun 26, 2022 at 17:10

1 Answer 1

2

Trick to search through multiple properties is to create combined search property when loading GeoJSON layer.

Code could look something like this:

var univ = L.geoJSON(data, {
 onEachFeature: function(feature, layer) {
 layer.feature.properties.searchItem = layer.feature.properties.Institutio + ', ' + layer.feature.properties.Department;
 }
};
var controlSearch = new L.Control.Search({
 layer: univ,
 initial: false,
 collapsed: true,
 textPlaceholder: 'Search for University or Department',
 propertyName: 'searchItem' ,
 hideMarkerOnCollapse: true,
 moveToLocation:function(latlng, title, map) {
 latlng.layer.setIcon(theIcon())
 map.setView(latlng, 13); 
 }, 
 marker: false,
});
answered Jun 26, 2022 at 18:50
1
  • Thank you. This works perfectly. Commented Jun 27, 2022 at 4:55

Your Answer

Draft saved
Draft discarded

Sign up or log in

Sign up using Google
Sign up using Email and Password

Post as a guest

Required, but never shown

Post as a guest

Required, but never shown

By clicking "Post Your Answer", you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.