Giter VIP home page Giter VIP logo

leaflet.activelayers's Introduction

Leaflet.ActiveLayers

It is a plugin for the Leaflet library. This plugin adds new L.Control.ActiveLayers with functionality to get currently active layers on the map.

Supports Leaflet 0.7.x and 1.0+.

Example usage

Also see example.html

var attribution = '&copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>'

mapnikLayer = L.tileLayer(
    'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
    {attribution: attribution}
)
var blackAndWhite = L.tileLayer(
    'http://{s}.www.toolserver.org/tiles/bw-mapnik/{z}/{x}/{y}.png',
    {attribution: attribution}
)
var clouds = L.tileLayer('http://{s}.tile.openweathermap.org/map/clouds/{z}/{x}/{y}.png', {
    attribution: 'Map data &copy; <a href="http://openweathermap.org">OpenWeatherMap</a>',
    opacity: 0.5
})

map = L.map('map', {
    center: new L.LatLng(39.73, -104.99),
    zoom: 10,
    layers: [mapnikLayer, clouds]
})

var baseLayers = {
    'Mapnik': mapnikLayer,
    'Black and Whilte': blackAndWhite
}
var overlayLayers = {
    'Clouds': clouds
}

var control = L.control.activeLayers(baseLayers, overlayLayers)
control.addTo(map)

// 'Mapnik'
console.log(control.getActiveBaseLayer().name)

//Clouds
var overlayLayers = control.getActiveOverlayLayers()
for (var overlayId in overlayLayers) {
    console.log(overlayLayers[overlayId].name)
}

API

get active base layer

control.getActiveBaseLayer()

return object LL:

{
    name: 'layer name on the control',
    layer: L.TileLayer,
    overlay: is overlay layer
}

get active overlay layers

control.getActiveOverlayLayers()

return object:

{layerId: LL}

where LL is the object defined in the previous method and layerId is L.stamp(LL.layer)

leaflet.activelayers's People

Contributors

chugcup avatar dependabot[bot] avatar mpuputti avatar robertd avatar vogdb 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

Watchers

 avatar  avatar  avatar  avatar  avatar

leaflet.activelayers's Issues

ActiveLayers fails with new Leaflet versions

Function _recountLayers accesses function this._form.getElementsByTagName.
With newer versions of Leaflet, this code fails, because Leaflet has changed _form to _section.
I think the change was in Leaflet version 1.4.0.

That is, line 108 in ActiveLayers.js should be changed from
inputs = this._form.getElementsByTagName('input'),
to
inputs = this._section.getElementsByTagName('input'),

Is getActiveOverlayLayers() asynchronous?

I'm attempting something quite simple using the following code:

currentActiveLayers = control.getActiveOverlayLayers();

currentLayerList = [];
for (var layer in currentActiveLayers) {
if (currentActiveLayers.hasOwnProperty(layer)) {
currentLayerList.push(currentActiveLayers[layer]);
}
}

This code is within an 'onadd' event for the map object. However, when logging the 'currentLayerList', it will always show the state of the list on the previous event. E.g if the map starts with 2 active layers, when turning a new one on using the control, the list will show two layers, when adding a second (4 total active layers now) it will show 3. I guess this is because it is continuing on with the creation of 'currentLayerList' before 'getActiveOverlayLayers()' has finished executing, and so uses the previous 'currentActiveLayers' object to build the list (currentActiveLayers is global). Is this true or is getActiveOverlayLayers synchronous and the problem is elsewhere?

Thanks,

Please add versioning

While trying to add your component to rails assets (https://rails-assets.org/components), I get this error:

vogdb/Leaflet.ActiveLayers has no versions defined. Please create an issue in component's repository.

Please add a SymVer, so your component gets the fame and adoration it deserves.

cant load ActiveLayers

Hi.. i try to load ActiveLayers in my html script section but i get
Uncaught SyntaxError: Unexpected token < ActiveLayers.js:7

And therefore (i think):
<
trackmobile.js:124 Uncaught TypeError: L.control.activeLayers is not a function

Hmmm

Tomber

Leaflet 1.0.3 issue

Hi!

I'm currently using the activeLayers plugin with leaflet 0.7, but I would like to update to leaflet 1.0
I took the latest activeLayers version (0.3.0) and the lastest leaflet version (1.0.3), but I'm having troubles with it.

This is how I implement the plugin:

layers = L.control.activeLayers(baseLayers, topLayers);
layers.addTo(map);

Using leaflet-src.js and src/activeLayers.js I get the following error in the console:

Uncaught TypeError: layer.on is not a function
    at NewClass._addLayer (Control.Layers.js:223)
    at NewClass.initialize (Control.Layers.js:86)
    at new NewClass (Class.js:21)
    at Function.L.control.activeLayers (activeLayers.js:136)
    at init (main.js:410)

Can you help me, I ran out of ideas...
Thanks, Philipp!

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.