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

Line arrows example #3343

Merged
merged 3 commits into from
Mar 16, 2015
Merged

Line arrows example #3343

merged 3 commits into from
Mar 16, 2015

Conversation

fredj
Copy link
Member

@fredj fredj commented Mar 12, 2015

Add an example than shows how to draw arrows to a linestring (end of each segment)

@fredj fredj changed the title [wip] Line arrows example Line arrows example Mar 16, 2015
@fredj
Copy link
Member Author

fredj commented Mar 16, 2015

Thanks for any review

* @template T
* @api
*/
ol.geom.LineString.prototype.forEachSegments = function(callback) {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

forEachSegments -> forEachSegment

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

yes, will change

@fredj
Copy link
Member Author

fredj commented Mar 16, 2015

Note: an icon is used for the arrow but it should be possible to use a ol.style.RegularShape when an anchor options will be available (see #3342)

@fredj
Copy link
Member Author

fredj commented Mar 16, 2015

@elemoine @tsauerwein: any other comments?

@tsauerwein
Copy link
Member

No more objections from my side.

@elemoine
Copy link
Member

Looking again…

@elemoine
Copy link
Member

It looks good to me.

I just note that the style function will generate a lot of garbage if there is a lot of lines and segments. I know that this is not a problem in that particular example, but it would be good to always show good practices to users.

Instead of function(ol.Feature, number):Array.<ol.style.Style> the style function signature could possibly be function(ol.Feature, number, function(ol.style.Style)). And the style function would call the function passed as a third argument for each style it wants to generate.

So with this your example's style function could look like this:

var lineStyle = new ol.style.Style({
  stroke: new ol.style.Stroke({
    // …
  })
});

var arrowStyle = new ol.style.Style({
  image: new ol.style.Icon({
    // …
  })
});


function styleFunction(feature, resolution, callback) {
  callback(lineStyle);
  feature.getGeometry().forEachSegment(function(start, end) {
    var dx = end[0] - start[0];
    var dy = end[1] - start[1];
    var rotation = Math.atan2(dy, dx);
    arrowStyle.setGeometry(new ol.geom.Point(end));
    arrowStyle.getImage().setRotation(-rotation);
    callback(arrowStyle);
  });
}

Note something to do in this PR, but as a separate PR possibly.

@fredj fredj mentioned this pull request Mar 16, 2015
2 tasks
@fredj
Copy link
Member Author

fredj commented Mar 16, 2015

Thanks for the review

fredj added a commit that referenced this pull request Mar 16, 2015
@fredj fredj merged commit 3a7b975 into openlayers:master Mar 16, 2015
@fredj fredj deleted the line-arrows branch March 16, 2015 13:30
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.

3 participants