gpt4 book ai didi

leaflet - 如何使用鼠标和leaflet.js绘制折线

转载 作者:行者123 更新时间:2023-12-04 00:35:53 33 4
gpt4 key购买 nike

我想在带有传单的 map 上绘制多段线。我想应用的基本手势是:

  • 用户单击并按住鼠标按钮 -> 定义第一个标记。如果用户按住鼠标按钮并移动鼠标,则会显示相应的“橡皮筋”。
  • 用户释放鼠标按钮 -> 将第二个标记添加到 map ,并且这两个标记由一条线链接。
  • 从第二个标记开始,用户可以使用与上述相同的过程继续构建第二条线。

  • 最终结果应该是由折线链接的一组坐标/标记。

    最佳答案

    朱利安 V 伊万桑切斯 说,你可以实现一些类似draw的plugins

    在下面的例子中:

    你可以看到 Leaflet.draw 的用法插入。希望能帮助到你 :)

    // center of the map
    var center = [46.165164, 15.750443];

    // Create the map
    var map = L.map('map').setView(center,15);

    // Set up the OSM layer
    L.tileLayer(
    'https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
    attribution: 'Data © <a href="http://osm.org/copyright">OpenStreetMap</a>',
    maxZoom: 18
    }).addTo(map);



    // Initialise the FeatureGroup to store editable layers
    var editableLayers = new L.FeatureGroup();
    map.addLayer(editableLayers);

    var options = {
    position: 'topleft',
    draw: {
    polygon: {
    allowIntersection: false, // Restricts shapes to simple polygons
    drawError: {
    color: '#e1e100', // Color the shape will turn when intersects
    message: '<strong>Oh snap!<strong> you can\'t draw that!' // Message that will show when intersect
    },
    shapeOptions: {
    color: '#97009c'
    }
    },
    polyline: {
    shapeOptions: {
    color: '#f357a1',
    weight: 10
    }
    },
    // disable toolbar item by setting it to false
    polyline: true,
    circle: true, // Turns off this drawing tool
    polygon: true,
    marker: true,
    rectangle: true,
    },
    edit: {
    featureGroup: editableLayers, //REQUIRED!!
    remove: true
    }
    };

    // Initialise the draw control and pass it the FeatureGroup of editable layers
    var drawControl = new L.Control.Draw(options);
    map.addControl(drawControl);

    var editableLayers = new L.FeatureGroup();
    map.addLayer(editableLayers);

    map.on('draw:created', function(e) {
    var type = e.layerType,
    layer = e.layer;

    if (type === 'polyline') {
    layer.bindPopup('A polyline!');
    } else if ( type === 'polygon') {
    layer.bindPopup('A polygon!');
    } else if (type === 'marker')
    {layer.bindPopup('marker!');}
    else if (type === 'circle')
    {layer.bindPopup('A circle!');}
    else if (type === 'rectangle')
    {layer.bindPopup('A rectangle!');}


    editableLayers.addLayer(layer);
    });
    html, body, #map { margin: 0; height: 100%; width: 100%; }
    <!DOCTYPE html>
    <html>
    <head>
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-beta.2.rc.2/leaflet.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.0.0-beta.2.rc.2/leaflet.css" rel="stylesheet" />
    <script src="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.2.3/leaflet.draw.js"></script>
    <link href="https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/0.2.3/leaflet.draw.css" rel="stylesheet" />
    <meta charset="utf-8">
    <title>TEST</title>

    </head>
    <body>
    <div id='map'></div>
    </body>
    </html>

    关于leaflet - 如何使用鼠标和leaflet.js绘制折线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42939633/

    33 4 0
    Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
    广告合作:1813099741@qq.com 6ren.com