1

当我尝试使用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
                    }
                });
4

3 回答 3

6

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

        markersLength = this._markers.length;
        if (markersLength == 2) {
            this._fireCreatedEvent();
            this.disable();
        }

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

    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();
            }
};
于 2017-07-19T13:01:00.560 回答
2

您可以自动触发第二次单击以完成形状。

map.on('draw:drawvertex', e => {

    const layerIds = Object.keys(e.layers._layers);

    if (layerIds.length > 1) {

        const secondVertex = e.layers._layers[layerIds[1]]._icon;

        requestAnimationFrame(() => secondVertex.click());

    }

});
于 2017-08-27T22:29:46.150 回答
2

在折线上添加多个顶点(例如,不会在第二次单击时自动完成折线)是Leaflet.Draw.

您也许可以修改此行为。我建议您查看 Leaflet.draw 文档,尤其是L.Draw.Polyline.completeShape() 方法

于 2017-02-08T17:32:49.697 回答