makinacorpus/Leaflet.StyledLayerControl

Name: Leaflet.StyledLayerControl

Owner: Makina Corpus

Description: A styled Leaflet plugin that implements the management and control of layers by organization into categories or groups

Forked from: davicustodio/Leaflet.StyledLayerControl

Created: 2017-05-16 12:52:28.0

Updated: 2017-12-03 16:11:09.0

Pushed: 2017-08-29 15:06:42.0

Homepage: null

Size: 2083

Language: JavaScript

GitHub Committers

UserMost Recent Commit# Commits

Other Committers

UserEmailMost Recent Commit# Commits

README

Leaflet.StyledLayerControl

What is Leaflet.StyledLayerControl?

A Leaflet plugin that implements the management and control of layers by organization into categories or groups. The StyledLayerControl class extends the original L.control.layers control. The plugin uses HTML5 and CSS3 to style the presentation in a modern way. The initial ideas were based in the plugin: Leaflet.Groupedlayercontrol

preview

Tested with Leaflet 0.7.3

Main features
Live Demos
How to use?

1 - Create the reference to Leaflet

<link rel="stylesheet" href="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.css" />
<script src="http://cdn.leafletjs.com/leaflet-0.7.3/leaflet.js"></script>

2 - Insert references to styledLayerControl.css and styledLayerControl.js

<link rel="stylesheet" href="../css/styledLayerControl.css" />
<script src="../src/styledLayerControl.js"></script>

3 - Define your layers (base maps and overlays)

// Google layers
var g_roadmap   = new L.Google('ROADMAP');
var g_satellite = new L.Google('SATELLITE');
var g_terrain   = new L.Google('TERRAIN');

// OSM layers
var osmUrl='http://{s}.tile.osm.org/{z}/{x}/{y}.png';
var osmAttrib='Map data © <a href="http://openstreetmap.org">OpenStreetMap</a> contributors';
var osm = new L.TileLayer(osmUrl, {attribution: osmAttrib});

// ... more Base Maps

// Sao Paulo Soybeans Plant
var soybeans_sp = new L.LayerGroup();
L.marker([-22, -49.80]).addTo(soybeans_sp),
L.marker([-23, -49.10]).addTo(soybeans_sp),
L.marker([-21, -49.50]).addTo(soybeans_sp);

// Rio de Janeiro Corn Plant
var corn_rj = new L.LayerGroup();
L.marker([-22, -43.20]).addTo(corn_rj),
L.marker([-23, -43.50]).addTo(corn_rj);

// ... more Overlays

4 - Create the Leaflet Map Object and add the layer that will be default basemap

var map = L.map('map', {
    center: [-16, -54],
    zoom: 4
});

map.addLayer(g_roadmap);

5 - Define structure of groups and layers of basemap

var baseMaps = [
                { 
                    groupName : "Google Base Maps",
                    expanded : true,
                    layers    : {
                        "Satellite" :  g_satellite,
                        "Road Map"  :  g_roadmap,
                        "Terreno"   :  g_terrain
                    }
                }, {
                    groupName : "OSM Base Maps",
                    layers    : {
                        "OpenStreetMaps" : osm
                    }
                }, {
                    groupName : "Bing Base Maps",
                    layers    : {
                        "Satellite" : bing1,
                        "Road"      : bing2
                    }
                }                           
];  

5 - Define structure of groups and layers of overlays

var overlays = [
                 {
                    groupName : "Sao Paulo",
                    expanded  : true,
                    layers    : { 
                        "Soybeans Plant" : soybeans_sp,
                        "Corn Plant"     : corn_sp
                    }   
                 }, {
                    groupName : "Rio de Janeiro",
                    expanded  : true,
                    layers    : { 
                        "Bean Plant"     : bean_rj,
                        "Corn Plant"     : corn_rj,
                        "Rice Plant"     : rice_rj      
                    }   
                 }, {
                    groupName : "Belo Horizonte",
                    layers    : { 
                        "Sugar Cane Plant"  : sugar_bh,
                        "Corn Plant"        : corn_bh       
                    }   
                 }                           
];

6 - Declare which layers can be deleted and visible (create the removable property with true in the options StyledLayerControl that can be created in the layer object). Each layer declared as removable = true will show an icon to delete the user to remove the layer

soybeans_sp.StyledLayerControl = {
    removable : true,
    visible : false
}
// ... more layers

7 - Define the options for StyledLayerControl

var options = {
    container_width     : "300px",
    container_maxHeight : "350px", 
    group_maxHeight     : "80px",
    exclusive           : false
};

8 - Create the StyledLayerControl

var control = L.Control.styledLayerControl(baseMaps, overlays, options);
map.addControl(control);
How to add and remove layers and groups dynamically ?
How to select and unSelect layers dynamically ?
How to select and unSelect group layers dynamically ?
License

Creative Commons License
This work is licensed under a Creative Commons Attribution 3.0 Unported License.


This work is supported by the National Institutes of Health's National Center for Advancing Translational Sciences, Grant Number U24TR002306. This work is solely the responsibility of the creators and does not necessarily represent the official views of the National Institutes of Health.