var smoothing = {{smoothing}};//a value between 0 and 1 describing the smoothness of the rotation
var latLongPropName = "{{latLongPropName}}"
var min_lat = {{minLat}};
var max_lat = {{maxLat}};
var output

var clipValue = function(n, min, max) {
	return Math.min(Math.max(n, min), max);
}
var LatLongToPixelXY = function(latitude, longitude, layersMaxZL) {
    latitude = clipValue(latitude, min_lat, max_lat);
    //longitude = clipValue(longitude, min_lng, max_lng);
    x = (longitude + 180) / 360;
    sinLatitude = Math.sin(latitude * Math.PI / 180);
    y = 0.5 - Math.log((1 + sinLatitude) / (1 - sinLatitude)) / (4 * Math.PI);
    mapSize = Math.abs(256 << 23);//tilesize and zl irrelevant for direction vector
    pixelX = x * mapSize + 0.5;
    pixelY = clipValue(y * mapSize + 0.5, 0, mapSize - 1);
    return [pixelX, pixelY];
}
var getDirectionVec = function(effectLatLong, t1, t2){
    var latLon1= effectLatLong.valueAtTime(t1);
    var latLon2= effectLatLong.valueAtTime(t2);
    var p1 = LatLongToPixelXY(latLon1[0], latLon1[1])
    var p2 = LatLongToPixelXY(latLon2[0], latLon2[1])
    return p2-p1;
}
var getPreviousKeyIndex = function(prop){
    var nearestKey = prop.nearestKey(time)
    var previousKeyIndex = nearestKey.index;
    if(nearestKey.time > time){
        previousKeyIndex --
    }
    return previousKeyIndex
}

var effectLatLong = effect(latLongPropName)(1);
var v = getDirectionVec(effectLatLong, time-smoothing/2, time+framesToTime(1)+smoothing/2)

if(v[0] == 0 && v[1] == 0){  
    var previousKeyIndex = getPreviousKeyIndex(effectLatLong)
    if(previousKeyIndex>0){
        var previousKeyTime = effectLatLong.key(previousKeyIndex).time
        v = getDirectionVec(effectLatLong, previousKeyTime-0.01, previousKeyTime)
    }else{
        var nextKeyTime = effectLatLong.key(previousKeyIndex+1).time
        v = getDirectionVec(effectLatLong, nextKeyTime, nextKeyTime+0.01)
    }
}
output = radiansToDegrees(Math.atan2(v[1], v[0]))+value;

output