问题
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