MapXtreme 地圖搜索之圖元定位

 

js中的代碼:

function analysis_map_search_result()
{

var mapImage = document.getElementById("MapControl1_Image_Image");
      
var url = "MapController.ashx?Command=map_search&Width=" + mapImage.width +"&Height=" + mapImage.height + "&ExportFormat=" + mapImage.exportFormat+"&Ran=" + Math.random()+"&feature_name="+encodeURI(jsTrim($("Text1").value))+"&feature_kind="+encodeURI($("Select1").value);

if (mapImage.mapAlias)

    url += "&MapAlias=" + mapImage.mapAlias;
   
     try
        {
         
         //獲取數據
            var xmlHttp = CreateXMLHttp();
           
         xmlHttp.open("GET", url,false);    //提交搜索命令
        
         xmlHttp.send(null);
        
         var result = xmlHttp.responseText;
        
         if(result=="")
         {
        
            alert("沒查到相關記錄!");
           
            return;
           
         }
            
        }
       
     catch(e)
          {
       
              alert("Error!");
           
          }

var sel=$("Sel_name");

var result=result.split("$");

var leng=result.length;

if(leng==1)

{
alert("沒有查詢到相關記錄!");

return;

}

var len=sel.length;

for(var i=0;i<len-1;i++)

    sel.options.remove(1);
   
for(var i=0;i<result.length;i++)

    if(result[i]!="")

       sel.options.add(new Option(result[i],result[i]));

sel.options[1].selected=true;

var count=sel.length-1;

location_feature(sel.value,$("Select1").value); //定位圖元

if(sel.length>2)                              //只查詢到一條記錄,進行定位

    alert("共查詢到"+count+"相關記錄!");

current_kind=$("Select1").value;                            //最近查詢類別
    
}

 

CustomizedCommands.cs中的map_search命令代碼:

[Serializable]

    public class map_search : MapBaseCommand
    {

        public map_search()
        {

            this.Name = "map_search";

        }

        public override void Process()
        {

            string feature_name = Convert.ToString(HttpContext.Current.Request["feature_name"]);

                                                                                                                        //圖元名稱(模糊)

            string feature_kind = Convert.ToString(HttpContext.Current.Request["feature_kind"]);

                                                                                                                      //圖元種類

            MapControlModel model = MapControlModel.GetModelFromSession();

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

            Catalog cat = MapInfo.Engine.Session.Current.Catalog;

            string result = "";                                             //返回結果:格式爲$**$

            string _findColumnName, _findLayerName;

            _findLayerName = feature_kind;                    //圖層名

            _findColumnName = "Name";                         //列名

            SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere(_findColumnName + " like '%" + feature_name + "%'");

                                                                      

            IResultSetFeatureCollection ifs = MapInfo.Engine.Session.Current.Catalog.Search(_findLayerName, si);

            MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();

            int i = 0;

            foreach (Feature feature in ifs)
            {

                result = result + feature["name"].ToString() + "$";

                i++;


            }


            HttpContext.Current.Response.Output.Write(result);

        }
    }

 

 

[Serializable]

    public class location_feature : MapBaseCommand
    {
        public location_feature()
        {
            Name = "location_feature";

        }

        public override void Process()
        {

            string feature_name = System.Convert.ToString(HttpContext.Current.Request["feature_name"]);

            string findLayerName = System.Convert.ToString( HttpContext.Current.Request["feature_kind"]);

             MapControlModel model = MapControlModel.GetModelFromSession();

            model.SetMapSize(MapAlias, MapWidth, MapHeight);
           
            try
            {     
                  string _findColumnName = "NAME";

                MapInfo.Mapping.Map map = MapInfo.Engine.Session.Current.MapFactory["Map1"];

                SearchInfo si = MapInfo.Data.SearchInfoFactory.SearchWhere(_findColumnName + " like '%" + feature_name + "%'");

                IResultSetFeatureCollection ifs = MapInfo.Engine.Session.Current.Catalog.Search(findLayerName, si);

                MapInfo.Engine.Session.Current.Selections.DefaultSelection.Clear();//去除默認選擇,達到不高亮顯示

                                    if (ifs.Count == 1)
                    {


                        map.Center = new DPoint(ifs[0].Geometry.Centroid.x, ifs[0].Geometry.Centroid.y);

                           //使該圖元位於屏幕中心

                        MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(0.5, map.Zoom.Unit);

                           //設置地圖的比例尺

                        map.Zoom = d;


                    }
                    else
                    {

                        map.SetView(ifs.Envelope);

                    }

                   
   }
            finally
            {
                System.IO.MemoryStream ms = model.GetMap(MapAlias, MapWidth,MapHeight,ExportFormat);

                StreamImageToClient(ms);
            }
   

        }
    }

}

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章