var mapcompLayerName = "{{mapcompLayerName}}";
var mapcompName = "{{mapcompName}}";
var latLongPropName = "{{latLongPropName}}";
var initialHeightNormalizeVal = {{initialHeightNormalizer}};
var mapcompTileSize = {{tileSize}};
var isShapeAnchor = {{isShapeAnchor}};

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(mapcompTileSize << layersMaxZL);
    pixelX = x * mapSize + 0.5;
    pixelY = clipValue(y * mapSize + 0.5, 0, mapSize - 1);
    return [pixelX, pixelY];
}
//try{
    try {
        var isParented = parent;
    } catch (e) {
        var isParented = null;
    }
    var pivotLayer = comp(mapcompName).layer("MapPivot");
    var layersMaxZL = pivotLayer.transform.opacity.value;
    var diffTime = thisComp.layer(mapcompLayerName).startTime;
    var min_lat = {{minLat}};
    var max_lat = {{maxLat}};
    //var min_lng = ' + String(TileUtils.min_lng) + ';
    //var max_lng = ' + String(TileUtils.max_lng) + ';
    var effectLatLong = effect(latLongPropName)(1);
    var effectLatLongCurrentVal = effectLatLong.valueAtTime(time)
    var heightOffset = effectLatLongCurrentVal[2];
    var scaleValNormalizer = 1;
    if(heightOffset > 0){
        scaleValNormalizer = pivotLayer.transform.scale.valueAtTime(time - diffTime)[1] * Math.pow(2,pivotLayer.transform.opacity.value) / initialHeightNormalizeVal;
    }
    var coordsPX = LatLongToPixelXY(effectLatLongCurrentVal[0], effectLatLongCurrentVal[1], layersMaxZL);
    var x = coordsPX[0];
    var y = coordsPX[1];
    var pivotLayerToWorld = pivotLayer.toWorld([x, y, effectLatLong.value[2]], time - diffTime);
    var pivotLayerToWorldHeightOffset = [pivotLayerToWorld[0], pivotLayerToWorld[1], heightOffset*-scaleValNormalizer]
    var mapCompLayer = thisComp.layer(mapcompLayerName);
    if (isParented == null) {
        if (value.length == 2) {
            mapCompLayer.toComp(pivotLayerToWorldHeightOffset);
        } else {
            mapCompLayer.toWorld(pivotLayerToWorldHeightOffset);
        }
    } else {
        if (isParented == mapCompLayer) {
            if(isShapeAnchor){
                [pivotLayerToWorldHeightOffset[0], pivotLayerToWorldHeightOffset[1], -0.001];
            }else{
                [pivotLayerToWorldHeightOffset[0], pivotLayerToWorldHeightOffset[1], pivotLayerToWorldHeightOffset[2]];
            }
            
        } else {
            if (value.length == 2) {
                isParented.fromCompToSurface(mapCompLayer.toComp(pivotLayerToWorldHeightOffset));
            } else {
                var toSurface = isParented.fromCompToSurface(mapCompLayer.toComp(pivotLayerToWorldHeightOffset));
                [toSurface[0], toSurface[1], 0]
            }
        }
    }
//}catch(e){
//value
//}