javascript - 使用自定义按钮扩展 L.control.zoom

标签 javascript inheritance leaflet overriding extends

我正在寻找一种向 L.control.zoom 添加额外按钮的方法。 Leaflet 正在从 CDN 加载,我使用的是普通 Javascript(没有预处理器或任何东西)。

我希望有类似 L.control.zoom.extend({}) 的东西,但不幸的是这并不存在。尝试 L.Control.extend({...L.control.zoom}) 也不起作用。

对于上下文,通过复制粘贴 original code 来完成此操作。在第 42 行添加自定义按钮的代码如下所示:

let zoomControls = L.Control.extend({
    // @section
    // @aka Control.Zoom options
    options: {
        position: 'topleft',

        // @option zoomInText: String = '+'
        // The text set on the 'zoom in' button.
        zoomInText: '+',

        // @option zoomInTitle: String = 'Zoom in'
        // The title set on the 'zoom in' button.
        zoomInTitle: 'Zoom in',

        // @option zoomOutText: String = '−'
        // The text set on the 'zoom out' button.
        zoomOutText: '−',

        // @option zoomOutTitle: String = 'Zoom out'
        // The title set on the 'zoom out' button.
        zoomOutTitle: 'Zoom out'
    },

    onAdd: function (map) {
        var zoomName = 'leaflet-control-zoom',
            container = L.DomUtil.create('div', zoomName + ' leaflet-bar'),
            options = this.options;

        let locationLink = L.DomUtil.create('a', 'leaflet-bar-part leaflet-bar-part-single', container);
        L.DomEvent.disableClickPropagation(locationLink);
        locationLink.title = 'My location';
        let locationIcon = L.DomUtil.create('span', 'fa-lg fas fa-map-marker-alt', locationLink);
        L.DomEvent.on(locationLink, 'click', () => {
            alert('BUTTON CLICKED');
        });
        this._zoomInButton  = this._createButton(options.zoomInText, options.zoomInTitle,
                zoomName + '-in',  container, this._zoomIn);
        this._zoomOutButton = this._createButton(options.zoomOutText, options.zoomOutTitle,
                zoomName + '-out', container, this._zoomOut);

        this._updateDisabled();
        map.on('zoomend zoomlevelschange', this._updateDisabled, this);

        return container;
    },

    onRemove: function (map) {
        map.off('zoomend zoomlevelschange', this._updateDisabled, this);
    },

    disable: function () {
        this._disabled = true;
        this._updateDisabled();
        return this;
    },

    enable: function () {
        this._disabled = false;
        this._updateDisabled();
        return this;
    },

    _zoomIn: function (e) {
        if (!this._disabled && this._map._zoom < this._map.getMaxZoom()) {
            this._map.zoomIn(this._map.options.zoomDelta * (e.shiftKey ? 3 : 1));
        }
    },

    _zoomOut: function (e) {
        if (!this._disabled && this._map._zoom > this._map.getMinZoom()) {
            this._map.zoomOut(this._map.options.zoomDelta * (e.shiftKey ? 3 : 1));
        }
    },

    _createButton: function (html, title, className, container, fn) {
        var link = L.DomUtil.create('a', className, container);
        link.innerHTML = html;
        link.href = '#';
        link.title = title;

        /*
         * Will force screen readers like VoiceOver to read this as "Zoom in - button"
         */
        link.setAttribute('role', 'button');
        link.setAttribute('aria-label', title);

        L.DomEvent.disableClickPropagation(link);
        L.DomEvent.on(link, 'click', L.DomEvent.stop);
        L.DomEvent.on(link, 'click', fn, this);
        L.DomEvent.on(link, 'click', this._refocusOnMap, this);

        return link;
    },

    _updateDisabled: function () {
        var map = this._map,
            className = 'leaflet-disabled';

        L.DomUtil.removeClass(this._zoomInButton, className);
        L.DomUtil.removeClass(this._zoomOutButton, className);

        if (this._disabled || map._zoom === map.getMinZoom()) {
            L.DomUtil.addClass(this._zoomOutButton, className);
        }
        if (this._disabled || map._zoom === map.getMaxZoom()) {
            L.DomUtil.addClass(this._zoomInButton, className);
        }
    }
});

最佳答案

虽然传单中没有明确说明 class customization tutorial工厂之间有一个微妙的区别,工厂是小写的,不能扩展,而类是Pascal大小写的,可以在其上使用Leaflet extend机制:

var MyNewZoomControl = L.Control.Zoom.extend({
  onAdd: function (map) {
    // your new method content
  }
}

话虽这么说,如果您的新按钮并未真正与缩放按钮共享功能或未与它们“合并”,您可以简单地创建一个单独的控件并将其插入到相同的 Angular 落位置。还有 Leaflet EasyButton 插件可以在这方面提供帮助。

关于javascript - 使用自定义按钮扩展 L.control.zoom,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/57024817/

相关文章:

javascript - TypeError [ERR_INVALID_ARG_TYPE] : The "data" argument must be of type string or an instance of Buffer, TypedArray,或DataView

javascript - 在 props 中调用回调函数

c# - 如何在 C# 中使用带有继承的依赖注入(inject)

c++ - 从外部代码加载阶段

node.js - Angular-cli 'JavaScript heap out of memory' Assets 优化

javascript - 使用包含数组信息的传单添加折线

javascript - 检索 Canvas 上元素的 ID

javascript - 如何更改 javascript 字符串的一个单词的背景颜色?

c++ - C2597 对非静态成员的非法引用

javascript - 如何使用传单图表在 reactjs 中显示数组中的标记