|
阅读:2668回复:1
ArcGIS Server附带例子--回调原理初探
<P>要是想明白无刷新回调的原理,就需要弄明白基本的处理过程,否则看的是一头雾水,简要的说明其原理:页面或者相应的控件要实现ICallbackEventHandler接口并提供RaiseCallbackEvent()方法和<FONT face=Verdana>public string GetCallbackResult()</FONT>方法,只有这样的话才能够实现有调有回,然后在Page_Load中使用<FONT face=Verdana>GetCallbackEventReference()</FONT>架起一座桥梁.参数的意义请参考其他文档.</P>
<P>这里以附带的<FONT face=Verdana>Common_Callback_CSharp</FONT>例子加以说明.所做的内容就是实现两种方法定位地图中心:地名和坐标.首先在客户端实现此代码:</P> <P> <FONT face=Verdana> <script language="javascript"><BR> //根据地名定位地图中心<BR> function ZoomToLocationClient(val)<BR> {<BR> var message = 'zoomtolocation';<BR> message += ',' + val;<BR> var context = 'Map1'; <BR> //把服务端的生成的脚本段sCallBackFunctionInvocation输出 <BR> //输出内容:WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true) <BR> <%=sCallBackFunctionInvocation%><BR> }<BR> <BR> //根据坐标定位地图中心<BR> function ZoomToPointClient()<BR> {<BR> //获取输入的x值<BR> var x = document.getElementById('TextBoxX').value;<BR> //获取输入的y值<BR> var y = document.getElementById('TextBoxY').value;</FONT></P> <P><FONT face=Verdana> //生成请求字符串<BR> var message = 'zoomtopoint';<BR> message += ',' + x + ',' + y;<BR> var context = 'Map1'; <BR> //把服务端的生成的脚本段sCallBackFunctionInvocation输出<BR> //输出内容:WebForm_DoCallback('__Page',message,processCallbackResult,context,postBackError,true) <BR> <%=sCallBackFunctionInvocation%><BR> } <BR> </script> </FONT></P> <P>相应的客户端代码为:</P> <P><FONT face=Verdana> //根据坐标定位地图中心<BR> public void ZoomToPointServer(string ea)<BR> {<BR> char[] parser_char = { ',' };<BR> string[] messages = ea.Split(parser_char);<BR> double map_width_eight = Map1.Extent.Width/8;<BR> double x_center = Double.Parse(messages[1]);<BR> double y_center = Double.Parse(messages[2]);<BR> <BR> // Map control changes extent, recognizes that a new Web ADF tile cache needs to generated by the client<BR> ESRI.ArcGIS.ADF.Web.Geometry.Envelope env = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(x_center - map_width_eight, y_center - map_width_eight, x_center + map_width_eight, y_center + map_width_eight);<BR> Map1.Extent = env;<BR> <BR> // The callback string generated by the Map control is returned to the client.<BR> mapstring = Map1.CallbackResults.ToString();</FONT></P> <P><FONT face=Verdana> }</FONT></P> <P><FONT face=Verdana> //根据名称定位地图中心<BR> public void ZoomToLocationServer(string ea)<BR> {<BR> //请求字符串分割处理<BR> char[] parser_char = { ',' };<BR> string[] messages = ea.Split(parser_char);<BR> string location = messages[1];<BR> double minx = 0;<BR> double miny = 0;<BR> double maxx = 0;<BR> double maxy = 0;</FONT></P> <P><FONT face=Verdana> bool validlocation = false;</FONT></P> <P><FONT face=Verdana> //根据地点名称不同定位到不同的坐标<BR> //这里为了方便直接给min和max赋值,在实际的开发中可以更加地点名称进行坐标的查询然后更加查询的坐标进行定位<BR> switch (location)<BR> {<BR> case "Asia":<BR> minx = -125.0;<BR> miny = 30.0;<BR> maxx = -110.0;<BR> maxy = 45.0;<BR> validlocation = true;<BR> break;<BR> case "Africa":<BR> minx = -80.0;<BR> miny = 40.0;<BR> maxx = -70.0;<BR> maxy = 45.0;<BR> validlocation = true;<BR> break;<BR> default:<BR> break;<BR> }</FONT></P> <P><FONT face=Verdana> if (validlocation)<BR> {<BR> ESRI.ArcGIS.ADF.Web.Geometry.Envelope new_extent = new ESRI.ArcGIS.ADF.Web.Geometry.Envelope(minx, miny, maxx, maxy);</FONT></P> <P><FONT face=Verdana> //设置当前地图的Extent<BR> Map1.Extent = new_extent;</FONT></P> <P><FONT face=Verdana> //把CallbackResults返回给客户端,让客户端更新地图显示<BR> mapstring = Map1.CallbackResults.ToString();<BR> } else {<BR> mapstring = "";<BR> }<BR> }</FONT></P> <P>然后客户端通过<FONT face=Verdana> </FONT></P> <P><FONT face=Verdana><asp:MenuItem Text="Zoom To" Value="Zoom To"><BR> <asp:MenuItem Text="Asia" Value="Asia" NavigateUrl="javascript: ZoomToLocationClient('Asia');"></asp:MenuItem><BR> <asp:MenuItem Text="Africa" Value="Africa" NavigateUrl="javascript: ZoomToLocationClient('Africa');"></asp:MenuItem><BR> </asp:MenuItem>向服务器注册请求.</FONT></P> <P>服务器通过在Page_Load中实现下面代码响应请求并向提供RaiseCallbackEvent()参数:</P> <P><FONT face=Verdana> sCallBackFunctionInvocation = Page.ClientScript.GetCallbackEventReference(this, "message", "processCallbackResult", "context", "postBackError", true);</FONT></P> <P>RaiseCallbackEvent()获得参数后继续交给GetCallbackEventReference()由其处理最终的结果,最后由GetCallbackResult()回调给客户端处理的结果.</P> <P>RaiseCallbackEvent()代码:</P> <P><FONT face=Verdana> public void RaiseCallbackEvent(string eventArgs)<BR> {<BR> //根据坐标定位地图中心<BR> if (eventArgs.Contains("zoomtopoint"))<BR> {<BR> ZoomToPointServer(eventArgs);<BR> }<BR> //根据名称定位地图中心<BR> else if (eventArgs.Contains("zoomtolocation"))<BR> {<BR> ZoomToLocationServer(eventArgs);<BR> }<BR> }</FONT></P> <P>GetCallbackResult()代码:</P> <P><FONT face=Verdana> public string GetCallbackResult()<BR> { <BR> return mapstring;<BR> }</FONT></P> |
|
|
|
1楼#
发布于:2008-10-19 01:06
学习了,<img src="images/post/smile/dvbbs/em02.gif" />
|
|