最新消息:雨落星辰是一个专注网站SEO优化、网站SEO诊断、搜索引擎研究、网络营销推广、网站策划运营及站长类的自媒体原创博客

javascript - I´m stucked creating a new Leaflet custom control - Stack Overflow

programmeradmin1浏览0评论

I´ve read the Leaflet doc and some online tutorials but nothing works for me.

I´m looking to add a new single button under the Leaflet zoom control (topleft) but can´t find the way to add it.

I´ve tried something like this:

    var control = L.Control.Button = L.Control.extend({
                options: {
                    position: 'topleft'
                },
                onAdd: function(map) {
                    this._map = map;
                    var container = L.DomUtil.create("div", "leaflet-control-button");
                    this._container = container;
                    return this._container;
                },
                onRemove: function(map) {},
    
            });

control.addTo(map);

The button function is show some data that I´ve get from an API (I almost have ready the function).

Please, someone help me, I would appreciate it so much!

I´ve read the Leaflet doc and some online tutorials but nothing works for me.

I´m looking to add a new single button under the Leaflet zoom control (topleft) but can´t find the way to add it.

I´ve tried something like this:

    var control = L.Control.Button = L.Control.extend({
                options: {
                    position: 'topleft'
                },
                onAdd: function(map) {
                    this._map = map;
                    var container = L.DomUtil.create("div", "leaflet-control-button");
                    this._container = container;
                    return this._container;
                },
                onRemove: function(map) {},
    
            });

control.addTo(map);

The button function is show some data that I´ve get from an API (I almost have ready the function).

Please, someone help me, I would appreciate it so much!

Share Improve this question asked Sep 24, 2020 at 12:12 B2DAWB2DAW 952 silver badges10 bronze badges
Add a ment  | 

2 Answers 2

Reset to default 8

You are on the right way. Add the leaflet control css class to the container, so it is correct displayed leaflet-bar leaflet-control

L.Control.Button = L.Control.extend({
    options: {
        position: 'topleft'
    },
    onAdd: function (map) {
        var container = L.DomUtil.create('div', 'leaflet-bar leaflet-control');
        var button = L.DomUtil.create('a', 'leaflet-control-button', container);
        L.DomEvent.disableClickPropagation(button);
        L.DomEvent.on(button, 'click', function(){
            console.log('click');
        });

        container.title = "Title";

        return container;
    },
    onRemove: function(map) {},
});
var control = new L.Control.Button()
control.addTo(map);

For "leaflet": "^1.9.4", I've used this code, for adding button:

 const customButton = L.control({ position: 'topleft' });
 customButton.onAdd = () => {
    const buttonDiv = L.DomUtil.create('div', 'button-wrapper');

    buttonDiv.innerHTML = `<button>Custom Button</button>`;
    buttonDiv.addEventListener('click', () => console.log('click'))
    return buttonDiv;
};
customButton.addTo(map)
发布评论

评论列表(0)

  1. 暂无评论