Hey there Ishmael
This was a couple months ago, but maybe still relevant.
My issues are different than yours, but I think we're after the same thing — the ability to gather
all the "Layer" objects (which can be icons, shapes, or tiles) at any point to manipulate them
(or gather info from them). For me, I need to assign properties to icons such that when they're
clicked, I can affect other JS components on the page...
Here's what I came up with --- maybe it's relevant to you.
First, I created a method to get all the Layer objects in the map ------ these
include the icons, but also shapes and tiles.
L.Map.prototype.getLayers = function() {
return this._layers;
}
Also (as I don't see this anywhere in the API, but maybe it's there) I made a method to see whether
any object with latlng values is visible in the frame.
function isInBounds(latlng, bounds) {
var is_in_x_view = (latlng.lng < bounds._northEast.lng && latlng.lng > bounds._southWest.lng);
var is_in_y_view = (latlng.lat < bounds._northEast.lat && latlng.lat > bounds._southWest.lat);
return (is_in_y_view && is_in_x_view);
}
Finally I created a method to inspect the situation when the map is loaded, or when zoom/scope
changes (using Underscore.js's "each()" for traversing the Layers). I also set a "test_id" value on
every icon in the icon options when they were first created so that I'd have a hook into some related data.
// m here is the central map object
function inspectLayers(m) {
var bounds = m.getBounds();
var layers = m.getLayers();
var $layer_icon = {};
var inBnds = false;
var test_id = "";
_.each (layers, function (layer) {
if (typeof layer._icon != "undefined") {
$layer_icon = $(layer._icon);
test_id = layer.options.test_id;
inBnds = isInBounds(layer._latlng, bounds);
console.log("is (id:" + test_id + ") marker in bounds?:", inBnds);
// if inBnds is true, you can manipulate the $layer_iconn
// DOM object or do something else with layer.options data
}
});
}
I'd love to see your plugin, if you're able to share.
Best,
Michael Richardson
tags (getLayers, get layers, is an icon in bounds)