如何以编程方式放置自定义leaflet.draw形状?

问题描述

我想从数据库中恢复自定义的L.Draw.Marker(L.Draw.Waypoint),但是当我尝试对其进行初始化时,结果不是适当的图层,而仅仅是处理程序。在不使用鼠标单击“按钮”以激活处理程序,然后在地图上的某个地方放置Waypoint的情况下,该怎么办?

我可以使用L.geoJson()做一些接近的事情,但这只是没有我的Waypoint对象功能的普通标记...还是有办法在GeoJSON图层之外创建Waypoint?>

类似这样的东西:

function load_shape() {
    let test_wp = {
        "type": "Feature","properties": {
            "layer_type": "waypoint","map_id": 0,"rotation": -44
        },"geometry": {
            "type": "Point","coordinates": [27.712348,5.000000]
        }
    };
    
    let geojson = L.geoJson(test_wp); <- works
    let wp = waypoint(map,geojson._layers[geojson._leaflet_id-1]); <- works too,but this is 
                                                                       just the handler

    drawnItems.addLayer(wp.layer); <- doesn't work of course,just to get the idea...
    - or -
    wp.layer.addTo(map)
}

解决方法

我找到了解决方法:

L.geoJson(test_wp,{
            // style: function (feature) {
            //     return {color: feature.properties.color};
            // },pointToLayer: function (feature,latlng) {
                return L.marker(latlng,{
                    draggable: true,icon: L.icon({
                        iconUrl: 'static/images/icons/arrow-alt-circle-up-regular.svg',iconSize: [18,18],iconAnchor: [9,9],}),rotationOrigin: 'center center',})
            },onEachFeature: function (feature,layer) {

                layer.on('click',function (e) {
                    //open a popup or whatever
                });

                layer.on('drag',function (e) {
                    //when dragging do...
                });

                layer.bindPopup(feature.properties.description).addTo(drawnItems);
            }
        });

这不会创建我的自定义标记,但是我可以为其提供所需的全部功能。