leaflet geoman无法捕捉问题的解决

花了2天时间将mapus里的bug定位到。

每当画线或面域的时候,无法捕捉,并且出现错误提示,但无法定位。

原来的代码如下:

  // Render a new object
  async function renderShape(snapshot, key) {
    // var user = await checkAuth();
    // if (await checkAuth() != false) {
    if (snapshot.type == "draw" || snapshot.type == "line" || snapshot.type == "area") {
      if (objects.filter(function(result) {
          return result.id === key && result.user === snapshot.user
        }).length == 0) {
        // If the shape doesn't exist locally, create it
        var line = L.polyline([
          [snapshot.initlat, snapshot.initlng]
        ], {
          color: snapshot.color,
          // pmIgnore: false
        });
        if (snapshot.completed && (snapshot.type == "line" || snapshot.type == "area")) {
          // If the shape is already finished, give it all its coordinates
          line = L.polyline(snapshot.path, {
            color: snapshot.color,
            // pmIgnore: false
          }); // 这里是否允许激活线段编辑
        }
        line.addTo(map); // 这里导致无法捕捉!!

修改后的代码如下

  // Render a new object
  async function renderShape(snapshot, key) {
    // var user = await checkAuth();
    // if (await checkAuth() != false) {
    if (snapshot.type == "draw" || snapshot.type == "line" || snapshot.type == "area") {
      if (objects.filter(function(result) {
          return result.id === key && result.user === snapshot.user
        }).length == 0) {
        // If the shape doesn't exist locally, create it
        if (snapshot.completed && (snapshot.type == "line" || snapshot.type == "area")) {
          // If the shape is already finished, give it all its coordinates
          line = L.polyline(snapshot.path, {
            color: snapshot.color,
            pmIgnore: false
          }); // 这里是否允许激活线段编辑
          line.addTo(map);// 放到这里就可以了。file=109可以捕捉!!其他项目不一定可以。
        } else {
          var line = L.polyline([
            [snapshot.initlat, snapshot.initlng]
          ], {
            color: snapshot.color,
            pmIgnore: false
          });
        }

原因分析:因为从数据库里读取图元,用上面这个rendershape方法添加到页面上,但是有些多段线不具备completed时,也被添加到页面上,但看不见,我估计是一些错误的数据——比如画了一半的多段线,按了取消键,这个时候数据存到数据库里了,但是completed=false。按照上述修改后,只将满足completed=true的多段线加到页面上。

L.PM.setOptIn(true);不要加

pmIgnore: false,不用加

  // Stop drawing lines / polygons
  map.on('pm:drawend', e => {
    lineon = false;
    followcursor.closeTooltip();
    // cursorTool();// 这里影响文本输入
  });
 map.addEventListener('mousemove', async (event) => {
    // var user = await checkAuth();
    // console.log(user)
    // if (await checkAuth() != false) {
    // Get cursor coordinates and save them locally
    let lat = Math.round(event.latlng.lat * 100000) / 100000;
    let lng = Math.round(event.latlng.lng * 100000) / 100000;
    cursorcoords = [lat, lng];

    // Make tooltip for line and area hints follow the cursor
    // followcursor.setLatLng([lat, lng]);//这里影响捕捉
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值