Javascript 按ctrl&;键绘制一个圆;在传单中拖动鼠标

Javascript 按ctrl&;键绘制一个圆;在传单中拖动鼠标,javascript,leaflet,Javascript,Leaflet,我正在尝试开发一个带有传单的函数,它使用户能够通过按住ctrl键并拖动鼠标来绘制一个圆,如下所示 let mouseDownPos = null let mouseUpPos = null L.Map.CircleSelector = L.Map.Drag.extend({ _onMouseDown: function(e) { if (!e.ctrlKey) return

我正在尝试开发一个带有传单的函数,它使用户能够通过按住ctrl键并拖动鼠标来绘制一个圆,如下所示

    let mouseDownPos = null
    let mouseUpPos = null

    L.Map.CircleSelector = L.Map.Drag.extend({
        _onMouseDown: function(e) {
            if (!e.ctrlKey)
                return

            let map = this._map
            map.dragging.disable()

            mouseDownPos = map.containerPointToLatLng(this._point)
        }, 
        _onMouseUp: function(e) {
            if (!e.ctrlKey) {
                this._map.dragging.enable()
                return
            }

            let map = this._map
            mouseUpPos = map.containerPointToLatLng(this._point)

            let radius = map.distance(mouseDownPos, mouseUpPos)
            L.circle(mouseDownPos, {radius: radius}).addTo(map)

            map.dragging.enable()
        }
    })

    L.Map.mergeOptions({circleSelector: true})
    L.Map.addInitHook('addHandler', 'circleSelector', L.Map.CircleSelector)
    L.Map.CircleSelector = L.Draw.SimpleShape.extend({
        _onMouseUp: function (e) {
            // TODO
            // 1. Get the circle center & radius
            // 2. Calculate distances between center & markers
            // 3. If the distance in step 2 <= radius, it is in the circle
            // 4. Anything you'd like to do......
        }
    })
当我在地图上按住ctrl键并拖动鼠标时,它仍然不起作用

我在_onMouseDown()中尝试在开始时将文本打印到控制台,但没有显示任何内容

这件事似乎没有触发

我需要修改什么?谢谢。

最后,我向我的目标迈进

参考L.Draw.Circle的源代码,我从L.Draw.Circle扩展了选择器。主要修改的部分在_onMouseUp中,如下所示

    let mouseDownPos = null
    let mouseUpPos = null

    L.Map.CircleSelector = L.Map.Drag.extend({
        _onMouseDown: function(e) {
            if (!e.ctrlKey)
                return

            let map = this._map
            map.dragging.disable()

            mouseDownPos = map.containerPointToLatLng(this._point)
        }, 
        _onMouseUp: function(e) {
            if (!e.ctrlKey) {
                this._map.dragging.enable()
                return
            }

            let map = this._map
            mouseUpPos = map.containerPointToLatLng(this._point)

            let radius = map.distance(mouseDownPos, mouseUpPos)
            L.circle(mouseDownPos, {radius: radius}).addTo(map)

            map.dragging.enable()
        }
    })

    L.Map.mergeOptions({circleSelector: true})
    L.Map.addInitHook('addHandler', 'circleSelector', L.Map.CircleSelector)
    L.Map.CircleSelector = L.Draw.SimpleShape.extend({
        _onMouseUp: function (e) {
            // TODO
            // 1. Get the circle center & radius
            // 2. Calculate distances between center & markers
            // 3. If the distance in step 2 <= radius, it is in the circle
            // 4. Anything you'd like to do......
        }
    })
L.Map.CircleSelector=L.Draw.SimpleShape.extend({
_onMouseUp:函数(e){
//待办事项
//1.获取圆心和半径
//2.计算中心和标记之间的距离

//3.如果你看过第二步中的距离吗?该插件有一个圆圈绘制选项。也许你可以使用该选项,或者看看他们的源代码。@用户:我知道该插件。我想自己开发这个函数作为练习,但遇到了这个问题。也许我可以先跟踪源代码,谢谢。