天天看點

使用WebTool工具

本次使用WEbTool自定義工具,實作一個簡單的功能

效果描述:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 在激發自定義工具後,在地圖界面點選時,彈出該點的用戶端坐标,與相應的經緯度坐标

一:先從用戶端處理怎麼發送請求

1:拖webtool工具到界面

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 将工具WebTool 拖到MapForm.aspx界面上,設定對應的地圖如下: 

< cc1:WebTool  ID ="WebTool1"  runat ="server"  MapControlID ="MapControl1"     />

說明:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 用戶端有三個重要的腳本:在項目MapXtremeWebResources檔案夾下

1.Interaction.js---互動類型腳本

(已實作的有:ClickInteraction(單擊)、RectInteraction(畫矩形)..等等,基本是和已有工具對應的)

2.Command.js----指令發送請求腳本

(已實作的有:MapCommand(擷取地圖)、PanCommand(拖動地圖)、DistanceCommand(測量)..等等,基本是和已有工具對應的)

3.Tool.js----------工具指令狀态激活腳本

(在激活不同的工具時,改變指令的請求參數)

[現在開始處理自定義的用戶端腳本請求]

2:設定工具屬性事件為點選

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 互動:由于要實作的功能,互動類型也是通過點選引發的,互動就直接使用已有的ClickInteraction

是以設定工具屬性:ClientInteraction="ClickInteraction"

3:設定工具指令[使用測量工具的指令功能]

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 指令:由于要實作的功能,用戶端類型在接收服務端傳回的消息後,直接彈出,這點和測量功能是一樣的,于是指令就直接使用DistanceCommand

是以設定工具屬性:ClientCommand="DistanceCommand"

4:自定義指令關鍵字

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 再設定一個自定義指令文本:Command="GetXY" --這個GetXY是随便起的名字,等一下對應服務端的請求指令

OK,至此,用戶端處理完成,實際我們什麼也沒做,隻是把工具往界面一拖,然後設定了一下屬性:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> < cc1:WebTool  ID ="WebTool1"  runat ="server"  MapControlID ="MapControl1"   ClientInteraction ="ClickInteraction"  Command ="GetXY"  ClientCommand ="DistanceCommand"   />

二:服務端接收請求并輸出資訊

1:建立自定義指令類:WebInfoGetXY

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 在App_Code裡,我們建立一個類,叫WebInfoGetXY,讓它繼承自MapInfo.WebControls.MapBaseCommand

同時加上可序列化屬性[Serializable],不加就報錯了,配置檔案那節裡有說到

在構造函數裡,寫base.Name = "GetXY";//這個就對應了用戶端發送的Command

代碼如下:

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> [Serializable]

public   class  WebInfoGetXY:MapInfo.WebControls.MapBaseCommand

{

     public  WebInfoGetXY()

    {

           base .Name  =   " GetXY " ;

    }

}

然後,重寫一下Process()方法輸出文本即可,代碼如下

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->      public   override   void  Process()

    {

        MapControlModel model  =  MapControlModel.GetModelFromSession();

        MapInfo.Mapping.Map map  =  model.GetMapObj(MapAlias);

        System.Drawing.Point[] points  =  ExtractPoints(DataString);

        MapInfo.Geometry.DPoint dpoint  =   new  MapInfo.Geometry.DPoint();

        map.DisplayTransform.FromDisplay(points[ 0 ],  out  dpoint); // 螢幕xy轉經緯度

         string  outText = " 螢幕xy: "   +  points[ 0 ].X  +   " , "   +  points[ 0 ].Y;

        outText +=   " <br>經緯度xy: "   +  dpoint.x  + " , " +  dpoint.y;

        HttpContext.Current.Response.Write(outText);

    }

OK,服務端到此也就結束了

三:在頁面上用代碼注冊工具指令

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

-->        if (Session.IsNewSession)

        {

            MapInfo.WebControls.MapControlModel model = MapInfo.WebControls.MapControlModel.SetDefaultModelInSession();

            model.Commands.Add(new WebInfoGetXY());

        }

四:運作效果

<!--

Code highlighting produced by Actipro CodeHighlighter (freeware)

http://www.CodeHighlighter.com/

--> 在激發自定義工具後,在地圖界面點選時,彈出該點的用戶端坐标,與相應的經緯度坐标

繼續閱讀