Development and application of "Silverlight" Bing maps Five: Implement drag-and-drop (draggable) extension of Pushpin (Pushpin)
Currently, there is no drag-and-drop Pushpin (pushpin) control in Bing Maps, and its powerful, flexible architectural design provides developers with very powerful extended support to implement the drag-and-drop of Pushpin controls.
System.Object
System.Windows.DependencyObject
System.Windows.UIElement
System.Windows.FrameworkElement
System.Windows.Controls.Control
System.Windows.Controls.ContentControl
Microsoft.Maps.MapControl.Pushpin
Before extending Pushpin, understand the structure design of the Pushpin control in the Bing Maps Silverlight controls (such as the upper code structure), The Pushpin is developed by extending the ContentControl control in Silverlight and adding the positional coordinates (Location) attributes and other related properties and fields. In view of the position of the Pushpin is mainly used location attribute, so this article only proposed this attribute, the other field properties in detail in the subsequent related Bovenri introduction.
In the use of the Bing Maps Silverlight control Development, there are two main ways to use the built-in Pushpin (without the ability to drag and drop operations), which is to add to the Maplayer as a child object after the dynamic creation of the background code, Another way is to directly configure it directly in XAML code. The following code block:
<m:Map x:Name="myMap" Margin="0,0,0,0" CredentialsProvider="{StaticResource MyCredentials}">
<m:MapLayer x:Name="MapLayer">
<m:Pushpin Location="29.5076372217973, 106.489384971208" Content="D"></m:Pushpin>
</m:MapLayer>
</m:Map>
By the structure of the code snippet, it is clear that the parent of the Pushpin Control (pushpin) is a Maplayer object, and in Maplayer there is a property parentmap can get the current Maplayer object belonging to that map control object. It is then possible to get to the map control directly belonging to the Pushpin object, which means that the map control can be dynamically controlled in the Pushpin control.
Next, by customizing a drag-and-drop Pushpin class to extend the Pushpin controls built into the Bing Maps Silverlight control, you can implement a basic drag-and-drop Pushpin structure with the following code.
public class DraggablePushpin : Microsoft.Maps.MapControl.Pushpin
{
/// <summary>
/// 是否正在拖放
/// </summary>
private bool IsDragging = false;
/// <summary>
/// 鼠标拖放
/// </summary>
private EventHandler<MapMouseDragEventArgs> MapMouseDragHandler;
/// <summary>
/// 鼠标左键弹起
/// </summary>
private MouseButtonEventHandler MapMouseLeftButtonUpHandler;
/// <summary>
/// 鼠标移动
/// </summary>
private MouseEventHandler MapMouseMoveHandler;
}