Windows Phone 7:在Bing Map控件中加入简单的形状和层

添加形状

演示在Bing Map控件中点击(或者触控后)来添加线条。

image

 

Map控件中所有支持的形状均继承自MapShapeBase类型,目前有两个形状MapPolyline和MapPolygon。

image

 

为了能够在屏幕上添加点,需要将屏幕上的点坐标转换成Map控件中的GeoCoordinate,然后添加到MapShapeBase的Location属性中,注意这个Location属性需要自己初始化,它继承自ObservableCollection<GeoCoordinate>类型。

 

具体代码,首先在Page背后代码中添加字段:

MapPolyline polyline;

 

接着在Page加载事件中初始化MapPolyline并将其添加到Map控件中:

private void PhoneApplicationPage_Loaded(object sender, RoutedEventArgs e)

{

    polyline = new MapPolyline();

    //设置线条外表

    polyline.Stroke = new SolidColorBrush(Colors.Blue);

    polyline.StrokeThickness = 3;

    polyline.Locations = new LocationCollection();

    //将线条添加到Map控件中

    map.Children.Add(polyline);

}

 

接着在Map事件中,将点加入到MapPolyline中就可以了:

private void Map_MouseLeftButtonUp(object sender, MouseButtonEventArgs e)

{

    //获取屏幕点

    var point = e.GetPosition((UIElement)sender);

    //转换成GeoCoordinate

    var geoPoint = map.ViewportPointToLocation(point);

    //添加

    polyline.Locations.Add(geoPoint);

}

 

添加层

我们还可以通过添加层来添加任何控件(UIElement),MapLayer代表着一个层,通过它可以根据地理位置来添加子控件。将MapLayer添加到Map的方式和MapShapeBase相似,都是通过Map.Children属性。MapLayer内具体控件的位置可以通过MapLayer的附加属性:Position、PositionOrigin和PositionOffset,以及PositionRectangle。

 

当然MapLayer的构造函数的不同重载会设置相应的属性,比如用GeoCoordinate(通过Map.BoundingRectangle.Center):

var uiElement = new Button()

{

    Content = "Mgen",

    Background = new SolidColorBrush(Colors.Gray)

};

 

var layer = new MapLayer();

layer.AddChild(uiElement, map.BoundingRectangle.Center);

 

map.Children.Add(layer);

 

结果会是:

image 

如果用LocationRect对象(直接通过Map.BoundingRectangle属性):

layer.AddChild(uiElement, map.BoundingRectangle);

 

按钮会铺满整个Map控件:

image

 

比如设置一下偏移量:

layer.AddChild(uiElement, map.BoundingRectangle);

MapLayer.SetPositionOffset(layer, new Point(5050));

 

结果会是:

image

作者:Mgen 

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值