天天看點

在使用者控件中使用 MapControl 的正确方法 mapxtreme載入地圖

private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此處放置使用者代碼以初始化頁面
            if(!Page.IsPostBack)
            {
                //載入MapControl
                string strRootPath=Server.MapPath("./ZH") + "\ZH.mws";
                MapControl_PL.Map.Clear();

                MapInfo.Mapping.MapWorkSpaceLoader wl = new  
MapInfo.Mapping.MapWorkSpaceLoader(strRootPath);

                    wl.Load(MapControl_PL.Map);

                    Load_ALL();

            }
        }      

MapControl 在運作時的生命周期如下所示:

1. 在構造函數中,在建立所有預設工具的同時建立工具集合

2. 在 OnInit 中執行下列任務:

a. 嘗試使用 Map 對象指向的 MapAlias 從 Session 中擷取 Map 對象。如果該對象

不存在,則建立一個空地圖。使用 Map 對象還可建立用于導出的 MapExport

對象。

b. 如果是第一次通路此頁面并且 UseDesigntimeWorkspace 标記為 True,則将工作

空間加載到地圖中。

3. 在 OnLoad 中,顯示用戶端上需要的所有資訊,例如 javascript 和工具資訊。

4. 在 RenderContents 中,渲染儲存 MapControl 的标記和表示圖像的 IMG 标記。工具

使用的其它資訊用于用戶端操作。LayerControl 以 javascript 變量的形式渲染。

在 Web 窗體的 Page_Load 方法運作時,可以從預設方法偏離并使用自己的 Map 對象。但

是,建立地圖的别名必須與 MapControl 的 MapAlias 比對。在将另一個工作空間加載到地

圖時,請注意該地圖的别名更改為工作空間中的别名

private void Page_Load(object sender, System.EventArgs e)
        {
            // 在此處放置使用者代碼以初始化頁面
            this.ListBox_Result.Attributes.Add("ondblclick"," ShowItem(0);");
            this.ListBoxComLineCopy.Attributes.Add("ondblclick"," ShowItem(1);");
            this.ListBoxPeoLineCopy.Attributes.Add("ondblclick"," ShowItem(3);");
            this.ListBox_PreResult.Attributes.Add("ondblclick","ShowItem(2)");
            if(!Page.IsPostBack)
            {
                //載入MapControl
                string strRootPath=Server.MapPath("./ZH") + "\ZH.mws";
                MapControl_PL.Map.Clear();
                MapInfo.Engine.Session.Current.MapFactory.Remove(MapControl_PL.MapAlias);
                MapInfo.Engine.Session.Current.MapFactory.CreateEmptyMap(MapControl_PL.MapAlias, MapControl_PL.MapAlias,new Size(200,200));
                MapInfo.Mapping.MapWorkSpaceLoader wl = new MapInfo.Mapping.MapWorkSpaceLoader(strRootPath);
                MapInfo.Mapping.Map map=MapInfo.Engine.Session.Current.MapFactory[MapControl_PL.MapAlias];
                if(map!=null)
                {
                    wl.Load(MapControl_PL.Map);
                    wl.Load(map);
                    MapControl_PL.Map=map;
                    Load_ALL();
                }
            }
        }      

官方的代碼 VB的:

private void Page_Load(object sender, System.EventArgs e)
{
if (!Page.IsPostBack) {
// Remove the map from session; we want to use ours
MapInfo.Engine.Session.Current.MapFactory.Remove
(MapControl1.MapAlias);
// Create a empty map with same alias as MapControl
MapInfo.Engine.Session.Current.MapFactory.CreateEmptyMap
(MapControl1.MapAlias, MapControl1.MapAlias,
new Size(200,200));
// Load the map with table.
MapInfo.Mapping.MapLoader ml =
MapInfo.Mapping.MapLoader.CreateFromFile(@"World.tab");
// Get the map object out of session we just created
MapInfo.Mapping.Map map =
MapInfo.Engine.Session.Current.MapFactory
[MapControl1.MapAlias];
if (map != null) {
ml.Load(map);
MapControl1.Map = map;
}
}
}