shramov / leaflet-plugins

Plugins for Leaflet library
http://psha.org.ru/b/leaflet-plugins.html
MIT License
725 stars 288 forks source link

Marker Rotation over shortest angle #210

Closed thomasbrueggemann closed 8 years ago

thomasbrueggemann commented 8 years ago

I added a function that calculates the shortest angle between the current marker angle and the new - to be set - marker angle.

This is especially interesting and necessary if the marker movements are animated, e.g. with the following CSS transition:

<style>
    /* Animates marker movements */
    .leaflet-marker-pane > * {
        -webkit-transition: transform 1.0s linear;
        -moz-transition: transform 1.0s linear;
        -o-transition: transform 1.0s linear;
        -ms-transition: transform 1.0s linear;
        transition: transform 1.0s linear;
    }
</style>

Without the shortest angle calculation, the animation would always turn clockwise, even though an angle change from 10° to 340° is shorter to turn counter clockwise.

For non-animated marker movements, there won't be any noticeable difference in rotation.

Credits for the shortest-angle function: stefbw

brunob commented 8 years ago

I was to going to merge this one, but i need to understand your case.

From my side, if i start form angle at 10 + marker.setIconAngle(340); it turns clockwise. But now, if i call marker.setIconAngle(10); it turns anti clockwise...

And i can also, starting form angle 10, call marker.setIconAngle(-20); and i get an angle of 340 turning anti clockwise.

So, i'd like to understand what's exactlly your problem :)

Anyway, thx for the transition tip, i'll add it to the example file.

thomasbrueggemann commented 8 years ago

Okay, I see. Well if you try to animate a continous stream of GPS positions with the marker and want to rotate the marker according to the GPS heading information (which is of course always an absolute integer between 0 and 359) and also use the CSS transition, you would want a turn vom 10 to 340 degrees to be counterclockwise.

So given the parameters 10 and 340 it would calculate a -20. So I guess the function only adds value to people that use the marker rotation with absolute heading values and it should be used outside of your marker.rotate function.

Or maybe added as an additional function: setHeading() or something What do you think?

brunob commented 8 years ago

Ok, i see now, thx for the details :)

I think we can merge this patch as is, wait for feedback and switch back to the setHeading alernative if it cause too much issues.

Any thoughts about it @thomasbrueggemann ?

thomasbrueggemann commented 8 years ago

Cool :)

brunob commented 8 years ago

Et voilà :)