简体   繁体   中英

Openlayer 4 get polygon coordinates after select feature

I want get coordinates after select feature using interaction.Select.

Fragment of my code:

  var selectf = new ol.interaction.Select({
    layers: [vectorLayer],
     }); 
   map.addInteraction(selectf); 


  selectf.on('select', function(evt) {

//here I want read coordinates of selected feature
}); 

How I can do it?

On 'Select' event, you can use the following approach to get the co-ordinate from the vector feature.

select.on('select', function(event) {
console.log(event.selected[0].getGeometry().getCoordinates());

  });

@BruceWayne's answer is correct for all standard geometries obtained from sources such as geojson (and the question did specify polygon) but if you are using the non-standard circle geometry specific to OpenLayers a special case will be needed:

select.on('select', function(event) {
  var geom = event.selected[0].getGeometry();
  if (geom.getType() == 'Circle') {
    console.log(geom.getCenter());
    console.log(geom.getRadius());
  } else {
    console.log(geom.getCoordinates());
  }
});

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