使用Bing Maps Silverlight Control开发中,很多时候都需要实现在的地图上打点的功能,也就是通过鼠标点击事件处理当前地图上点击点添加一个标注(比如图钉),主要使用ViewportPointToLocation方法进行坐标转换,将鼠标所点击点的物理坐标转化为地理坐标(经度、纬度),该方法如下定义:
[ScriptableMemberAttribute]
public override Location ViewportPointToLocation (Point viewportPoint)
{}
public override Location ViewportPointToLocation (Point viewportPoint)
{}
namespace
Microsoft.Maps.MapControl
{
public class MapMouseEventArgs : MapEventArgs
{
public MapMouseEventArgs(Point viewportPoint);
[ScriptableMember]
public Point ViewportPoint { get ; }
}
}
{
public class MapMouseEventArgs : MapEventArgs
{
public MapMouseEventArgs(Point viewportPoint);
[ScriptableMember]
public Point ViewportPoint { get ; }
}
}
最近不少朋友问我Bing Maps Silverlight Control怎么没有和DeepEarth中提供的用于显示当前鼠标所在的地理位置(经度、纬度)的显示控件,在DeepEarth中我叫它坐标控件(CoordControl)。在Bing Maps Silverlight Control中确实没有坐标控件(CoordControl),但是Bing Maps Silverlight Control为我们提供了非常灵活的编程模型框架,可以通过扩展自己开发出这样的控件。
<
Border
Background
="#FF000000"
CornerRadius
="8,8,8"
Padding
="0,8"
Opacity
="0.68"
MinWidth
="190"
MinHeight
="30"
HorizontalAlignment ="Right" VerticalAlignment ="Bottom" Margin ="0,5,30" >
< TextBlock x:Name ="Coords" HorizontalAlignment ="Center" textwrapping ="Wrap" Foreground ="White" />
</ Border >
HorizontalAlignment ="Right" VerticalAlignment ="Bottom" Margin ="0,5,30" >
< TextBlock x:Name ="Coords" HorizontalAlignment ="Center" textwrapping ="Wrap" Foreground ="White" />
</ Border >
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( " 坐标: {0:f6},{1:f6} " , location.Longitude, location.Latitude);
}
}
{
Point viewportPoint = e.GetPosition(map);
Location location;
if (map.TryViewportPointToLocation(viewportPoint, out location))
{
Coords.Text = String.Format( " 坐标: {0:f6},{1:f6} " , location.Longitude, location.Latitude);
}
}
以上是直接在Map所在页面实现的,我们也可以将其封装为Silverlight用户控件,具体实现就是将上面的Border布局的界面那一堆代码移植到Silverlignt UserControl中去,如下XAML代码块:
<
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" Opacity ="0.68" MinWidth ="190" MinHeight ="30"
HorizontalAlignment ="Right" VerticalAlignment ="Bottom" Margin ="0,30" >
< TextBlock x:Name ="Coords" HorizontalAlignment ="Center" textwrapping ="Wrap" Foreground ="White" />
</ Border >
</ Grid >
</ UserControl >
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" Opacity ="0.68" MinWidth ="190" MinHeight ="30"
HorizontalAlignment ="Right" VerticalAlignment ="Bottom" Margin ="0,30" >
< TextBlock x:Name ="Coords" HorizontalAlignment ="Center" textwrapping ="Wrap" Foreground ="White" />
</ Border >
</ Grid >
</ UserControl >
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( " 坐标: {0:f6}, location.Latitude);
}
};
}
}
}
{
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( " 坐标: {0:f6}, location.Latitude);
}
};
}
}
}
LayoutRoot.Children.Add(
new
CoordControl(
this
.map));
推荐博文:
中国Bing Maps:
http://cn.bing.com/ditu/
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 [email protected] 举报,一经查实,本站将立刻删除。