单击第二个点后如何在 Leaflet.Draw 中完成折线?

sha*_*kla 1 leaflet leaflet.draw

当我尝试使用Leaflet.Draw 插件绘制多段线时遇到问题。

首先,我点击地图绘制第一个点,然后第二次点击完成线。

但是,在我第二次单击该行后,该行不会自行完成。它显示了该行的扩展。

当我双击它时,该行完成,否则我需要手动单击完成按钮。我想在第二次点击地图时完成那条线。

这是我绘制折线的代码:

var drawControl = new L.Control.Draw({
                    position: 'topleft',
                    draw: {                            
                        polygon: {
                            allowIntersection: true,
                            showArea: true,
                            drawError: {
                                color: '#b00b00',
                                timeout: 1000
                            },
                            shapeOptions: {
                                color: '#0033ff'
                            }
                        },
                        circle: {
                            shapeOptions: {
                                color: '#0033ff'
                            }
                        },
                        polyline: {
                            shapeOptions: {
                                color: 'red'
                            },
                        },
                        rectangle: {
                            shapeOptions: {
                                color: '#0033ff'
                            }
                        },
                        marker: false,
                        polyline: true,
                    },
                    edit: {
                        featureGroup: drawnItems,
                        remove: true
                    }
                });
Run Code Online (Sandbox Code Playgroud)

Mah*_*tib 5

您可以在 javascript 中使用位于Leaflet.draw/src/draw/handler/Draw.Polyline.js 中的类覆盖addVertex函数,并在其末尾添加以下代码:L.Draw.Polylineprototype

        markersLength = this._markers.length;
        if (markersLength == 2) {
            this._fireCreatedEvent();
            this.disable();
        }
Run Code Online (Sandbox Code Playgroud)

而且,这里是完整的代码:

    L.Draw.Polyline.prototype.addVertex = function (latlng) {
            var markersLength = this._markers.length;
            // markersLength must be greater than or equal to 2 before intersections can occur

            if (markersLength >= 2 && !this.options.allowIntersection && this._poly.newLatLngIntersects(latlng)) {
                this._showErrorTooltip();
                return;
            }
            else if (this._errorShown) {
                this._hideErrorTooltip();
            }

            this._markers.push(this._createMarker(latlng));

            this._poly.addLatLng(latlng);

            if (this._poly.getLatLngs().length === 2) {
                this._map.addLayer(this._poly);
            }

            this._vertexChanged(latlng, true);
            markersLength = this._markers.length;
            if (markersLength == 2) {
                this._fireCreatedEvent();
                this.disable();
            }
};
Run Code Online (Sandbox Code Playgroud)