I am attempting to add the following plugin to search a GeoJSON file I have open in Leaflet Javascript: http://labs.easyblog.it/maps/leaflet-search/examples/geojson-layer.html

I have brought in the search js and css files and called them, I just do not understand the syntax to incorporate it into the existing script.

Here is the script it needs to be able to search (the field is Sheet_Numb):

    // add GeoJSON layer and popups to the map once the file is loaded
    L.geoJson(data, {
   style: function (feature) {
       return {};
   onEachFeature: function (feature, layer) {
       layer.bindPopup(feature.properties.Sheet_Numb + "</br>" + "  " + feature.properties.LOC_Catalo);

I know I have to add the following somewhere:

var searchControl = new L.Control.Search({layer: featuresLayer, propertyName: 'name', circleLocation:false});

searchControl.on('search_locationfound', function(e) {

    e.layer.setStyle({fillColor: '#3f0', color: '#0f0'});

}).on('search_collapsed', function(e) {

    featuresLayer.eachLayer(function(layer) {   //restore feature color

map.addControl( searchControl );  //inizialize search control

You need to assign your L.geoJson layer to a named var outside the getJSON function in order to access it via controls. First, create it without any data, along with whatever options you want to apply to the features:

//create empty geoJson layer to be populated later
//styles, popups and other layer options can be specified here
var syriaLayer = L.geoJson(false, {
    style: function (feature) {
        return {};
    onEachFeature: function (feature, layer) {
        layer.bindPopup("Sheet Number: " + feature.properties.Sheet_Numb + "</br>" 
                      + "Catalog Record: " + "<a href = '" + feature.properties.LOC_Catalo + "'>" + feature.properties.LOC_Catalo + "</a>");

Then you can use addData to populate it with the data from your file:

$.getJSON("Syriashape.json", function (data) {
    // add GeoJSON data to layer and add to the map once the file is loaded

Then, because you have defined syriaLayer, you can refer to it (and Sheet_Numb) when you create your search control:

var searchControl = new L.Control.Search({layer: syriaLayer, propertyName: 'Sheet_Numb', circleLocation:false});

Here is a working fiddle that searches by state using the data from the example you linked to:


  • It is trying to function, however it looks like there is a small kink. Any comments on this question? gis.stackexchange.com/questions/165693/…
    – Map Man
    Oct 7 '15 at 18:46
  • I think I may see the problem. Because syriaLayer is defined within the function for getJson, it is confined to that scope and cannot be accessed globally, so my answer was incorrect. The solution is (probably) to define syriaLayer outside of the getJSON call and use an onAdd function to apply the bindings and styles. I will update my answer with the appropriate code later today when I have time to look into it in more detail. Oct 7 '15 at 20:27
  • OK; answer is now edited and includes an example fiddle. Oct 7 '15 at 21:31

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.