Finish lines by clicking last point

This commit is contained in:
Tim Schaub
2013-10-30 15:41:20 -06:00
parent d821f227ba
commit ea6500ecd0

View File

@@ -56,11 +56,12 @@ ol.interaction.Draw = function(options) {
this.mode_ = options.mode; this.mode_ = options.mode;
/** /**
* Start coordinate for the feature. * Finish coordinate for the feature (first point for polygons, last point for
* linestrings).
* @type {ol.Coordinate} * @type {ol.Coordinate}
* @private * @private
*/ */
this.startCoordinate_ = null; this.finishCoordinate_ = null;
/** /**
* Sketch feature. * Sketch feature.
@@ -102,7 +103,7 @@ ol.interaction.Draw.prototype.setMap = function(map) {
} else { } else {
// removing from a map, clean up // removing from a map, clean up
this.sketchLayer_ = null; this.sketchLayer_ = null;
this.startCoordinate_ = null; this.finishCoordinate_ = null;
this.sketchFeature_ = null; this.sketchFeature_ = null;
} }
@@ -135,10 +136,10 @@ ol.interaction.Draw.prototype.handleMapBrowserEvent = function(event) {
* @private * @private
*/ */
ol.interaction.Draw.prototype.handleClick_ = function(event) { ol.interaction.Draw.prototype.handleClick_ = function(event) {
if (goog.isNull(this.startCoordinate_)) { if (goog.isNull(this.finishCoordinate_)) {
this.startDrawing_(event); this.startDrawing_(event);
} else if (this.mode_ === ol.interaction.DrawMode.POINT || } else if (this.mode_ === ol.interaction.DrawMode.POINT ||
this.atStart_(event)) { this.atFinish_(event)) {
this.finishDrawing_(event); this.finishDrawing_(event);
} else { } else {
this.addToDrawing_(event); this.addToDrawing_(event);
@@ -155,9 +156,9 @@ ol.interaction.Draw.prototype.handleClick_ = function(event) {
*/ */
ol.interaction.Draw.prototype.handleMove_ = function(event) { ol.interaction.Draw.prototype.handleMove_ = function(event) {
if (this.mode_ === ol.interaction.DrawMode.POINT && if (this.mode_ === ol.interaction.DrawMode.POINT &&
goog.isNull(this.startCoordinate_)) { goog.isNull(this.finishCoordinate_)) {
this.startDrawing_(event); this.startDrawing_(event);
} else if (!goog.isNull(this.startCoordinate_)) { } else if (!goog.isNull(this.finishCoordinate_)) {
this.modifyDrawing_(event); this.modifyDrawing_(event);
} }
return true; return true;
@@ -170,12 +171,12 @@ ol.interaction.Draw.prototype.handleMove_ = function(event) {
* @return {boolean} The event is within the snapping tolerance of the start. * @return {boolean} The event is within the snapping tolerance of the start.
* @private * @private
*/ */
ol.interaction.Draw.prototype.atStart_ = function(event) { ol.interaction.Draw.prototype.atFinish_ = function(event) {
var map = event.map; var map = event.map;
var startPixel = map.getPixelFromCoordinate(this.startCoordinate_); var finishPixel = map.getPixelFromCoordinate(this.finishCoordinate_);
var pixel = event.getPixel(); var pixel = event.getPixel();
var dx = pixel[0] - startPixel[0]; var dx = pixel[0] - finishPixel[0];
var dy = pixel[1] - startPixel[1]; var dy = pixel[1] - finishPixel[1];
return Math.sqrt(dx * dx + dy * dy) <= this.snapTolerance_; return Math.sqrt(dx * dx + dy * dy) <= this.snapTolerance_;
}; };
@@ -187,7 +188,7 @@ ol.interaction.Draw.prototype.atStart_ = function(event) {
*/ */
ol.interaction.Draw.prototype.startDrawing_ = function(event) { ol.interaction.Draw.prototype.startDrawing_ = function(event) {
var start = event.getCoordinate(); var start = event.getCoordinate();
this.startCoordinate_ = start; this.finishCoordinate_ = start;
var sketchFeature = new ol.Feature(); var sketchFeature = new ol.Feature();
sketchFeature.setRenderIntent(ol.layer.VectorLayerRenderIntent.SELECTED); sketchFeature.setRenderIntent(ol.layer.VectorLayerRenderIntent.SELECTED);
var features = [sketchFeature]; var features = [sketchFeature];
@@ -260,6 +261,7 @@ ol.interaction.Draw.prototype.addToDrawing_ = function(event) {
var geometry = this.sketchFeature_.getGeometry(); var geometry = this.sketchFeature_.getGeometry();
var coordinates, last; var coordinates, last;
if (this.mode_ === ol.interaction.DrawMode.LINESTRING) { if (this.mode_ === ol.interaction.DrawMode.LINESTRING) {
this.finishCoordinate_ = coordinate.slice();
coordinates = geometry.getCoordinates(); coordinates = geometry.getCoordinates();
coordinates.push(coordinate.slice()); coordinates.push(coordinate.slice());
geometry.setCoordinates(coordinates); geometry.setCoordinates(coordinates);
@@ -278,7 +280,7 @@ ol.interaction.Draw.prototype.addToDrawing_ = function(event) {
* @private * @private
*/ */
ol.interaction.Draw.prototype.finishDrawing_ = function(event) { ol.interaction.Draw.prototype.finishDrawing_ = function(event) {
this.startCoordinate_ = null; this.finishCoordinate_ = null;
var sketchFeature = this.sketchFeature_; var sketchFeature = this.sketchFeature_;
var features = [sketchFeature]; var features = [sketchFeature];
if (this.mode_ !== ol.interaction.DrawMode.POINT) { if (this.mode_ !== ol.interaction.DrawMode.POINT) {