Giter VIP home page Giter VIP logo

leaflet.layerindex's Introduction

Leaflet.LayerIndex

Efficient spatial index for Leaflet layers. It works recursively for L.FeatureGroup objects.

Requires the Magnificient RTree.js

Check out the live demo

Usage

On L.Map objects


    L.Map.include(L.LayerIndexMixin);

    var map = L.map(...);
    ...
    var layer = L.GeoJSON(data).addTo(map);
    map.indexLayer(layer);

    // Search visible features for example
    map.on('moveend', function () {
        var shown = map.search(map.getBounds());
        console.log(shown.length + ' objects shown.');
    });

    // remove the spatial index when the layer is unused
    map.unindexLayer(layer);

map.unindexLayer() function accepts an options object as an optional second parameter to define the bounds to be removed from the index. It should have one of the following attributes:

  • options.bounds - a leaflet latLngBounds object
  • options.latlng - a leaflet latLng object
  • options.latlngs - an array of leaflet latLng objects

If no option is provided, the function will use the bounds of the layer's geometry.

Using inherited class


    L.IndexedGeoJSON = L.GeoJSON.extend({
        includes: L.LayerIndexMixin,

        initialize: function (geojson, options) {
            // Decorate onEachFeature to index layers
            var onEachFeature = function (geojson, layer) {
                this.indexLayer(layer);
                if (this._onEachFeature) this._onEachFeature(geojson, layer);
            };
            this._onEachFeature = options.onEachFeature;
            options.onEachFeature = L.Util.bind(onFeatureParse, this);

            // Parent initialization
            L.GeoJSON.prototype.initialize.call(this, geojson, options);
        },

        removeLayer: function (layer) {
          if (this.hasLayer(layer)) {
            this.unindexLayer(layer)
          }
          return window.L.GeoJSON.prototype.removeLayer.call(this, layer)
        }
    });


    var layer = L.IndexedGeoJSON(data).addTo(map);

    var aroundToulouse = layer.searchBuffer(L.latLng([43.60, 1.44]), 0.1);

Changelog

0.0.2

  • Add bower.json

0.0.1

  • Initial version

Authors

Makina Corpus

leaflet.layerindex's People

Contributors

fredericbonifas avatar leplatrem avatar matt-in-a-hat avatar

Stargazers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

Watchers

 avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar  avatar

leaflet.layerindex's Issues

Removing layers using inherited class

I was having an issue using the L.IndexedGeoJSON inherited class from the example in the readme, where removing layers from the group was not removing them from the map.

It seems that the index layer was caching them, so adding the following to the L.GeoJSON.extend({ fixed it.

removeLayer: function (layer) {
  if (this.hasLayer(layer)) {
    this.unindexLayer(layer)
  }
  return window.L.GeoJSON.prototype.removeLayer.call(this, layer)
}

Now when I call myIndexedLayer.clearLayers() it actually removes them. Just thought this might be useful to someone else.

Unclear (to me) how to use the inherited class

I'm trying to use layerindex with Leaflet.Snap and I'm having some trouble understanding how to implement the inherited class.

This is the snippet from the README:

  L.IndexedGeoJSON = L.GeoJSON.extend({
       includes: L.LayerIndexMixin,

       initialize: function (geojson, options) {
           // Decorate onEachFeature to index layers
           var onEachFeature = function (geojson, layer) {
               this.indexLayer(layer);
               if (this._onEachFeature) this._onEachFeature(geojson, layer);
           };
           this._onEachFeature = options.onEachFeature;
           options.onEachFeature = L.Util.bind(onFeatureParse, this);

           // Parent initialization
           L.GeoJSON.prototype.initialize.call(this, geojson, options);
       }
   });


   var layer = L.IndexedGeoJSON(data).addTo(map);

   var aroundToulouse = layer.searchBuffer(L.latLng([43.60, 1.44]), 0.1);

onFeatureParse is not defined. Is it a function I should provide? What is it supposed to do?

Demo not working on iPhone 5s

Tried to view the demo on my iPhone 5s, both in Chrome and Safari - but no markers are drawn.
The counter works, so the script is running - but nothing is drawn on the map - only the base map layer.

GeoJSON seen as just one object?

I have a GeoJSON FeaturCollection with around 5000 LineStrings. I tried adding it per the example code in the readme:

L.Map.include(L.LayerIndexMixin);

var map = L.map(...);
...
var layer = L.GeoJSON(data).addTo(map);
map.indexLayer(layer);

// Search visible features for example
map.on('moveend', function () {
    var shown = map.search(map.getBounds());
    console.log(shown.length + ' objects shown.');
});

But the console.log only ever outputs 1.

Am I supposed to add each feature as its own layer? (as is done with the circle markers in the live demo)

Recommend Projects

  • React photo React

    A declarative, efficient, and flexible JavaScript library for building user interfaces.

  • Vue.js photo Vue.js

    ๐Ÿ–– Vue.js is a progressive, incrementally-adoptable JavaScript framework for building UI on the web.

  • Typescript photo Typescript

    TypeScript is a superset of JavaScript that compiles to clean JavaScript output.

  • TensorFlow photo TensorFlow

    An Open Source Machine Learning Framework for Everyone

  • Django photo Django

    The Web framework for perfectionists with deadlines.

  • D3 photo D3

    Bring data to life with SVG, Canvas and HTML. ๐Ÿ“Š๐Ÿ“ˆ๐ŸŽ‰

Recommend Topics

  • javascript

    JavaScript (JS) is a lightweight interpreted programming language with first-class functions.

  • web

    Some thing interesting about web. New door for the world.

  • server

    A server is a program made to process requests and deliver data to clients.

  • Machine learning

    Machine learning is a way of modeling and interpreting data that allows a piece of software to respond intelligently.

  • Game

    Some thing interesting about game, make everyone happy.

Recommend Org

  • Facebook photo Facebook

    We are working to build community through open source technology. NB: members must have two-factor auth.

  • Microsoft photo Microsoft

    Open source projects and samples from Microsoft.

  • Google photo Google

    Google โค๏ธ Open Source for everyone.

  • D3 photo D3

    Data-Driven Documents codes.