Handling Map Events

This documentation is no longer available on MSDN, however it is available as a CHM download.

The Bing Maps Silverlight Control can respond to events that are raised when the user interacts with a map. There are several events that are monitored by the map control such as keyboard, mouse, and touch input events, and map navigation actions. The following types of events are handled by the map control and are defined in the Map class:

  1. Successful loading of the map control. Events overridden by the Map class include:

  2. Changing the current map mode. Events overridden by the Map class include:

  3. Navigating around the map. Events overridden by the Map class include:

  4. Handling mouse input events. Events overridden by the Map class include:

  5. Handling keyboard input events. Events overridden by the Map class include:

  6. Handling screen touch events. Events overridden by the Map class include:

Ee681897.note(en-us,MSDN.10).gifNote:
For a complete list of events that are handled by the standard Map control, see the Map event members.

For the map control to start listening or override the default implementation of an event, you must register that event and define a method handler. For example, if you want to track the latitude and longitude position of the map, as it animates between a source and target view, you can register the ViewChangeOnFrame event when a map called MyMap (assumed to be defined in the XAML design code) is loaded.

Public Sub New()
    InitializeComponent()
    AddHandler MapWithEvents.ViewChangeOnFrame, AddressOf MapWithEvents_ViewChangeOnFrame
End Sub

Assuming you have a TextBlock element named CurrentPosition defined in the XAML design code, you can track the current position of a map view while it is animating between locations. This code tracks the position, in latitude and longitude, of the northwest and southeast corners of the bounded map view.

    Private Sub MyMap_ViewChangeOnFrame(ByVal sender As Object, ByVal e As MapEventArgs)
        'Gets the map that raised this event
        Dim map As Map = CType(sender, Map)
        'Gets the bounded rectangle for the current frame
        Dim bounds As LocationRect = map.BoundingRectangle
        'Update the current latitude and longitude
        CurrentPosition.Text += String.Format("Northwest: {0:F5}, Southeast: {1:F5} (Current)", bounds.Northwest, bounds.Southeast)
    End Sub

The following example wires several map events and displays the number of times each event occurred while navigating the map. Each event handler calls the ShowEvent method, which updates the display panel’s counter for the raised event (passed in as a string parameter). A single mouse click event changes the map mode between Road, Aerial, and AerialWithLabels.

Ee681897.d56cb281-19c8-440b-8f95-d57d687fab94(en-us,MSDN.10).jpg

Map Event Counter Panel

<UserControl x:Class="SilverlightTest1.MapEvents"
    xmlns="http://schemas.microsoft.com/winfx/2006/xaml/presentation" 
    xmlns:x="http://schemas.microsoft.com/winfx/2006/xaml" 
    xmlns:m="clr-namespace:Microsoft.Maps.MapControl;assembly=Microsoft.Maps.MapControl"
    Width="1024" Height="768">
    <Grid x:Name="LayoutRoot" Background="White">
        <m:Map x:Name="MapWithEvents" CredentialsProvider="your key" Mode="Road" Grid.Column="0" Grid.Row="1" />
        <Border Background="Black" VerticalAlignment="Top" HorizontalAlignment="Right"  Opacity="0.8" BorderBrush="White" BorderThickness="2" CornerRadius="5">
            <StackPanel x:Name="eventsPanel" IsHitTestVisible="False" Canvas.Left="0" Canvas.Top="0"/>
        </Border>
    </Grid>
</UserControl>
	


Imports System.Windows.Media
Imports Microsoft.Maps.MapControl


	Partial Public Class MapEvents
		Inherits UserControl
		' A collection of key/value pairs containing the event name 
		' and the text block to display the event to.
		Private eventBlocks As New Dictionary(Of String, TextBlock)()
		' A collection of key/value pairs containing the event name  
		' and the number of times the event fired.
		Private eventCount As New Dictionary(Of String, Integer)()

		Public Sub New()
			InitializeComponent()

		End Sub

		' Fires when the mouse is clicked, toggling between the Road, 
		' Aerial, and AerialWithLabels map modes.
		Private Sub MapWithEvents_MouseClick(ByVal sender As Object, ByVal e As MapMouseEventArgs) _
		    Handles MapWithEvents.MouseClick

			' Updates the count of single mouse clicks.
			ShowEvent("MapWithEvents_MouseClick")

			' Toggles between map modes.
			Select Case eventCount("MapWithEvents_MouseClick") Mod 2
				Case 0
					MapWithEvents.Mode = New RoadMode()
				Case 1
					MapWithEvents.Mode = New AerialMode()
			End Select

			' The event has been handled by this custom handler, disabling
			' the default single-click behavior.
			e.Handled = True
		End Sub
		
		' Fires when a drag box is created, if implemented
		' (e.g. hold CTRL and pan mouse)
		Private Sub MapWithEvents_MouseDragBox() Handles MapWithEvents.MouseDragBox
			' Updates the count of mouse drag boxes created.
			ShowEvent("MapWithEvents_MouseDragBox")
		End Sub

		' Fires when the mouse pans the map
		Private Sub MapWithEvents_MousePan()  Handles MapWithEvents.MousePan
			' Updates the count of mouse pans.
			ShowEvent("MapWithEvents_MousePan")
		End Sub

		' Fires when the mouse is double clicked
		Private Sub MapWithEvents_MouseDoubleClick() Handles MapWithEvents.MouseDoubleClick
			' Updates the count of mouse double clicks.
			ShowEvent("MapWithEvents_MouseDoubleClick")
		End Sub

		' Fires when the map view has reached its new target view.
		Private Sub MapWithEvents_ViewChangeEnd() Handles MapWithEvents.ViewChangeEnd
			'Updates the number of times the map view has changed.
			ShowEvent("ViewChangeEnd")
		End Sub

		' Fires when the map view starts to move to its new target view.
		Private Sub MapWithEvents_ViewChangeStart() Handles MapWithEvents.ViewChangeStart
			'Updates the number of times the map view started changing.
			ShowEvent("ViewChangeStart")
		End Sub

		' Fires every animated frame from one location to another.
		Private Sub MapWithEvents_ViewChangeOnFrame() Handles MapWithEvents.ViewChangeOnFrame
			' Updates the number of times a map view has changed 
			' during an animation from one location to another.
			ShowEvent("ViewChangeOnFrame")
		End Sub

		' Fires when the map view location has changed.
		Private Sub MapWithEvents_TargetViewChanged() Handles MapWithEvents.TargetViewChanged
			' Updates the number of map view changes that occured during
			' a zoom or pan.
			ShowEvent("TargetViewChange")
		End Sub

	    ' Fires when a different mode button on the navigation bar is selected.
		Private Sub MapWithEvents_ModeChanged() Handles MapWithEvents.ModeChanged
			' Updates the number of times the map mode changed.
			ShowEvent("ModeChanged")
		End Sub

		Private Sub ShowEvent(ByVal eventName As String)
			' Updates the display box showing the number of times 
			' the wired events occured.
			If Not eventBlocks.ContainsKey(eventName) Then
				Dim tb As New TextBlock()
				tb.Foreground = New SolidColorBrush(Color.FromArgb(255, 128, 255, 128))
				tb.Margin = New Thickness(5)
				eventBlocks.Add(eventName, tb)
				eventCount.Add(eventName, 0)
				eventsPanel.Children.Add(tb)
			End If

			eventCount(eventName) += 1
			eventBlocks(eventName).Text = eventName & ": [" & eventCount(eventName) & " times] " & _
			                              Now & " (HH:mm:ss:ffff)"
		End Sub
	End Class

	
 
Show: