How to create a time layer animation track


This sample code creates a time layer animation track and adds it to the animation track container. It illustrates how to create a new time layer animation track, set time layer keyframes, and work with the time window object. The procedure "CreateAGAnimTrackTimeLayer" creates two time layer keyframes, which is the minimum requirement for a time layer animation track to play. However, more keyframes can be created following the same method.

How to use

  1. Open ArcMap and add a layer with a time field to the ArcMap TOC;
  2. Copy and paste the following code into the VB editor in ArcMap;
  3. Make sure that "ESRI Animation Object Library" and "ESRI AnimationUI Object Library" are checked in the References dialog box of the VBA project;
  4. Change the variables used by the "CreateAGAnimTrackTimeLayer" function according to your data;
  5. Run the macro "CreateAndAddTimeTrack";
  6. Click the Animation dropdown on the animation toolbar and click Animation Manager. In the Animation Manager dialog box, click the Tracks tab to verify that a new time animation track has been created.
[VBA]
Public Sub CreateAndAddTimeTrack()
    'Get the animation extension
    Dim pAnimExt As IAnimationExtension
    Set pAnimExt = FindAnimationExtension()
    
    'Get the animation track container from the animation extension
    Dim pAnimTracks As IAGAnimationTracks
    Set pAnimTracks = pAnimExt.AnimationTracks
    
    'Get the first layer in TOC (assuming the first layer is a time layer)
    Dim pMxDoc As IMxDocument
    Set pMxDoc = ThisDocument
    Dim pLayer As ILayer
    Set pLayer = pMxDoc.Maps.Item(0).Layer(0)
    
    Dim dTrackStartTime As Date
    Dim dTrackEndTime As Date
    Dim dblInterval As Double
    Dim iTimeUnits As Integer
    Dim sStartTimeField As String
    Dim sStartTimeFieldFormat As String
    Dim sEndTimeField As String
    Dim sEndTimeFieldFormat As String
    Dim bAnimateCumulatively As Boolean
    Dim bShowText As Boolean
    
    'Change the following variables according to your data
    dTrackStartTime = #7 / 20 / 1988#
    dTrackEndTime = #9 / 10 / 1988#
    dblInterval = 1#
    iTimeUnits = esriTimeUnitsDays
    sStartTimeField = "Date_"
    sStartTimeFieldFormat = ""
    sEndTimeField = ""
    sEndTimeFieldFormat = ""
    bAnimateCumulatively = True
    bShowText = True
    
    'Create the track
    Dim pAnimTrack As IAGAnimationTrack
    Set pAnimTrack = CreateAGAnimTrackTimeLayer(pLayer, dTrackStartTime, dTrackEndTime, dblInterval, _
                     iTimeUnits, sStartTimeField, sStartTimeFieldFormat, _
                     sEndTimeField, sEndTimeFieldFormat, bAnimateCumulatively, _
                     bShowText)
    
    'Add the track to the animation track container so that it is visible in the Animation Manager
    pAnimTracks.AddTrack pAnimTrack
    pAnimExt.AnimationContentsModified
    
End Sub

Private Function CreateAGAnimTrackTimeLayer(ByRef pLayer As ILayer, _
                                            ByVal startTime As Date, ByVal endTime As Date, _
                                            ByVal interval As Double, ByVal intervalUnits As Long, _
                                            ByVal startField As String, ByVal startFieldFormat As String, _
                                            ByVal endField As String, ByVal endFieldFormat As String, _
                                            ByVal animateCumulatively As Boolean, ByVal bShowTimeText As Boolean) As IAGAnimationTrack
    
    Dim pAnimTrack As IAGAnimationTrack
    Dim pAnimTrackKeyframes As IAGAnimationTrackKeyframes
    Dim pKeyframe As IAGKeyframe
    Dim pAnimtype As IAGAnimationType
    
    Set pAnimtype = New AnimationTypeTimeLayer
    Set pKeyframe = New TimeLayerKeyframe
    Set pAnimTrack = New AGAnimationTrack
    Set pAnimTrackKeyframes = pAnimTrack 'QI
    
    'Set the AnimationType property of the animation track
    Set pAnimTrack.AnimationType = pAnimtype
    pAnimTrack.Name = "Timelayertrack1"
    
    'Set keyframe 1
    pAnimTrackKeyframes.InsertKeyframe pKeyframe, -1
    pKeyframe.TimeStamp = 0
    pKeyframe.Name = "Timelayerkeyframe1"
    
    Dim myDate As Date
    myDate = startTime
    pKeyframe.PropertyValue(0) = DateValue(myDate)
    pKeyframe.PropertyValue(1) = interval
    pKeyframe.PropertyValue(2) = intervalUnits
    
    'Set keyframe 2
    Set pKeyframe = New TimeLayerKeyframe
    pAnimTrackKeyframes.InsertKeyframe pKeyframe, -1
    pKeyframe.TimeStamp = 1
    pKeyframe.Name = "Timelayerkeyframe2"
    
    myDate = endTime
    pKeyframe.PropertyValue(0) = DateValue(myDate)
    pKeyframe.PropertyValue(1) = interval
    pKeyframe.PropertyValue(2) = intervalUnits
    
    'Attach the layer object to the track
    pAnimTrack.AttachObject pLayer
    
    'Set time window properties for the layer
    Dim pAnimTypeTimeLayer As IAnimationTypeTimeLayer
    Set pAnimTypeTimeLayer = pAnimTrack.AnimationType
    Dim pTimeWnd As ITimeWindow
    pAnimTypeTimeLayer.GetTimeWindow pAnimTrack, pLayer, pTimeWnd
    pTimeWnd.startField = startField
    pTimeWnd.startFieldFormat = startFieldFormat
    pTimeWnd.endField = endField
    pTimeWnd.endFieldFormat = endFieldFormat
    pTimeWnd.animateCumulatively = animateCumulatively
    
    'Set time text display properties
    Dim pTimeTrackExt As ITimeLayerTrackExtension
    Set pTimeTrackExt = pAnimTypeTimeLayer.GetTimeLayerTrackExtension(pAnimTrack)
    pTimeTrackExt.ShowTrackTime = bShowTimeText
    
    Set CreateAGAnimTrackTimeLayer = pAnimTrack
End Function

Private Function FindAnimationExtension() As IAnimationExtension
    Dim pUID As New UID
    pUID.Value = "esriAnimation.AnimationExtension"
    Dim pAnimExt As IAnimationExtension
    Set pAnimExt = Application.FindExtensionByCLSID(pUID)
    
    Set FindAnimationExtension = pAnimExt
End Function