1

Multiple GeoJSON markers can be combined to one group layer then search on a specific attribute but doesn't work for GeoJSON polygons.

How to put multiple GeoJSON markers and polygons in one search box?

 var map = L.map('map', {
            zoom: 14,
            center: new L.latLng(35.948782,8.139912),
            layers: L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png')
        });
    //Geojson points 
    var piky1=new L.geoJson(pol00);
    var piky2=new L.geoJson(pol01);
    //Geojson polygones
    var piky3=new L.geoJson(pick1);
    var piky4=new L.geoJson(pick2);

      //group of Geojson points markers
      var lay=L.featureGroup([piky1,piky2]);
      //group of Geojson polygones
      var pik=L.featureGroup([piky3,piky4]);

   L.control.search({

   //If we consider searshing layers as the marker group
   //layer: pik,

   //If we consider searshing layers as the polygones group
    layer: lay,

   propertyName: 'Name',
    })
    .addTo(map);
1
  • Are you referring to filtering GeoJSON layers based on an attribute? What "search box" are you referring to?
    – Barbarossa
    Commented Jan 7, 2016 at 6:14

2 Answers 2

3

Searching in GeoJSON features on a specific "attribute", especially through a search box, is not part of standard Leaflet API as far as I know.

Do you mean you use a plugin that does not support polygons? If so, please can you kindly detail which plugin you are using?

I know Leaflet Control Search plugin has no problem searching into GeoJSON markers and other geometry types.

Demo with US States polygons: http://labs.easyblog.it/maps/leaflet-search/examples/geojson-layer.html


EDIT: following the sharing of your code

In your code, you build one GeoJSON Layer Group per GeoJSON data object (which contains a Feature Collection, with several points or polygons). Then you gather them into a single Feature Group, so that Leaflet Control Search can receive a single Layer Group in its layer option.

Unfortunately, current version of Leaflet Control Search badly handles nested GeoJSON Layer Groups: it correctly manages points (markers), but it fails on vector shapes because they do not have getLatLng method.

So you have 2 possible workarounds:

  1. Instead of building several GeoJSON Layer Groups and to nest them into a single Feature Group, directly build a single GeoJSON Layer Group (pass all your GeoJSON data objects as an array, like L.geoJson([pol00, pol01]). Then you can directly pass this Group into layer option of L.control.search factory. And Leaflet Control Search plugin will correctly search into the polygons.
// You can directly build a GeoJSON layer group with an array of GeoJSON objects:
var piky5 = L.geoJson([pol00, pol01]);
  1. Patch Leaflet Control Search plugin code to correctly handle nested Layer Groups with vector shapes inside. It is quite simple, even though it means many lines of code, but most of them are unchanged.
///////////////////////////
// Patch leaflet-search to manage nested GeoJSON shapes.
///////////////////////////

// Copy-paste into your script before instantiating L.control.search.
L.Control.Search.include({
  _recordsFromLayer: function() { //return table: key,value from layer
    var that = this,
      retRecords = {},
      propName = this.options.propertyName,
      loc;

    function searchInLayer(layer) {
      if (layer instanceof L.Control.Search.Marker) return;

      if (layer instanceof L.Marker || layer instanceof L.CircleMarker) {
        if (that._getPath(layer.options, propName)) {
          loc = layer.getLatLng();
          loc.layer = layer;
          retRecords[that._getPath(layer.options, propName)] = loc;

        } else if (that._getPath(layer.feature.properties, propName)) {

          loc = layer.getLatLng();
          loc.layer = layer;
          retRecords[that._getPath(layer.feature.properties, propName)] = loc;

        } else {
          throw new Error("propertyName '" + propName + "' not found in marker");
        }
      } else if (layer.hasOwnProperty('feature')) { //GeoJSON

        if (layer.feature.properties.hasOwnProperty(propName)) {
          loc = layer.getBounds().getCenter();
          loc.layer = layer;
          retRecords[layer.feature.properties[propName]] = loc;
        } else {
          throw new Error("propertyName '" + propName + "' not found in feature");
        }
      } else if (layer instanceof L.LayerGroup) {
        //TODO: Optimize
        layer.eachLayer(searchInLayer, this);
      }
    }

    this._layer.eachLayer(searchInLayer, this);

    return retRecords;
  }
});

Updated JSFiddle: http://jsfiddle.net/gevzoxs1/15/

EDIT: mentioned in repository issue #95 and submitted PR #101. Hopefully the plugin will be corrected soon and it should work for you with the new version without having to "patch" it in your script.

0

Yes with Leaflet Control Search plugin. I want to search over multiple Geojson I did it for points but for polygones Geojson doesn't work.? the plugin is the same you mentioned on the link.

3
  • If you use that plugin, it should work with polygons without issue. Maybe there is some subtlety in how you use those polygons. Please can you share some of your code? Otherwise it would be hard to help you further, as your issue is very probably dependent on your implementation. Bonus point if you can reproduce your issue in an online editing tool, like Plunker or JSFiddle for example.
    – ghybs
    Commented Jan 7, 2016 at 19:23
  • I am implementing my problem in JSFiddle here the link jsfiddle.net/roby04/gevzoxs1/14/#&togetherjs=eEaiLLbh4D when we consider the group layer GeoJson points it works but when we consider the group layer Geojson polygones it does't work ?
    – Rabie Ben
    Commented Jan 7, 2016 at 21:52
  • 1
    Thank you for the JSFiddle! Please consider editing your question to post your code in as well, so that future user can better understand what was your issue and the path to resolving it. I will edit my answer to propose a solution.
    – ghybs
    Commented Jan 8, 2016 at 5:51

Your Answer

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

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