ArcGIS API for Silverlight 点沿着线流动

概述

前段时间做了一个项目,要求是有一些电力输送线,电力输送线或者石油管道都是有流动方向的,用户想做一个动态效果来模拟电力的输送。其实做简单了只要在线上标识个箭头就可以了。但也要是做成动态的,至少ArcEngine实现起来是有点麻烦的。但ArcGIS API for Silverlight可以解决这个问题。

实现思路

在地图上展示输送电力的线和模拟电力输送方向的电都是ArcGIS  API中定义的对象,否者这些数据在地图上就不好展示了。那么怎么让点沿着线动起来呢?要使用Timer吗?然后没间隔0.1秒就算一下点应该在的位置?这是最原始的办法,相信在比较早期的时候,我们使用ArcEngine做跟踪会回放的时候都采用这种方式。但现在我们用的API是基于Silverlight的,Silverlight自身是有动画模块的,我们可以借助Silverlight的动画来实现。

Silverlight动画针对的对象都是Silverligt中定义的点、线或其他对象,这是我们还需要转换,需要在动画播放的时候,把获取的点转换成ArcGIS API中对象能识别的点。

能解决以上问题,解决方案就差不多了。

实现

首先,我们要定义动画,Silverlight中定义的动画类型有很多种,我们使用PointAnimationUsingPath类,通过其名称也能看到该类的作用。基于线的点动画。

通过查看该类的定义,我们知道使用该类时需要定义好一段路径,设置走完这段路经需要多长时间以及是不是循环播放。

下面的代码我们就定义路径,我们是先知道的ArcGIS中的线对象,再转换成动画中的路径对象,代码如下:

 Point myStartPoint=new Point (this._ELine.StartPoint.Point.X,this._ELine.StartPoint.Point.Y);
 List<PathSegment> myPathSegmentList=new List<PathSegment> ();
 foreach(ESRI.ArcGIS.Client.Geometry.PointCollection myPointCollection in this._ELine.Line.Paths)
 {
    List<Point> myPointList=new List<Point> ();
    foreach(ESRI.ArcGIS.Client.Geometry.MapPoint myMapPoint in myPointCollection)
    {
       myPointList.Add (new Point (myMapPoint.X,myMapPoint.Y));
     }
     PolyLineSegment myPolyLineSegment=new PolyLineSegment(myPointList,false);
     myPathSegmentList.Add (myPolyLineSegment);
 }
定义还是挺麻烦的,但代码一看就明白,下面我们就动画对象

PointAnimationUsingPath myPointAnimationUsingPath = new PointAnimationUsingPath();
List<PathFigure> myPathFigureList=new List<PathFigure> ();
myPathFigureList.Add(new PathFigure (myStartPoint,myPathSegmentList,false));
myPointAnimationUsingPath.PathGeometry = new PathGeometry(myPathFigureList);//设置路径
myPointAnimationUsingPath.Duration = new Duration(TimeSpan.FromSeconds(5));//设置走完路径需要的时间
myPointAnimationUsingPath.RepeatBehavior = RepeatBehavior.Forever;//循环播放

下面我们就定义要运动的点和画板:

this._ArrowGraphic = new ArrowGraphic();//定义我们要运动的点对象
this._ArrowGraphic.SetZIndex((int)GraphicZIndex.ELineArrow);//显示在最上面
this._ArrowGraphic.Geometry = new ESRI.ArcGIS.Client.Geometry.MapPoint(0, 0);//设置起始位置
this._application.GraphicsLayer.Graphics.Add(this._ArrowGraphic);//添加到地图上

this._Storyboard = new Storyboard();//定义画板
this._Storyboard.Children.Add(myPointAnimationUsingPath);//把动画加到画板上
Storyboard.SetTarget(this._Storyboard, this._ArrowGraphic);//关联画板和要运行的对象
Storyboard.SetTargetProperty(myPointAnimationUsingPath, new PropertyPath(ArrowGraphic.PointProperty));//关联动画对象和运动点的一个属性

this._Storyboard.Begin();//启动动画
通过查看PointAnimationUsingPath类的API我们可以知道,该动画的输出是一个System.Windows.Point类型的对象,我们必须要把这个对象转换成ArcGIS API的对象能识别的几何体才可以,所以我们就自定义了上面实例化的ArrowGraphic对象,ArrowGraphic是继承ArcGIS API 中的Graphic类的一个对象。定义如下:

public class ArrowGraphic:Graphic
{
    public Point Point
    {
        get { return (Point)GetValue(PointProperty); }
        set { SetValue(PointProperty, value); }
    }

    public static readonly DependencyProperty PointProperty = DependencyProperty.Register
        ("Point", typeof(Point), typeof(ArrowGraphic), new FrameworkPropertyMetadata(OnPointChanged));
    public static void OnPointChanged(DependencyObject d, DependencyPropertyChangedEventArgs e)
    {
        ArrowGraphic myArrowGraphic = d as ArrowGraphic;
        MapPoint myMapPoint = new MapPoint(myArrowGraphic.Point.X,myArrowGraphic.Point.Y);
        myArrowGraphic.Geometry = myMapPoint;
    }

    public ArrowGraphic()
    {
        SimpleMarkerSymbol myMarkerSymbol = new SimpleMarkerSymbol();
        myMarkerSymbol.Size = 10;
        myMarkerSymbol.Style = SimpleMarkerSymbol.SimpleMarkerStyle.Circle;
        myMarkerSymbol.Color = System.Windows.Media.Brushes.Green;
        this.Symbol = myMarkerSymbol;
    }
}
该对象的作用很简单,就是通过PointProperty把Silverlight的Point转换成ArcGIS API中的Mappoint,然后赋值给该Graphic,这样随着动画的播放,Graphic的Geometry就会不断发生变化,这样Graphic就可以沿着线移动了。

至于我们自定义的Graphic如何显示,是点、图片还是其他就需要自己定义了。

例如我们想显示成箭头,箭头是有方向的,怎么控制箭头的方向呢?那就获取到一个点后,当PointProperty获取到一个新值后,就可以和上一个值进行角度计算,然后再给Geometry赋值就可以了。

下面是展示效果:

图片中的线上的几个绿色的点是一直沿着线动的,实在不会做jpg动画,只能贴一张静态图片了。




  • 5
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 2
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值