Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Make it possible to programmatically finish drawing #2927

Merged
merged 2 commits into from
Nov 10, 2014
Merged

Make it possible to programmatically finish drawing #2927

merged 2 commits into from
Nov 10, 2014

Conversation

elemoine
Copy link
Member

@elemoine elemoine commented Nov 9, 2014

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:

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);
});

This makes it posssible to programmatically finish drawing.
@fredj
Copy link
Member

fredj commented Nov 10, 2014

LGTM

@elemoine
Copy link
Member Author

Thanks @fredj.

elemoine pushed a commit that referenced this pull request Nov 10, 2014
Make it possible to programmatically finish drawing
@elemoine elemoine merged commit b737677 into openlayers:master Nov 10, 2014
@elemoine elemoine deleted the finishdrawing branch November 10, 2014 07:55
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

Successfully merging this pull request may close these issues.

None yet

2 participants