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 TrackCurrentMapPosition
{
    InitializeComponent();
    MyMap.ViewChangeOnFrame += new EventHandler<MapEventArgs>(MyMap_ViewChangeOnFrame);
}

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.


void MyMap_ViewChangeOnFrame(object sender, MapEventArgs e)
{
    //Gets the map that raised this event
    Map map = (Map) sender;
    //Gets the bounded rectangle for the current frame
    LocationRect bounds = map.BoundingRectangle;
    //Update the current latitude and longitude
    CurrentPosition.Text += String.Format("Northwest: {0:F5}, Southeast: {1:F5} (Current)",
                bounds.Northwest, bounds.Southeast);
}

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>
	
using System;
using System.Collections.Generic;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Media;
using Microsoft.Maps.MapControl;

namespace SilverlightTest1
{
    public partial class MapEvents : UserControl
    {
        // A collection of key/value pairs containing the event name 
        // and the text block to display the event to.
        Dictionary<string, TextBlock> eventBlocks = new Dictionary<string, TextBlock>();
        // A collection of key/value pairs containing the event name  
        // and the number of times the event fired.
        Dictionary<string, int> eventCount = new Dictionary<string, int>();

        public MapEvents()
        {
            InitializeComponent();

            // Fires every animated frame from one location to another.
            MapWithEvents.ViewChangeOnFrame += 
                new EventHandler<MapEventArgs>(MapWithEvents_ViewChangeOnFrame);
            // Fires when the map view location has changed.
            MapWithEvents.TargetViewChanged += 
                new EventHandler<MapEventArgs>(MapWithEvents_TargetViewChanged);
            // Fires when the map view starts to move to its new target view.
            MapWithEvents.ViewChangeStart += 
                new EventHandler<MapEventArgs>(MapWithEvents_ViewChangeStart);
            // Fires when the map view has reached its new target view.
            MapWithEvents.ViewChangeEnd += 
                new EventHandler<MapEventArgs>(MapWithEvents_ViewChangeEnd);
            // Fires when a different mode button on the navigation bar is selected.
            MapWithEvents.ModeChanged += 
                new EventHandler<MapEventArgs>(MapWithEvents_ModeChanged);
            // Fires when the mouse is double clicked
            MapWithEvents.MouseDoubleClick += 
                new EventHandler<MapMouseEventArgs>(MapWithEvents_MouseDoubleClick);
            // Fires when the mouse pans the map
            MapWithEvents.MousePan += 
                new EventHandler<MapMouseDragEventArgs>(MapWithEvents_MousePan);
            // Fires when a drag box is created, if implemented
            // (e.g. hold CTRL and pan mouse)
            MapWithEvents.MouseDragBox += 
                new EventHandler<MapMouseDragEventArgs>(MapWithEvents_MouseDragBox);

            // Fires when the mouse is clicked, toggling between the Road, 
            // Aerial, and AerialWithLabels map modes.
            MapWithEvents.MouseClick += 
                new EventHandler<MapMouseEventArgs>(MapWithEvents_MouseClick);
        }

        void MapWithEvents_MouseClick(object sender, MapMouseEventArgs e)
        {
            // Updates the count of single mouse clicks.
            ShowEvent("MapWithEvents_MouseClick");

            // Toggles between map modes.
            switch (eventCount["MapWithEvents_MouseClick"] % 2)
            {
                case 0:
                    MapWithEvents.Mode = new RoadMode();
                    break;
                case 1:
                    MapWithEvents.Mode = new AerialMode();
                    break;
            }

            // The event has been handled by this custom handler, disabling
            // the default single-click behavior.
            e.Handled = true;
        }

        void MapWithEvents_MouseDragBox(object sender, MapMouseDragEventArgs e)
        {
            // Updates the count of mouse drag boxes created.
            ShowEvent("MapWithEvents_MouseDragBox");
        }

        void MapWithEvents_MousePan(object sender, MapMouseDragEventArgs e)
        {
            // Updates the count of mouse pans.
            ShowEvent("MapWithEvents_MousePan");
        }

        void MapWithEvents_MouseDoubleClick(object sender, MapMouseEventArgs e)
        {
            // Updates the count of mouse double clicks.
            ShowEvent("MapWithEvents_MouseDoubleClick");
        }

        void MapWithEvents_ViewChangeEnd(object sender, MapEventArgs e)
        {
            //Updates the number of times the map view has changed.
            ShowEvent("ViewChangeEnd");
        }

        void MapWithEvents_ViewChangeStart(object sender, MapEventArgs e)
        {
            //Updates the number of times the map view started changing.
            ShowEvent("ViewChangeStart");
        }

        void MapWithEvents_ViewChangeOnFrame(object sender, MapEventArgs e)
        {
            // Updates the number of times a map view has changed 
            // during an animation from one location to another.
            ShowEvent("ViewChangeOnFrame");
        }
        void MapWithEvents_TargetViewChanged(object sender, MapEventArgs e)
        {
            // Updates the number of map view changes that occured during
            // a zoom or pan.
            ShowEvent("TargetViewChange");
        }

        void MapWithEvents_ModeChanged(object sender, MapEventArgs e)
        {
            // Updates the number of times the map mode changed.
            ShowEvent("ModeChanged");
        }

        void ShowEvent(string eventName)
        {
            // Updates the display box showing the number of times 
            // the wired events occured.
            if (!eventBlocks.ContainsKey(eventName))
            {
                TextBlock tb = 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);
            }

            eventCount[eventName]++;
            eventBlocks[eventName].Text = String.Format(
                "{0}: [{1} times] {2} (HH:mm:ss:ffff)",
                eventName, eventCount[eventName].ToString(), DateTime.Now.ToString());
        }
    }
}
	
Show: