【三】Bing Maps Silverlight 控件 之 标注地图

[b]图钉标签[/b]

如果我们需要在Bing Maps中加入一个小图钉标记,该如何实现了?Bing Maps地图控件直接提供了图钉层,通过内嵌的方式既可加入图钉层,默认使用Bing Maps提供的图形标记:
[code]
<m:Map ZoomLevel="14" Name="myMap" Center="39.9,116.4" CredentialsProvider="Your Bing Map Key">
<m:Map.Mode>
<m:AerialMode Labels="True" FadingLabels="True"/>
</m:Map.Mode>
<m:Pushpin Location="39.9,116.4"></m:Pushpin>
</m:Map>
[/code]

[img]http://dl.iteye.com/upload/attachment/261379/f5219853-ec52-3ab7-b38d-c2473020d523.png[/img]

通过编程的方式,也可以得到类似的结果,我们可以为地图层增加一个鼠标单击响应事件:每次点击都会在地图上打上一个图钉:
[code]
private void OnMouseClick(object sender, MapMouseEventArgs e)
{
Pushpin pushpin = new Pushpin(); //初始化一个图钉
pushpin.Location = myMap.ViewportPointToLocation(e.ViewportPoint); //设置图钉的位置
myMap.Children.Add(pushpin);
}
[/code]

说明一下:ViewportPointToLocation()方法,即将屏幕像素转换成带经纬度的位置。关于ViewportPointToLocation()方法的定义及用法参见[url="http://msdn.microsoft.com/en-us/library/microsoft.maps.mapcontrol.map_events.aspx"]MSDN[/url]。

除了添加图钉外,我们还可以自定义添加图形、图片、视频等在地图上,要实现添加图形、图片或视频等数据到地图上,需要使用Bing Maps为我们提供的地图图层(MapLayer)来实现:

[code]
<m:Map ZoomLevel="14" Name="myMap" Center="39.9,116.4" CredentialsProvider="Your Bing Map Key">
<m:Map.Mode>
<m:AerialMode Labels="True" FadingLabels="True"/>
</m:Map.Mode>
<m:MapLayer x:Name="myMapLayer"></m:MapLayer>
</m:Map>
[/code]

如上在地图中加入了一空白地图图层,接下来就可以使用程序动态在地图图层上添加自己想加的东西了:

比如添加名胜风景的图片
[code]
Location location = new Location(latitude, longitude);
Image image = new Image();
image.Source = new BitmapImage(new Uri("http://localhost:2986/Images/China.jpg", UriKind.RelativeOrAbsolute));
image.Stretch = Stretch.None;
image.ImageFailed += delegate(object senders, ExceptionRoutedEventArgs ex){};
PositionOrigin position = new PositionOrigin(1.0, 1.0);
this.myMapLayer.AddChild(image, location, position);
[/code]

比如绘制线条或者多边形

[code]
MapPolygon polygon = new MapPolygon();
polygon.Fill = new System.Windows.Media.SolidColorBrush(System.Windows.Media.Colors.Red);
polygon.StrokeThickness = 5;
polygon.Opacity = 0.7;
polygon.Locations = new LocationCollection() {
new Location(34.9294740237661,107.506492025863),
new Location(37.7814222409819, 105.979148275863),
new Location(40.2865067209496, 109.219382650863) };
this.myMapLayer.Children.Add(polygon);
[/code]

比如添加视频
[code]
MediaElement video = new MediaElement();
//Define the URI location of the video
video.Source = new Uri(@"http://mschnlnine.vo.llnwd.net/d1/ch9/9/2/9/9/1/4/TCS2NBCOlympics_ch9.wmv",
UriKind.RelativeOrAbsolute);
//Define the video display properties
video.Opacity = 0.8;
video.Width = 250;
video.Height = 200;

//The map location to place the video at
Location location = new Location() { Latitude=-45, Longitude=122 };
//Center the video around the location specified
PositionOrigin position = PositionOrigin.Center;

//Add the video to the defined map layer
this.myMapLayer.AddChild(video, location, position);
[/code]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值