public static void CreateMapGraphicTrack(ICreateGraphicTrackOptions pOptions, IAGAnimationTracks tracks, IAGAnimationContainer pContainer)
{
pOptions.PathGeometry=SimplifyPath2D(pOptions.PathGeometry,pOptions.ReverseOrder,pOptions.SimplificationFactor);
IAGAnimationType animType = new AnimationTypeMapGraphic();
//remove tracks with the same name if overwrite is true
if (pOptions.OverwriteTrack == true)
{
IArray trackArray = new ArrayClass();
trackArray = tracks.get_TracksOfType(animType);
int count = trackArray.Count;
for (int i = 0; i < count; i++)
{
IAGAnimationTrack temp = (IAGAnimationTrack)trackArray.get_Element(i);
if (temp.Name == pOptions.TrackName)
tracks.RemoveTrack(temp);
}
}
//create the new track
IAGAnimationTrack animTrack = tracks.CreateTrack(animType);
IAGAnimationTrackKeyframes animTrackKeyframes = (IAGAnimationTrackKeyframes)animTrack;
animTrackKeyframes.EvenTimeStamps = false;
animTrack.Name = pOptions.TrackName;
IGeometry path = pOptions.PathGeometry;
IPointCollection pointCollection = (IPointCollection)path;
ICurve curve = (ICurve)path;
double length = curve.Length;
double accuLength = 0;
//loop through all points to create the keyframes
int pointCount = pointCollection.PointCount;
if (pointCount <= 1)
return;
for (int i = 0; i < pointCount; i++)
{
IPoint currentPoint = pointCollection.get_Point(i);
IPoint nextPoint = new PointClass();
if (i < pointCount-1)
nextPoint = pointCollection.get_Point(i + 1);
IPoint lastPoint = new PointClass();
if (i == 0)
lastPoint = currentPoint;
else
lastPoint = pointCollection.get_Point(i-1);
IAGKeyframe tempKeyframe = animTrackKeyframes.CreateKeyframe(i);
//set keyframe properties
double x;
double y;
currentPoint.QueryCoords(out x, out y);
tempKeyframe.set_PropertyValue(0, currentPoint);
tempKeyframe.Name = "Map Graphic keyframe " + i.ToString();
//set keyframe timestamp
accuLength += CalculateDistance(lastPoint, currentPoint);
double timeStamp = accuLength / length;
tempKeyframe.TimeStamp = timeStamp;
double x1;
double y1;
double angle;
if (i < pointCount - 1)
{
nextPoint.QueryCoords(out x1, out y1);
if ((y1 - y) > 0)
angle = Math.Acos((x1 - x) / Math.Sqrt((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y)));
else
{
angle = 6.2832 - Math.Acos((x1 - x) / Math.Sqrt((x1 - x) * (x1 - x) + (y1 - y) * (y1 - y)));
}
tempKeyframe.set_PropertyValue(1, angle);
}
else
{
IAGKeyframe lastKeyframe = animTrackKeyframes.get_Keyframe(i-1);
tempKeyframe.set_PropertyValue(1, lastKeyframe.get_PropertyValue(1));
}
}
//attach the point element
if(pOptions.PointElement != null)
animTrack.AttachObject(pOptions.PointElement);
//attach the track extension, which contains a line element for trace
IMapGraphicTrackExtension graphicTrackExtension = new MapGraphicTrackExtension();
graphicTrackExtension.ShowTrace = pOptions.AnimatePath;
IAGAnimationTrackExtensions trackExtensions = (IAGAnimationTrackExtensions)animTrack;
trackExtensions.AddExtension(graphicTrackExtension);
}