Skip to content

Instantly share code, notes, and snippets.

@js1568
Last active December 20, 2015 11:39
Show Gist options
  • Save js1568/6124574 to your computer and use it in GitHub Desktop.
Save js1568/6124574 to your computer and use it in GitHub Desktop.
Leaflet Identify function (similar to ESRI ArcGIS Javascript API function) for finding polygons based on a given latlng from in a L.GeoJson feature layer group.Requires Leaflet and geojson-js-utils: https://github.com/maxogden/geojson-js-utils
L.GeoJSON.include({
identify: function(latlng) {
var features = new L.FeatureGroup(),
geopoint = {
type: 'Point',
coordinates: [latlng.lng, latlng.lat]
};
this.eachLayer(function (layer) {
if (gju.pointInPolygon(geopoint, layer.feature.geometry)) {
features.addLayer(layer);
}
});
return features;
}
});
@js1568
Copy link
Author

js1568 commented Dec 16, 2013

Example for use:

geojson.on('click', function (e) {
  var layers = this.identify(e.latlng),
    panel = $('resultsPanel').empty();
  layers.eachLayer(function (layer) {
    panel.append(tmpl(layer.feature.properties));
  });
});

@Zverik
Copy link

Zverik commented Feb 13, 2014

You could have written L.GeoJSON.include({...}) :)

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment