简体   繁体   中英

LeafLet Error : Object #<Object> has no method 'onAdd' at file:///android_asset/www/leaflet.js:6

I'm trying to understand how Leaflet works. I'm trying this tutorial. I deleted some code from tutorial for more clean code. Now have a problem with L.control.layers .

If i want to add overlay like this

var overlays = {
          "Cities": cities
            };

var layersControl = L.control.layers(null, overlays);

There is no problem with adding overlays to control. With this code, I can show/hide layer with clicking checkbox

But if I want to add overlay like this

 var overlays = {
              "Cities": cities
                };
var layersControl = L.control.layers();
layersControl.addOverlay(overlays);

There is problem with overlays. Here when i want to show/hide layer with clicking checkbox, an error appearing.

Uncaught TypeError: Object # has no method 'onAdd' at file:///android_asset/www/leaflet.js:6

Uncaught TypeError: Object # has no method 'onRemove' at file:///android_asset/www/leaflet.js:6

I have to use 2. piece of code. Can anyone help me ?

Full code is here

var cities = new L.LayerGroup();

 L.marker([39.61, -105.02]).bindPopup('This is Littleton, CO.').addTo(cities),
            L.marker([39.74, -104.99]).bindPopup('This is Denver, CO.').addTo(cities),
            L.marker([39.73, -104.8]).bindPopup('This is Aurora, CO.').addTo(cities),
            L.marker([39.77, -105.23]).bindPopup('This is Golden, CO.').addTo(cities);


            var cmAttr = 'Map data &copy; 2011 OpenStreetMap contributors, Imagery &copy; 2011 CloudMade',
                cmUrl = 'http://{s}.tile.cloudmade.com/BC9A493B41014CAABB98F0471D759707/{styleId}/256/{z}/{x}/{y}.png';



            var map = L.map('map');


            map.addLayer(cities,true);
            map.setView([39.73, -104.99],10);

            var overlays = {

                "Cities": cities

            };

    var layersControl = L.control.layers(null, overlays);

     layersControl.setPosition("bottomleft");

     layersControl.addTo(map);

I found solution myself.

I've added cities object directly instead of trying to add overlays

var layersControl = L.control.layers();
layersControl.addOverlay(cities,'Cities');

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM