SuperMap iClient for Silverlight API基础开发之自定义Action画双线

SuperMap.Web 程序集提供了 MapAction 类,其子类有 DrawCircle、DrawLine、DrawPolygon、Pan、ZoomIn、ZoomOut、Edit 等。如果这些 Action 不能满足用户需求,则可以从 MapAction 继承进行扩展,也可以从其子类继承进行扩展。在扩展的类中重写父类的方法就可以了。

今天闲着无聊,正好有人说要画双线,就顺便实现一下,用法和其他的Action都是一样的:

  
    
1 private void test_Click( object sender , RoutedEventArgs e)
2 {
3 DrawMultiLine drawMultiLine = new DrawMultiLine(MyMap , 500000 );
4 drawMultiLine.DrawCompleted += new EventHandler < DrawEventArgs > (drawMultiLine_DrawCompleted);
5 MyMap.Action = drawMultiLine;
6 }
7
8 void drawMultiLine_DrawCompleted( object sender , DrawEventArgs e)
9 {
10 Feature f = new Feature();
11 f.Geometry = e.Geometry;
12 featuresLayer.AddFeature(f);
13 }
这是实现画双线的类:
   
     
1 public class DrawMultiLine : MapAction , IDrawStyle
2 {
3 public event EventHandler < DrawEventArgs > DrawCompleted;
4 private Feature feature;
5 private Point2DCollection points;
6 private Point2DCollection pointsMulti;
7
8 private bool isActivated;
9 private bool isDrawing;
10 private double _Distance;
11
12 public DrawMultiLine(Map map , double distance)
13 : base (map , " DrawMultiLine " , Cursors.Arrow)
14 {
15 if (distance <= 0 )
16 {
17 throw new Exception( " distance " );
18 }
19 _Distance = distance;
20
21 Stroke = new SolidColorBrush(Colors.Red);
22 StrokeThickness = 1 ;
23 Opacity = 1 ;
24 }
25
26 private void Activate(Point2D firstPoint , Point2D secPoint)
27 {
28 DrawLayer = new FeaturesLayer();
29 if (Map.Layers == null )
30 {
31 return ;
32 }
33 Map.Layers.Add(DrawLayer);
34
35 feature = new Feature();
36
37 points = new Point2DCollection();
38 pointsMulti = new Point2DCollection();
39
40 feature.Geometry = new GeoLine
41 {
42 Parts = new ObservableCollection < Point2DCollection >
43 {
44 points ,
45 pointsMulti
46 }
47 };
48
49 points.Add(firstPoint);
50 points.Add(firstPoint);
51
52 pointsMulti.Add(secPoint);
53 pointsMulti.Add(secPoint);
54
55 DrawLayer.Features.Add(feature);
56
57 isDrawing = true ;
58 isActivated = true ;
59 }
60 public override void OnMouseLeftButtonDown(MouseButtonEventArgs e)
61 {
62 Point2D item = Map.ScreenToMap(e.GetPosition(Map));
63 Point2D item2 = new Point2D(item.X + _Distance , item.Y + _Distance);
64 if ( ! isActivated)
65 {
66 this .Activate(item , item2);
67 }
68 else
69 {
70 points.Add(item);
71 pointsMulti.Add(item2);
72 }
73 e.Handled = true ;
74
75 base .OnMouseLeftButtonDown(e);
76 }
77 public override void OnMouseMove(MouseEventArgs e)
78 {
79 if (isDrawing)
80 {
81 Point2D item = Map.ScreenToMap(e.GetPosition(Map));
82
83 points.RemoveAt(points.Count - 1 );
84 points.Add(item);
85
86 pointsMulti.RemoveAt(pointsMulti.Count - 1 );
87 pointsMulti.Add( new Point2D(item.X + _Distance , item.Y + _Distance));
88 }
89 base .OnMouseMove(e);
90 }
91 public override void OnDblClick(MouseButtonEventArgs e)
92 {
93 endDraw();
94 e.Handled = true ;
95 base .OnDblClick(e);
96 }
97 private void endDraw( )
98 {
99 if (points != null && pointsMulti != null )
100 {
101
102 GeoLine geoLine = new GeoLine();
103 geoLine.Parts.Add(points);
104 geoLine.Parts.Add(pointsMulti);
105 DrawEventArgs args = new DrawEventArgs
106 {
107 DrawName = Name ,
108 Element = null ,
109 Geometry = geoLine
110 };
111
112 Deactivate();
113 OnDrawCompleted(args);
114 }
115 }
116 private void OnDrawCompleted(DrawEventArgs args)
117 {
118 if (DrawCompleted != null )
119 {
120 DrawCompleted( this , args);
121 }
122 }
123 public override void Deactivate( )
124 {
125 isActivated = false ;
126 isDrawing = false ;
127 pointsMulti = null ;
128 points = null ;
129 if (DrawLayer != null )
130 {
131 DrawLayer.Features.Clear();
132 }
133 if (Map != null && Map.Layers != null )
134 {
135 Map.Layers.Remove(DrawLayer);
136 }
137 }
138
139 private FeaturesLayer DrawLayer { get ; set ; }
140 public FillRule FillRule { get ; set ; }
141
142 #region IDrawStyle 成员
143
144 public Brush Fill { get ; set ; }
145
146 public Brush Stroke { get ; set ; }
147
148 public double StrokeThickness { get ; set ; }
149
150 public double StrokeMiterLimit { get ; set ; }
151
152 public double StrokeDashOffset { get ; set ; }
153
154 public DoubleCollection StrokeDashArray { get ; set ; }
155
156 public PenLineCap StrokeDashCap { get ; set ; }
157
158 public PenLineCap StrokeEndLineCap { get ; set ; }
159
160 public PenLineCap StrokeStartLineCap { get ; set ; }
161
162 public PenLineJoin StrokeLineJoin { get ; set ; }
163 public double Opacity { get ; set ; }
164
165
166 #endregion
167 }
   
效果如图:
     SuperMap iClient for Silverlight API基础开发之自定义Action画双线
其他画多线也类似了。

你可能感兴趣的:(silverlight)