Skip to content

Instantly share code, notes, and snippets.

@leplatrem
Last active December 14, 2015 16:29
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save leplatrem/5115239 to your computer and use it in GitHub Desktop.
Save leplatrem/5115239 to your computer and use it in GitHub Desktop.
Leaflet.LayerIndex example
L.Map.include(L.LayerIndexMixin);
var map = L.map('map');
L.tileLayer('http://{s}.tile.osm.org/{z}/{x}/{y}.png').addTo(map);
map.fitWorld();
map.addControl(new L.Control.Information());
for (var i=0; i<5000; i++) {
var lat = Math.random() * 170 - 85
, lng = Math.random() * 350 - 175;
var layer = L.circleMarker(L.latLng([lat, lng])).setRadius(1).addTo(map);
map.indexLayer(layer);
}
map.fire('moveend');
<html>
<head>
<link rel="stylesheet" href="http://leafletjs.com/dist/leaflet.css" />
<!--[if lte IE 8]><link rel="stylesheet" href="http://leafletjs.com/dist/leaflet.ie.css" /><![endif]-->
<script>L_PREFER_CANVAS = true;</script>
</head>
<body>
<div id="map" style="height:300px"/>
<script src="http://leafletjs.com/dist/leaflet.js"></script>
<script src="https://raw.github.com/imbcmdth/RTree/master/src/rtree.js"></script>
<script src="https://raw.github.com/makinacorpus/Leaflet.LayerIndex/master/leaflet.layerindex.js"></script>
<script src="infocontrol.js"></script>
<script src="demo.js"></script>
</body>
</html>
L.Control.Information = L.Control.extend({
options: {
position: 'bottomright',
},
onAdd: function (map) {
this._container = L.DomUtil.create('div', 'leaflet-control-attribution');
L.DomEvent.disableClickPropagation(this._container);
map.on('moveend', function (e) {
if (map._loaded) {
var shown = map.search(map.getBounds());
console.log(map.getBounds().toBBoxString());
this._container.innerHTML = shown.length + ' objects shown.';
}
}, this);
return this._container;
},
onRemove: function (map) {
map.off('moveend');
},
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment