typescript 使用Leaflet circle和leaflet-draw库防止可拖动的圆

koaltpgm  于 2023-05-01  发布在  TypeScript
关注(0)|答案(1)|浏览(170)

我有一个圆,当你点击一个特定的点(圆的质心将是该点所在的位置)时,我会添加到Map中,你可以编辑圆,这意味着你可以使用圆末端的夹爪调整它的大小:

调整大小工作正常,但我想做的是防止圆圈被拖动:

如何禁用拖动圆圈但仍保持调整大小?我正在使用leaflet-draw library和@types/leaflet-draw包。下面是我如何将圆添加到Map中:

onMapClick(leafletMouseEvent: LeafletMouseEvent) {
    const drawControl = new L.Control.Draw({
      draw: {
        circle: true, // Enable circle drawing
      },
      edit: {
        featureGroup: new L.FeatureGroup()
      }
    });
    
    map.addControl(drawControl);
    
    // Initialize the feature group for drawn items
    drawnItems = new L.FeatureGroup();
    
    map.addLayer(drawnItems);
    
    map.on(L.Draw.Event.CREATED, (e: any) => {
      let layer = e.layer;
      drawnItems.addLayer(layer); // Add shape to current drawing
    });
    
    map.on(L.Draw.Event.EDITED, (e: any) => {
      let layers = e.layers;
      layers.eachLayer((layer: any) => {
        drawnItems.addLayer(layer);
      });
    });
    
    const circle: any = new L.Circle(latLng, {
      radius: 1609.34, // 1 mile in meters
    });
    
    drawnItems.addLayer(circle);
    
    circle.editing.enable()
 }
j8ag8udp

j8ag8udp1#

我只找到了a hack来防止圆移动,也许你可以在上面扩展:

'use strict';

var startPosition = [33.812511, -117.918976];
var map = L.map('mapId').setView(startPosition, 12);
var drawnItems = L.featureGroup().addTo(map);
var osm = L.tileLayer('https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  attribution: '&copy; <a href="https://www.openstreetmap.org/">OpenStreetMap</a> contributors'
}).addTo(map);

map.addControl(new L.Control.Draw({
  edit: {
    featureGroup: drawnItems,
    circle: {
      allowIntersection: false
    }
  }
}));

var circle = L.circle(startPosition, 1609.34).addTo(drawnItems);
circle.editing.enable();

map.on(L.Draw.Event.EDITMOVE, function(ev) {
  circle.setLatLng(ev.target._lastCenter);
});
html,
body {
  padding: 0;
  margin: 0;
  height: 100%;
}

body {
  display: flex;
  flex-direction: column;
}

#mapId {
  flex-grow: 1;
}
<link type="text/css" rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet.min.css">
<script src="https://cdn.jsdelivr.net/npm/leaflet@1/dist/leaflet-src.min.js"></script>
<script src="https://cdn.jsdelivr.net/npm/leaflet-draw@1/dist/leaflet.draw.min.js"></script>
<link rel="stylesheet" href="https://cdn.jsdelivr.net/npm/leaflet-draw@1/dist/leaflet.draw.min.css">

<div id="mapId"></div>

相关问题