首页 > 解决方案 > 为什么我在活动中保存的信息会丢失

问题描述

早上好我正在尝试使用 Leaflet 绘制图像并保存数字,我创建了以下方法来启动地图:

initmap()
  {
    var url = 'http://kempe.net/images/newspaper-big.jpg';
    var bounds = [[-26.5,-25], [1021.5,1023]];
    var image = L.imageOverlay(url, bounds);
    var map = new L.Map('image-map', {layers: [image], crs: L.CRS.Simple, minZoom : -5});
    map.fitBounds(bounds);
    var drawnItems = new L.FeatureGroup();
    map.addLayer(drawnItems);

    var drawControl = new L.Control.Draw({
        position: 'topright',
        draw: {
            polyline: true,
            polygon: false,
            circle: false,
            marker: true
        },
        edit: {
            featureGroup: drawnItems,
            remove: true
        }
    });
    map.addControl(drawControl);

    map.on(L.Draw.Event.CREATED, function (e) {
      var type = e.layerType,
              layer = e.layer;
      if (type === 'marker') {
          layer.bindPopup('A popup!');
      }

      drawnItems.addLayer(layer);
      var type = e.layerType
      var shape = layer.toGeoJSON()
      var shape_to_json = JSON.stringify(shape);
      this.currentMap = shape_to_json;
    });


    map.on(L.Draw.Event.EDITED, function (e) {
      var layers = e.layers;
      var countOfEditedLayers = 0;
      layers.eachLayer(function (layer) {
          countOfEditedLayers++;
      });
    });
  }

正如您所看到的,当有人在绘图时,有一个事件定义,( map.on(L.Draw.Event.CREATED, function (e)) 关键是我记录了该方法,并使用 console.log 检查保存的内容

this.currentMap = shape_to_json;

如你看到的:

shape_to_json
installations-management.component.ts:120 {"type":"Feature","properties":{},"geometry":{"type":"Polygon","coordinates":[[[421.202413,315.527832],[421.202413,756.416626],[988.101415,756.416626],[988.101415,315.527832],[421.202413,315.527832]]]}}

一切似乎都很好。但是当我点击保存按钮时,this.currentMap的数据丢失了,并且没有其他任何地方被修改。那么,我是保存错误还是因为事件而在某处修改?谢谢你们 !

标签: javascriptangularleaflet

解决方案


推荐阅读