0

I have a map with a single GeoJSON layer that I referenced in my script (using the AJAX plugin). Now I am trying to add search functionality to the map so that the user can search for features by name. The plugin I am using is the Leaflet Search plugin.

However, I cannot get the search to perform, as it's giving me the "Location not found" error, whenever I type text into the search box. Here is my code:

searchlayer = L.geoJSON.ajax('data.json').addTo(map); 
map.addControl(new L.Control.Search({layer:searchlayer});

What am I doing wrong here? I assume that I'm not properly linking the layer and the field to be searched to the search control, but how do I do that?

Found that the issue was not adding the option for the control to search the geojson attribute. The default column that the search button works on is "title". I added the option of propertyName: 'Location' to L.Control.Search and it works perfect now!

1

Might be problem with GeoJSON data not being available yet when layer is added to control. Try with this:

searchlayer = L.geoJSON.ajax('data.json').addTo(map);
searchlayer.on('data:loaded', function() {
  map.addControl(new L.Control.Search({layer:searchlayer});
});
  • This would probably be a better way to write my code. But turns out the issue was not adding the option of propertyName. It was searching the geoJSON for a column named "title" according to default rules. I simply changed it to the attrobute I wanted and it works. – cesium133 Mar 16 at 23:39
  • 1
    Good! I see you also edited your question and added the solution to your problem. It's nice in such a case to mark somehow the added/changed part, for example: EDIT (in bold). – TomazicM Mar 17 at 9:34

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.