In this short post we are going to discuss how to display label on the LineString. This can be useful if you want to add some information along the line, for example start and destination city:
Label along the LineString geometry
In our case the label would be OpenLayer’s Text style for vector feature. It contains property named: rotation, which contains rotation in radians (positive rotation clockwise).
In order to calculate rotation value we need to do bearing to angle conversion (for more information on math behind this: Calculate distance, bearing and more between Latitude/Longitude points):
Here is the source code which add label to Line feature and display text on it, you can view it on github: