Convenience function for drawing box-shaped polygons

This commit is contained in:
Tim Schaub
2016-10-16 15:49:13 -06:00
parent 92583542eb
commit 9b12cac5c6
2 changed files with 60 additions and 0 deletions
+31
View File
@@ -2,6 +2,7 @@ goog.provide('ol.interaction.Draw');
goog.require('ol');
goog.require('ol.events');
goog.require('ol.extent');
goog.require('ol.events.Event');
goog.require('ol.Feature');
goog.require('ol.MapBrowserEvent.EventType');
@@ -766,6 +767,36 @@ ol.interaction.Draw.createRegularPolygon = function(opt_sides, opt_angle) {
};
/**
* Create a `geometryFunction` that will create a box-shaped polygon (aligned
* with the coordinate system axes). Use this with the draw interaction and
* `type: 'Circle'` to return a box instead of a circle geometry.
* @return {ol.DrawGeometryFunctionType} Function that draws a box-shaped polygon.
* @api
*/
ol.interaction.Draw.createBox = function() {
return (
/**
* @param {ol.Coordinate|Array.<ol.Coordinate>|Array.<Array.<ol.Coordinate>>} coordinates
* @param {ol.geom.SimpleGeometry=} opt_geometry
* @return {ol.geom.SimpleGeometry}
*/
function(coordinates, opt_geometry) {
var extent = ol.extent.boundingExtent(coordinates);
var geometry = opt_geometry || new ol.geom.Polygon(null);
geometry.setCoordinates([[
ol.extent.getBottomLeft(extent),
ol.extent.getBottomRight(extent),
ol.extent.getTopRight(extent),
ol.extent.getTopLeft(extent),
ol.extent.getBottomLeft(extent)
]]);
return geometry;
}
);
};
/**
* Get the drawing mode. The mode for mult-part geometries is the same as for
* their single-part cousins.