Snap to finish with configurable tolerance

This commit is contained in:
Tim Schaub
2013-10-30 16:01:26 -06:00
parent ea6500ecd0
commit b5cc35ee04
2 changed files with 18 additions and 12 deletions

View File

@@ -344,6 +344,8 @@
/** /**
* @typedef {Object} ol.interaction.DrawOptions * @typedef {Object} ol.interaction.DrawOptions
* @property {ol.layer.Vector} layer Destination layer for the features. * @property {ol.layer.Vector} layer Destination layer for the features.
* @property {number|undefined} snapTolerance Pixel distance for snapping to the
* drawing finish (default is 12).
* @property {ol.interaction.DrawMode} mode Drawing mode ('point', 'linestring', * @property {ol.interaction.DrawMode} mode Drawing mode ('point', 'linestring',
* or 'polygon'). * or 'polygon').
* @todo stability experimental * @todo stability experimental

View File

@@ -46,7 +46,8 @@ ol.interaction.Draw = function(options) {
* @type {number} * @type {number}
* @private * @private
*/ */
this.snapTolerance_ = 15; this.snapTolerance_ = goog.isDef(options.snapTolerance) ?
options.snapTolerance : 12;
/** /**
* Draw mode. * Draw mode.
@@ -232,21 +233,24 @@ ol.interaction.Draw.prototype.modifyDrawing_ = function(event) {
last[1] = coordinate[1]; last[1] = coordinate[1];
geometry.setCoordinates(last); geometry.setCoordinates(last);
} else { } else {
this.sketchPoint_.getGeometry().setCoordinates(coordinate); var potentiallyDone = false;
if (this.mode_ === ol.interaction.DrawMode.LINESTRING) { if (this.mode_ === ol.interaction.DrawMode.LINESTRING) {
coordinates = geometry.getCoordinates(); coordinates = geometry.getCoordinates();
last = coordinates[coordinates.length - 1]; potentiallyDone = coordinates.length > 2;
last[0] = coordinate[0];
last[1] = coordinate[1];
geometry.setCoordinates(coordinates);
} else if (this.mode_ === ol.interaction.DrawMode.POLYGON) { } else if (this.mode_ === ol.interaction.DrawMode.POLYGON) {
var ring = geometry.getRings()[0]; geometry = geometry.getRings()[0];
coordinates = ring.getCoordinates(); coordinates = geometry.getCoordinates();
last = coordinates[coordinates.length - 1]; potentiallyDone = coordinates.length > 3;
last[0] = coordinate[0];
last[1] = coordinate[1];
ring.setCoordinates(coordinates);
} }
if (potentiallyDone && this.atFinish_(event)) {
// snap to finish
coordinate = this.finishCoordinate_.slice();
}
this.sketchPoint_.getGeometry().setCoordinates(coordinate);
last = coordinates[coordinates.length - 1];
last[0] = coordinate[0];
last[1] = coordinate[1];
geometry.setCoordinates(coordinates);
} }
}; };