Open Layers 3 - get vector type (line, polygon, point) from dropped in vector data

房东的猫 提交于 2019-12-13 02:22:34

问题


OpenLayers 3 has a great drag and drop feature. The example is given here:

http://openlayers.org/en/master/examples/drag-and-drop.html

Within the event handler for the drag and drop you can access the vector layer that has been dragged in (vectorSource) in this case:

dragAndDropInteraction.on('addfeatures', function(event) {
    var vectorSource = new ol.source.Vector({
      features: event.features
    });
    map.addLayer(new ol.layer.Vector({
      source: vectorSource,
      style: styleFunction
    }));
    map.getView().fit(
        vectorSource.getExtent(), /** @type {ol.Size} */ (map.getSize()));
  });

vectorSource can be seen here being created from event.features, however I can't find a way of telling whether the vector that's been dropped into the map is of type polygon, point or line.

My question is, is there a way of telling what vector type the data is?

console.log(event.features); shows that there is a geometry term in there, but I'm not sure how that reliably gives me data type.

I need to know for my layer management tool so that I can correctly represent the vector layer.

I have tried:

event.features[0].getGeometry();
event.features[0].getGeometryName();

Neither seem to produce the information I need.


回答1:


If you have a feature, you can retrieve its Geometry and then its Type:

feature.getGeometry().getType()

The geometry type. One of 'Point', 'LineString', 'LinearRing', 'Polygon', 'MultiPoint', 'MultiLineString', 'MultiPolygon', 'GeometryCollection', 'Circle'.

Reference: http://openlayers.org/en/v3.0.0/apidoc/ol.geom.html#GeometryType



来源:https://stackoverflow.com/questions/36933301/open-layers-3-get-vector-type-line-polygon-point-from-dropped-in-vector-da

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!