openlayers/ol3 — pull request #2927 by elemoine (2014-11-09 20:11:35+00:00)
Title: Make it possible to programmatically finish drawing

This PR changes `ol.interaction.Draw#finishDrawing_` to a public function and adds that function to the API (with the `@api` annotation).

With this change one can listen to `change` event on the feature being drawn and finish the drawing when some (user-specific) condition is met.

For example, to automatically finish the drawing of a linestring when the number of vertices is 2:

```js
var listenerKey;
drawInteraction.on('drawstart', function(e) {
var feature = e.feature;
var lineString = feature.getGeometry();
// finish the drawing when the linestring has 2 vertices
listenerKey = lineString.on('change', function(e) {
var lineString = e.target;
var vertices = lineString.getCoordinates();
if (vertices.length == 3) {
drawInteraction.finishDrawing();
}
});
});
drawInteraction.on('drawend', function(e) {
ol.Observable.unByKey(listenerKey);
});
```