1

我已经阅读了@ghybs 页面给出的有用回答:“<a href="https://stackoverflow.com/questions/34738805/update-properties-of-geojson-to-use-it-with-leaflet">更新属性geojson 与传单一起使用”</p>

但我坚持使用引导弹出窗口使其正常工作,也从用户那里收集数据并将其保存在 feature.properties 上,稍后我将从多个标记收集多个数据,多边形折线转换为 geojson。我可以从弹出窗口中收集数据,但我创建的每个标记的数据都显示相同。每个标记的 feature.properties 应该不同。

请查看我的代码:

var osmUrl = 'http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png',
    osmAttrib = '&copy; <a href="http://openstreetmap.org/copyright">OpenStreetMap</a> contributors',
    osm = L.tileLayer(osmUrl, {
        maxZoom: 18,
        attribution: osmAttrib
    });

map = L.map('map', {
    layers: [osm],
    center: [31.9500, 35.9333],
    zoom: 15
});
var editableLayers = L.geoJson().addTo(map);
map.addControl(new L.Control.Draw({
    edit: {
        featureGroup: editableLayers
    }
}));

map.on('draw:created', function (e) {
      var layer = e.layer,
        feature = layer.feature = layer.feature || {};
    feature.type = feature.type || "Feature";
    var props = feature.properties = feature.properties || {};
    //layer.feature = {properties: {}}; // No need to convert to GeoJSON.
    //var props = layer.feature.properties;
    props.action = null;

    editableLayers.addLayer(layer);
    addPopup(layer);
});

function addPopup(layer) {
    var content = document.getElementById('action');
    layer.feature.properties.action = content;

/* content.addEventListener("keyup", function () {
        layer.feature.properties.action = content;
    });*/

/*    layer.on("popupopen", function () {
        content.value = layer.feature.properties.desc;
      content.focus();
    });
    layer.bindPopup(content).openPopup();*/

    layer.on('click', function() {
        $('#action').val(layer.feature.properties.action);

            //content.value = layer.feature.properties.action;
            $('#attributes').modal({'show' : true, backdrop:'static', keyboard:false});
           $('#action').val(layer.feature.properties.action);
        });

}

document.getElementById("convert").addEventListener("click", function () {
    console.log(JSON.stringify(editableLayers.toGeoJSON(), null, 2));
});
#map {
    height: 500px;
}
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>
<script src="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/js/bootstrap.min.js"></script>
<link href="https://maxcdn.bootstrapcdn.com/bootstrap/3.3.7/css/bootstrap.min.css" rel="stylesheet"/>
  
<script type="text/javascript" src="https://unpkg.com/leaflet@0.7.7/dist/leaflet-src.js"></script>
<link rel="stylesheet" href="https://unpkg.com/leaflet@0.7.7/dist/leaflet.css" type="text/css">
<link rel="stylesheet" href="https://cdn.rawgit.com/Leaflet/Leaflet.draw/v0.3.0/dist/leaflet.draw.css" type="text/css">
<!--js-->
<script type="text/javascript" src="https://cdn.rawgit.com/Leaflet/Leaflet.draw/v0.3.0/dist/leaflet.draw-src.js"></script>
<body>
<div id="map"></div>
<button id="convert">
Get all features to GeoJSON string
</button>

<div class="modal" id="attributes">
  <div class="modal-dialog">
    <div class="modal-content">
      <div class="modal-header">
        <h4 class="modal-title">Attribute Data</h4>
      </div>
      <div class="modal-body">
       

<div  class="content-scroll5">

<div class="col-xs-2">
  <label for="ex1">ACTION</label>
  <input class="form-control" name="action" id="actin" type="text">
</div>
      </div>
      <div class="modal-footer">
        <button type="button" class="btn btn-default" data-dismiss="modal">OK</button>
      </div>
    </div>
  </div>
</div>

4

0 回答 0