Leaflet getLayerTypeName
by Jay Cummins
HTML
<link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css">
<script src="https://unpkg.com/[email protected]/dist/leaflet.js"
integrity="sha512-A7vV8IFfih/D732iSSKi20u/ooOfj/AGehOKq0f4vLT1Zr2Y+RX7C+w8A1gaSasGtRUZpF/NZgzSAu4/Gc41Lg=="
crossorigin=""></script>
<script src="https://unpkg.com/[email protected]"></script>
<script
src="https://code.jquery.com/jquery-3.2.1.js"
integrity="sha256-DZAnKJ/6XZ9si04Hgrsxu/8s717jcIzLy3oi35EouyE="
crossorigin="anonymous"></script>
<div id="map"></div>
<div id="debugDiv"></div>
CSS
#map {
height: 400px;
}
JavaScript
// Create the map
var map = L.map('map').setView([39.5, -77.03], 5);
// Set up the OSM layer
L.tileLayer(
'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
{maxZoom: 18}).addTo(map);
L.esri.dynamicMapLayer({
url: 'https://services.arcgisonline.com/arcgis/rest/services/Demographics/USA_Median_Age/MapServer',
opacity: 0.7
}).addTo(map);
// add a marker in the given location
L.marker([36.83711,-77.464459]).addTo(map);
if (typeof console != "undefined")
if (typeof console.log != 'undefined')
console.olog = console.log;
else
console.olog = function() {};
console.log = function(message) {
console.olog(message);
$('#debugDiv').append('<p>' + message + '</p>');
};
console.error = console.debug = console.info = console.log;
function getLayerTypeName(layer)
{
if (layer instanceof L.Marker){
return 'Marker';
}
else if (layer instanceof L.Tooltip){
return 'Tooltip';
}
else if (layer instanceof L.esri.DynamicMapLayer){
return 'DynamicMapLayer';
}
else if (layer instanceof L.esri.BasemapLayer){
return 'BasemapLayer';
}
else if (layer instanceof L.Layer){
return 'Layer';
}
else
{
return 'Unknown';
}
}
map.eachLayer(function(layer) {
console.log('_leaflet_id=' + layer._leaflet_id + ' is layer type= '+ getLayerTypeName(layer));
});