Sam*_*sla 37 javascript gis leaflet leaflet.draw
对于任何有传单或leaflet.draw插件的人:
我想在不使用工具栏的情况下开始绘制多边形leaflet.draw.我已经设法找到允许编辑的属性,而不使用工具栏(layer.editing.enable();)通过在线搜索(它不在主文档中).我似乎无法找到如何在没有工具栏按钮的情况下开始绘制多边形.任何帮助将非常感激!
谢谢 :)
BaC*_*aCH 62
这个简单的代码适合我:
new L.Draw.Polyline(map, drawControl.options.polyline).enable();
Run Code Online (Sandbox Code Playgroud)
只需将其放入自定义按钮的onclick处理程序(或任何您想要的位置).
变量map并且drawControl是对传单映射和绘制控件的引用.
潜入源代码(leaflet.draw-src.js),您可以找到绘制其他元素以及编辑或删除它们的函数.
new L.Draw.Polygon(map, drawControl.options.polygon).enable()
new L.Draw.Rectangle(map, drawControl.options.rectangle).enable()
new L.Draw.Circle(map, drawControl.options.circle).enable()
new L.Draw.Marker(map, drawControl.options.marker).enable()
new L.EditToolbar.Edit(map, {
featureGroup: drawControl.options.featureGroup,
selectedPathOptions: drawControl.options.edit.selectedPathOptions
})
new L.EditToolbar.Delete(map, {
featureGroup: drawControl.options.featureGroup
})
Run Code Online (Sandbox Code Playgroud)
我希望这对你也有用.
编辑:L.EditToolbar.Edit和L.EditToolbar.Delete类公开以下有用的方法:
虽然BaCH的解决方案可能是最好的,但我想添加一个单行解决方案,它实际上更面向未来(比深入研究未记录的 Leaflet Draw 方法)并且是最简单的.
document.querySelector('.leaflet-draw-draw-polygon').click();
Run Code Online (Sandbox Code Playgroud)
就是这样。您只是利用了工具栏的存在,但实际上您并没有使用它。您可以以任何方式以编程方式启动绘图。您还可以使用 CSS 隐藏工具栏。
如果您想开始绘制不同的形状,请使用以下类之一:
.leaflet-draw-draw-polyline
.leaflet-draw-draw-rectangle
.leaflet-draw-draw-circle
.leaflet-draw-draw-marker
.leaflet-draw-draw-circlemarker
Run Code Online (Sandbox Code Playgroud)
我认为值得一提的是Jacob Toyes对这个问题的回答。您总是在Leaflet.draw 中使用处理程序进行绘图- 而不是直接使用图层。如果要编辑图层,请使用保存在图层editing字段中的处理程序,如下所示:layer.editing.enable();。如果你想创建一个新层,你首先创建一个新的处理程序:
// Define you draw handler somewhere where you click handler can access it. N.B. pass any draw options into the handler
var polygonDrawer = new L.Draw.Polyline(map);
// Assumming you have a Leaflet map accessible
map.on('draw:created', function (e) {
var type = e.layerType,
layer = e.layer;
// Do whatever you want with the layer.
// e.type will be the type of layer that has been draw (polyline, marker, polygon, rectangle, circle)
// E.g. add it to the map
layer.addTo(map);
});
// Click handler for you button to start drawing polygons
$('#draw_poly').click(function() {
polygonDrawer.enable();
});
Run Code Online (Sandbox Code Playgroud)
现在实际上在leaflet.draw github页面上有一个示例:https : //github.com/Leaflet/Leaflet.draw/blob/develop/docs/examples/edithandlers.html
尽管如此,我认为处理程序在那里还没有得到很好的记录。
如上所述,L.EditToolbar.Edit并L.EditToolbar.Delete公开有趣的方法和事件,如editstart和editstop。没有提到的是,这两个类本身是从L.Handler.
所以我已经为圆想出了这个,但对于多边形应该是一样的。其实很简单。希望下面的代码能回答您的问题,但如果没有让我知道,我可以发布更多内容到要点或其他内容。
// Creates the circle on the map for the given latLng and Radius
// If the createdWithAddress flag is true, the circle will not update
// it's address according to its position.
createCircle: function(latLng, radius, createdWithAddress) {
if (!this.circle) {
var self = this,
centerIcon,
centerMarker;
centerIcon = new L.Icon({
iconUrl: '/assets/location_pin_24px.png',
iconSize: [24, 24],
iconAnchor: [12, 24],
shadowUrl: '/assets/marker-shadow.png',
shadowSize: [20, 20],
shadowAnchor:[6, 20]
})
// Setup the options for the circle -> Override icons, immediately editable
options = {
stroke: true,
color: '#333333',
opacity: 1.0,
weight: 4,
fillColor: '#FFFFFF',
moveIcon: centerIcon,
resizeIcon: new L.Icon({
iconUrl: '/assets/radius_handle_18px.png',
iconSize: [12, 12],
iconAnchor: [0,0]
})
}
if (someConfigVarYouDontNeedToKnow) {
options.editable = false
centerMarker = new L.Marker(latLng, { icon:centerIcon })
} else {
options.editable = true
}
// Create our location circle
// NOTE: I believe I had to modify Leaflet or Leaflet.draw to allow for passing in
// options, but you can make it editable with circle.editing.enable()
this.circle = L.circle([latLng.lat, latLng.lng], radius, options)
// Add event handlers to update the location
this.circle.on('add', function() {
if (!createdWithAddress) {
self.reverseGeocode(this.getLatLng())
}
self.updateCircleLocation(this.getLatLng(), this.getRadius())
self.updateMapView()
})
this.circle.on('edit', function() {
if (self.convertLatLngToString(this.getLatLng()) !== self.getLocationLatLng()) {
self.reverseGeocode(this.getLatLng())
}
self.updateCircleLocation(this.getLatLng(), this.getRadius())
self.updateMapView()
})
this.map.addLayer(this.circle)
if (centerMarker) {
centerMarker.addTo(this.map)
this.circle.redraw()
centerMarker.update()
}
}
},
Run Code Online (Sandbox Code Playgroud)
抱歉,其中很多只是噪音,但它应该让您了解如何解决这个问题。您可以像使用editing.enable()/.disable() 所说的那样控制编辑。
确保对任何问题发表评论。祝你好运。
| 归档时间: |
|
| 查看次数: |
21373 次 |
| 最近记录: |