MapInfo MapXtreme 2005 WebGIS 简单鹰眼设计(转)
Posted on 2010-04-30 13:09 Happy Coding 阅读(774) 评论(0) 编辑 收藏 举报original link:
http://www.cnitblog.com/eastperson/archive/2006/10/17/18055.aspx
origin full text:
我在2005上研究了好长时间, 才弄出来个简单的鹰眼,与大家分享,我的设计思路是将后台设置两个map,map1和map2,map1为主地图,map2为鹰眼地图,但是map2没有MapControl,定义一个实现类继承于MapBaseCommand,将map1.Bounds的矩形在map2上转换为System.Drawing.Rectangle,之后将这个Rectangle的左上坐标和width,height传到客户端,应用JS进行客户端绘图,在客户端加入一个Div,Div里放置一个IMG,如下为部分代码:
自定义类:
1using System;
2using System.Collections;
3using System.Drawing;
4using System.IO;
5using System.Web;
6using System.Web.UI.WebControls;
7using System.Web.UI;
8using MapInfo.Mapping;
9using MapInfo.Data;
10using MapInfo.WebControls;
11
12
13namespace CustomWebTools
14{
15 /// <summary>
16 /// Info command for InfoWebTool.
17 /// </summary>
18 [Serializable]
19 public class Info : MapInfo.WebControls.MapBaseCommand
20 {
21 /// <summary>
22 /// Key to be used to get the pixel tolerance parameter value from the URL.
23 /// </summary>
24 protected const string PixelToleranceKey = "PixelTolerance";
25 protected const string InfoCommand = "Info";
26
27
28 /// <summary>
29 /// Constructor for Info class
30 /// </summary>
31 public Info()
32 {
33 Name = InfoCommand;
34 }
35
36 /// <summary>
37 /// Override the Execute method in MapBasicCommand class to not save state, because
38 /// for info tool, which does not change map state, so there is no need to save map state.
39 /// </summary>
40 public override void Execute()
41 {
42
43 StateManager sm = StateManager.GetStateManagerFromSession();
44 if (sm == null)
45 {
46 if(StateManager.IsManualState())
47 {
48 throw new NullReferenceException("Cannot find instance of StateManager in the ASP.NET session.");
49 }
50 }
51 ParseContext();
52 if(sm != null)
53 {
54 PrepareStateManagerParamsDictionary(sm);
55 sm.RestoreState();
56 }
57
58 Process();
59 }
60
61 /// <summary>
62 /// method to do the real server side process for info tool.
63 /// </summary>
64 public override void Process()
65 {
66 //get pixel tolerance from url of client side.
67 int pixelTolerance = System.Convert.ToInt32(HttpContext.Current.Request[PixelToleranceKey]);
68
69 MapControlModel model = MapControlModel.GetModelFromSession();
70 model.SetMapSize(MapAlias, MapWidth, MapHeight);
71
72 //extract points from url of client side.
73 System.Drawing.Point[] points = ExtractPoints(DataString);
74
75 //do searching and get results back
76 MultiResultSetFeatureCollection mrfc = RetrieveInfo(points, pixelTolerance);
77
78 IEnumerator resultEnum = mrfc.GetEnumerator();
79
80 //retrieve the selected feature from collection
81 while(resultEnum.MoveNext())
82 {
83 IResultSetFeatureCollection irfc = (IResultSetFeatureCollection)resultEnum.Current;
84 IFeatureEnumerator ftrEnum = irfc.GetFeatureEnumerator();
85
86 while(ftrEnum.MoveNext())
87 {
88 Feature ftr = (Feature)ftrEnum.Current;
89 //create a html table to display feature info and stream back to client side.
90 CreateInfoTable(ftr);
91 irfc.Close();
92 mrfc.Clear();
93 break;
94 }
95 break;
96 }
97 }
98
99 /// <summary>
100 /// Creates html table to hold passed in feature info, and stream back to client.
101 /// </summary>
102 /// <param name="ftr">feature object</param>
103 private void CreateInfoTable(Feature ftr)
104 {
105 //create a table control and populat it with the column name/value(s) from the feature returned and
106 // and the name of the layer where the feature belong
107 System.Web.UI.WebControls.Table infoTable = new System.Web.UI.WebControls.Table();
108 //set table attribute/styles
109 infoTable.CellPadding = 4;
110 infoTable.Font.Name = "Arial";
111 infoTable.Font.Size = new FontUnit(8);
112 infoTable.BorderWidth = 1;
113 //infoTable.BorderStyle = BorderStyle.Outset;
114
115 System.Drawing.Color backColor = Color.Bisque;
116
117 //add the first row, the layer name/value where the selected feature belongs
118 TableRow r = new TableRow();
119 r.BackColor = backColor;
120
121 TableCell c = new TableCell();
122 c.Font.Bold = true;
123 c.ForeColor = Color.Indigo;
124
125 c.Text = "Layer Name";
126 r.Cells.Add(c);
127
128 c = new TableCell();
129
130 //the feature returned is from a resultset table whose name is got from appending _2
131 //to the real table name, so below is to get the real table name.
132 string alias = ftr.Table.Alias;
133 c.Text = alias.Substring(0, alias.Length-2);
134 c.Font.Bold = true;
135 r.Cells.Add(c);
136
137 infoTable.Rows.Add(r);
138
139 foreach(Column col in ftr.Columns)
140 {
141 String upAlias = col.Alias.ToUpper();
142 //don't display obj, MI_Key or MI_Style columns
143 if(upAlias != "OBJ" && upAlias != "MI_STYLE" && upAlias != "MI_KEY")
144 {
145 r = new TableRow();
146 r.BackColor = backColor;
147
148 r.Cells.Clear();
149 c = new TableCell();
150 c.Text = col.Alias;
151 c.Font.Bold = true;
152 c.ForeColor = Color.RoyalBlue;
153
154 r.Cells.Add(c);
155 c = new TableCell();
156 c.Text = ftr[col.Alias].ToString();
157 r.Cells.Add(c);
158 infoTable.Rows.Add(r);
159 }
160 }
161
162 //stream the html table back to client
163 StringWriter sw = new StringWriter();
164 HtmlTextWriter hw = new HtmlTextWriter(sw);
165 infoTable.RenderControl(hw);
166 String strHTML = sw.ToString();
167 HttpContext.Current.Response.Output.Write(strHTML);
168 }
169
170 /// <summary>
171 /// Get a MultiFeatureCollection containing features in all layers falling into the tolerance of the point.
172 /// </summary>
173 /// <param name="points">points array</param>
174 /// <param name="pixelTolerance">pixel tolerance used when searching</param>
175 /// <returns>Returns a MultiResultSetFeatureCollection object</returns>
176 protected MultiResultSetFeatureCollection RetrieveInfo(Point[] points, int pixelTolerance)
177 {
178 if(points.Length != 1)
179 return null;
180
181 MapControlModel model = MapControlModel.GetModelFromSession();
182 //get map object from map model
183 MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
184
185 if(map == null) return null;
186
187 //creat a layer filter to include normal visible layers for searching
188 IMapLayerFilter layerFilter = MapLayerFilterFactory.FilterForTools(
189 map, MapLayerFilterFactory.FilterByLayerType(LayerType.Normal), MapLayerFilterFactory.FilterVisibleLayers(true),
190 "MapInfo.Tools.MapToolsDefault.SelectLayers", null);
191
192 ITableEnumerator tableEnum = map.Layers.GetTableEnumerator(layerFilter);
193
194 //return if there is no valid layer to search
195 if(tableEnum == null) return null;
196
197 System.Drawing.Point center = points[0];
198
199 //create a SearchInfo with a point and tolerance
200 SearchInfo si = MapInfo.Mapping.SearchInfoFactory.SearchNearest(map, center, pixelTolerance);
201 (si.SearchResultProcessor as ClosestSearchResultProcessor).Options = ClosestSearchOptions.StopAtFirstMatch;
202 //retrieve all columns
203 si.QueryDefinition.Columns = null;
204
205 MapInfo.Geometry.Distance d = MapInfo.Mapping.SearchInfoFactory.ScreenToMapDistance(map, pixelTolerance);
206 (si.SearchResultProcessor as ClosestSearchResultProcessor).DistanceUnit=d.Unit;
207 (si.SearchResultProcessor as ClosestSearchResultProcessor).MaxDistance = d.Value;
208
209
210 //do search
211 MultiResultSetFeatureCollection mrfc = MapInfo.Engine.Session.Current.Catalog.Search(tableEnum, si);
212 return mrfc;
213
214 }
215 }
216
217 /// <summary>
218 /// ZoomValue command to write current zoom value to client for display.
219 /// </summary>
220 [Serializable]
221 public class ZoomValue : MapInfo.WebControls.MapBaseCommand
222 {
223 /// <summary>
224 /// Constructor for ZoomValue class
225 /// </summary>
226 public ZoomValue()
227 {
228 Name = "ZoomValue";
229 }
230
231 /// <summary>
232 /// Override the Execute method in MapBasicCommand class to NOT save state, because
233 /// for this command, which does not change map state, so there is no need to save map state.
234 /// </summary>
235 public override void Execute()
236 {
237
238 StateManager sm = StateManager.GetStateManagerFromSession();
239 if (sm == null)
240 {
241 if(StateManager.IsManualState())
242 {
243 throw new NullReferenceException("Cannot find instance of StateManager in the ASP.NET session.");
244 }
245 }
246 ParseContext();
247 if(sm != null)
248 {
249 PrepareStateManagerParamsDictionary(sm);
250 sm.RestoreState();
251 }
252
253 Process();
254 }
255
256 public override void Process()
257 {
258 MapControlModel model = MapControlModel.GetModelFromSession();
259 //get map object from map model
260 MapInfo.Mapping.Map map = model.GetMapObj(MapAlias);
261 MapInfo.Mapping.Map map2 = model.GetMapObj("Map2");
262 System.Drawing.Rectangle rect;
263 map2.DisplayTransform.ToDisplay(map.Bounds, out rect);
264
265 HttpContext.Current.Response.Output.Write(rect.X);
266 HttpContext.Current.Response.Output.Write(',');
267 HttpContext.Current.Response.Output.Write(rect.Y);
268 HttpContext.Current.Response.Output.Write(',');
269 HttpContext.Current.Response.Output.Write(rect.Width);
270 HttpContext.Current.Response.Output.Write(',');
271 HttpContext.Current.Response.Output.Write(rect.Height);
289 }
290 }
291}
292
客户端JS代码:

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

后台HTML文件代码:

2

3

4

5

6

7

8

9

10

11

12

13

14

15

16

17

18

19

20

21

22

23

24

25

26

27

28

29

30

31

32

33

34

35

36

37

38

39

40

41

42

43

44

45

46

47

48

49

50

51

52

53

54

55

56

57

58

59

60

61

62

63

64

65

66

67

68

http://xiexiaokui.cnblogs.com