Posted on

Arrows in Leaflet

I know, there are some libraries for drawing lines with arrows, like Leaflet.TextPath or Leaflet.PolylineDecorator. But this time I wanted to create something really simple, that could fit my defined objects.

 

The biggest problem with arrows is to calculate and manipulate with angles, because that’s the reason of arrows. The theory is quite simple, we just need to google the trigonometric functions and Pythagorean theorem. We will improvise a little and act like our coordinate system is cartesian. Here we can see each line as a hypotenuse and we are able to calculate its length with Pythagorean theorem and differences in latitude and longitude between two points that are defining our line.

From trigonometric functions we will need arctan (Math.atan2 in javascript), that defines our angle from the ratio of our latitude and longitude differences. The javascript code should look something like (we also need to multiply out angle by 57.295779513082 to get grades) :

 

The next step is to define DivIcon class. This class is for setting custom icons for markers in leaflet map. One from the options here is “html” parameter. Here we are able to put custom html code (also with css) for displaying with our marker. I have found kind of a database with arrows in unicode and chose “➨” (one problem here is, that at this point we need to choose an arrow heading right or rewrite our angle calculation to have zero somewhere else). Then our divIcon should look like this :

 

We could also extend Polyline class with new function and put everything inside:

 

Working example is then on jsfiddle.

arrows_sample

 

PS: The problem here is, as I mentioned, we are counting with cartesian coordinate system, that UTM is not. Therefore we will have some problems with bigger latitudes, where our angles will not be so accurate.

Leave a Reply