Durante il mio studio di Silverlight e Bing Maps ho avuto la necessità di creare un’animazione utilizzando la storyboard ma il problema è che tra le DependencyProperty del controllo Map, da associare al target della storyboard, non c’era una DependencyProperty per poter effettuare l’animazione sulla centratura della cartina. Perciò ho cominciato a googlare e a bingare per cercare qualche informazione o qualche risorsa fino a che non ho trovato la soluzione
Poi ho pensato di scrivere questo post per non dimenticare e nel caso potesse servire a qualcuno.
Prima creo una nuova DependencyProperty
Imports Microsoft.Maps
Imports Microsoft.Maps.MapControl
Imports Microsoft.Maps.MapControl.Core
Imports StoryboardTest.GeocodeReference
Public Class NewAnimationProperty
Public Shared ReadOnly MapCenterProperty As DependencyProperty = DependencyProperty.RegisterAttached("MapCenter", GetType(Point), GetType(NewAnimationProperty), New PropertyMetadata(New PropertyChangedCallback(AddressOf OnMapCenterChanged)))
Public Shared Sub SetMapCenter(o As DependencyObject, value As Point)
o.SetValue(MapCenterProperty, value)
End Sub
Public Shared Function GetMapCenter(o As DependencyObject) As Point
Return DirectCast(o.GetValue(MapCenterProperty), Point)
End Function
Private Shared Sub OnMapCenterChanged(d As DependencyObject, e As DependencyPropertyChangedEventArgs)
Dim MapLocation As MapControl.Location = DirectCast(DirectCast(d, Map).GetValue(Map.CenterProperty), MapControl.Location)
Dim PointLoaction As New Point(MapLocation.Latitude, MapLocation.Longitude)
PointLoaction = DirectCast(e.NewValue, Point)
Dim LocationZoom As Double = CDbl(DirectCast(d, Map).ZoomLevel)
' DirectCast(d, Map).Center = New MapControl.Location(PointLoaction.X, PointLoaction.Y)
DirectCast(d, Map).SetView(New MapControl.Location(PointLoaction.X, PointLoaction.Y), LocationZoom)
End Sub
End Class
Come si può notare per poter effettuare l’animazione senza troppi preamboli, sfruttando la classe PointAnimation, non ho fatto altro che effettuare una conversione da Point a Location e viceversa.
Per sfruttare la DependecyProperty nell’animazione il codice è il seguente:
Dim sb_Center As New Storyboard
Dim pa_Center As New PointAnimation
pa_Center.Duration = New Duration(TimeSpan.FromSeconds(5))
pa_Center.From = New Point(0, 0)
pa_Center.To = New Point(41.902785999999999, 12.496225000000001)
Storyboard.SetTarget(pa_Center, Map1)
Storyboard.SetTargetProperty(pa_Center, New PropertyPath(NewAnimationProperty.MapCenterProperty))
sb_Center.Children.Add(pa_Center)
sb_Center.Begin()
Dim x As New Pushpin
x.Location = New MapControl.Location(41.902785999999999, 12.496226)
Map1.Children.Add(x)
Dim sb_Zoom As New Storyboard
Dim pa_Zoom As New DoubleAnimation
pa_Zoom.Duration = New Duration(TimeSpan.FromSeconds(5))
pa_Zoom.From = 5
pa_Zoom.To = 13
Storyboard.SetTarget(pa_Zoom, Map1)
Storyboard.SetTargetProperty(pa_Zoom, New PropertyPath(Map.ZoomLevelProperty))
sb_Zoom.Children.Add(pa_Zoom)
sb_Zoom.Begin()
Per rendere l’effetto più accattivante ho inserito anche un’animazione per lo zoom.
Francesco Valentino