In the development of Bing Maps Silverlight Control, the map hitting function is often required, that is to say, you can click an event to process the current map and click a vertex to add a annotation (compared to a dingtalk). The ViewportPointToLocation method is used for coordinate conversion, convert the physical coordinates of the clicked point to geographical coordinates (longitude and latitude). The method is defined as follows:
[ScriptableMemberAttribute]
Public override Location ViewportPointToLocation (Point viewportPoint)
{}
Clicking the mouse on the map triggers a series of map mouse events (MapMouseEventArgs). The event parameters can directly or obtain the physical coordinates of the current mouse clicking point, the event class is defined as follows:
Namespace Microsoft. Maps. MapControl
{
Public class MapMouseEventArgs: MapEventArgs
{
Public MapMouseEventArgs (Point viewportPoint );
[ScriptableMember]
Public Point ViewportPoint {get ;}
}
}
After learning about the preceding two key points, you can click the event on the map. For example, you can click the event and click the event on the map. The Code is as follows:
Protected void map_MouseClick (object sender, MapMouseEventArgs e)
{
// Initialize an icon
Pushpin pushpin = new Pushpin ();
// Set the positioning coordinate of the graph nail object
Pushpin. Location = map. ViewportPointToLocation (e. ViewportPoint );
// Add a map to the map
Map. Children. Add (pushpin );
}
Recently, many people have asked me how Bing Maps Silverlight Control does not display the geographical location (longitude and latitude) of the current mouse in DeepEarth, in DeepEarth, I call it a CoordControl ). There is indeed no CoordControl in Bing Maps Silverlight Control, but Bing Maps Silverlight Control provides us with a flexible programming model framework that can be developed by itself through extension.
First, design an appearance effect for the coordinate display control, use the Border layout, and set its horizontal to right, vertical to bottom alignment. As follows:
<Border Background = "# FF000000" CornerRadius = "8, 8, 8" Padding = "0.68, 190" Opacity = "" MinWidth = "" MinHeight = "30"
HorizontalAlignment = "Right" VerticalAlignment = "Bottom" Margin = ",">
<TextBlock x: Name = "Coords" HorizontalAlignment = "Center" TextWrapping = "Wrap" Foreground = "White"/>
</Border>
The preceding control interface design uses a Coords TextBlock control to display the geographic coordinates of the current mouse pointer. The MouseMove event of the Map object is used to display the coordinates:
Protected void map_MouseMove (object sender, MouseEventArgs e)
{
Point viewportPoint = e. GetPosition (map );
Location location;
If (map. TryViewportPointToLocation (viewportPoint, out location ))
{
Coords. Text = String. Format ("coordinates: {0: f6}, {1: f6}", location. longpolling, location. Latitude );
}
}
The above is implemented directly on the page where Map is located. We can also encapsulate it as a Silverlight user control, the specific implementation is to port the pile of code on the above Border layout interface to Silverlignt UserControl, the following XAML code block:
<UserControl x: Class = "BingMapsTraining. UIComponents. CoordControl"
Xmlns = "http://schemas.microsoft.com/winfx/2006/xaml/presentation"
Xmlns: x = "http://schemas.microsoft.com/winfx/2006/xaml">
<Grid x: Name = "LayoutRoot">
<Border Background = "# FF000000" CornerRadius = "8, 8, 8" Padding = "0.68, 190" Opacity = "" MinWidth = "" MinHeight = "30"
HorizontalAlignment = "Right" VerticalAlignment = "Bottom" Margin = ",">
<TextBlock x: Name = "Coords" HorizontalAlignment = "Center" TextWrapping = "Wrap" Foreground = "White"/>
</Border>
</Grid>
</UserControl>
Next, we need to reload or rewrite the constructor method of the control, so that a Map object parameter can be passed during external calls. In the constructor, we can monitor and process the MouseMove event of the Map object.
Public partial class CoordControl: UserControl
{
Private CoordControl ()
{
InitializeComponent ();
}
Public CoordControl (Map MapInstance)
: This ()
{
If (MapInstance! = Null)
{
MapInstance. MouseMove + = (sender, e) =>
{
Point viewportPoint = e. GetPosition (MapInstance );
Location location;
If (MapInstance. TryViewportPointToLocation (viewportPoint, out location ))
{
Coords. Text = String. Format ("coordinates: {0: f6}, {1: f6}", location. longpolling, location. Latitude );
}
};
}
}
}
After the coordinate control is encapsulated in the preceding method, the call is simpler. You only need to instantiate an object as a Silverlight sub-element and add it to the layout container, the following code:
LayoutRoot. Children. Add (new CoordControl (this. map ));
Recommended blog posts:
[Silverlight] Bing Maps Development Series
MSDN: http://msdn.microsoft.com/en-us/library/ee681890.aspx
Http://www.microsoft.com/maps
Bing Maps: http://cn.bing.com/ditu/
Official SDK: http://www.microsoft.com/maps/isdk/silverlight/
Copyright description
This article is an original article. You are welcome to repost it and indicate the source of the Article. Its copyright belongs to the author and the blog Park.
Author: Beniao
Article Source: http://beniao.cnblogs.com/or http://www.cnblogs.com/