简体   繁体   中英

How to send only the modified geometry in OpenLayers 5?

In my map there is multiple parcel which is display, the user cand modify the geometry of the parcel and send it to my API to update the geometry in my database. If I select a parcel I want to modify the geometry of, the application will also modify the geometry of all the other parcels that I could have clicked on beforehand. It's like it's keeping in memory everything I selected prior to the parcel that I'm really interested into.

the code when i recover the geometry after the modification and before send it to my API :

map.on("click", function (evt) {
  map.forEachFeatureAtPixel(evt.pixel, function (feature, layer) {
    modify.on('modifyend',function(event) {
          var geom = feature.getGeometry().getCoordinates();
          var wkt = new ol.format.WKT().writeGeometry(new ol.geom.MultiPolygon(geom));
    })
  })
})

This is what a parcel looks like :

在此处输入图片说明

My question is : How can I modify the geometry of the last parcel I selected (which is the one I'm interested in) without modifying every other one?

You only need to set up a single modifyend listener, the feature which has been modified can be obtained from the event, also there's no need to create a new geometry from the coordinates of the old one, it will be the same (unless you want to convert a polygon to multipolygon, but that would need extra [ ] )

modify.on('modifyend',function(event) {
      var wkt = new ol.format.WKT().writeGeometry(event.features.getArray()[0].getGeometry());
})

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM