MapXtreme2005 Web页面上用鼠标中间键控制地图缩放

 

一、在页面的</form>之前添加如下js代码:
<script type="text/javascript">
        var Img = document.getElementById("MapControl1_Image");
        if(Img != null)
        {
            Img.attachEvent('onmousewheel', bbb);
        }
       
        function bbb()
        {
            var mapImage = document.getElementById("MapControl1_Image");
            var url = "MapController.ashx?Command=WheelZoom&Width=" + mapImage.width +"&Height=" + mapImage.height +"&ExportFormat=" + mapImage.exportFormat + "&Ran=" + Math.random() + "&wheelvalue=" + event.wheelDelta;
              if (mapImage.mapAlias)
                     url +=  "&MapAlias=" + mapImage.mapAlias;   
              var xmlHttp = CreateXMLHttp();
              xmlHttp.open("GET", url, false);
              xmlHttp.send(null);
              try {
            mapImage.src = url;
              } catch(e) { alert("ll"); }
         }
    </script>           
 

二、在后端代码添加如下类:

 

 

[Serializable]
public class WheelZoom : MapBaseCommand
{
    public WheelZoom()
    {
        Name = "WheelZoom";
    }

    public override void Process()
    {
        int wheelvalue = int.Parse(System.Convert.ToString(HttpContext.Current.Request["wheelvalue"]));
        MapControlModel model = MapControlModel.GetModelFromSession();
        model.SetMapSize(MapAlias, MapWidth, MapHeight);
        try
        {
            MapInfo.Mapping.Map myMap = model.GetMapObj("map1");
            MapInfo.Geometry.Distance d = new MapInfo.Geometry.Distance(myMap.Zoom.Value * 0.1, myMap.Zoom.Unit);
            if(wheelvalue > 0)
                d = new MapInfo.Geometry.Distance(myMap.Zoom.Value -900, myMap.Zoom.Unit);
            else
                d = new MapInfo.Geometry.Distance(myMap.Zoom.Value + 900, myMap.Zoom.Unit);
            myMap.Zoom = d;
        }
        finally
        {
            System.IO.MemoryStream ms = model.GetMap(MapAlias, MapWidth, MapHeight, ExportFormat);
            StreamImageToClient(ms);
        }
    }
}
 

 
三、在页面加载处注册command:

 

      

if(!IsPostBack)
 {
            MapControlModel model = MapControlModel.SetDefaultModelInSession();
            model.Commands.Add(new WheelZoom());
 }
    完毕,运行在页面地图上滚动鼠标中间键即可看到效果。

 

 

posted on 2010-04-29 16:57  carekee  阅读(396)  评论(0)    收藏  举报