在MapXtreme 2005中,在Windows应用程序中自带鼠标中键缩放的功能,而有Web应用程序中却没有,如果能够实现会显得更加人性化。在百度里面一搜,还真有位高人实现了,于是借鉴了一下,作了一些修改,在这里和大家共享一下,你可能通过修改程序代码,控制每次缩放的比例,也可以选择等差或等比的方式进行缩放,代码比较简单,只要稍作修改就可以实现。
(1)在页面的</form>之前添加如下JavaScript代码:
<
script type
=
"
text/javascript
"
>
var Img = document.getElementById( " MapControl1_Image " );
if (Img != null )
{
Img.attachEvent('onmousewheel', GetMouseWheelEvent);
}
function GetMouseWheelEvent()
{
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;
try
{
mapImage.src = url;
}
catch(e)
{
alert("Error!");
}
}
< / script>
var Img = document.getElementById( " MapControl1_Image " );
if (Img != null )
{
Img.attachEvent('onmousewheel', GetMouseWheelEvent);
}
function GetMouseWheelEvent()
{
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;
try
{
mapImage.src = url;
}
catch(e)
{
alert("Error!");
}
}
< / script>
(2)在后台代码中,如自定义命令的文件CustomerCommands.cs中的添加如下类:
[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 map = model.GetMapObj(MapAlias);
MapInfo.Geometry.Distance d;
if (wheelvalue > 0)
{
d = new MapInfo.Geometry.Distance(map.Zoom.Value * 0.5, map.Zoom.Unit);
}
else
{
d = new MapInfo.Geometry.Distance(map.Zoom.Value * 2, map.Zoom.Unit);
}
map.Zoom = d;
}
finally
{
System.IO.MemoryStream ms = model.GetMap(MapAlias, MapWidth, MapHeight, ExportFormat);
StreamImageToClient(ms);
}
}
}
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 map = model.GetMapObj(MapAlias);
MapInfo.Geometry.Distance d;
if (wheelvalue > 0)
{
d = new MapInfo.Geometry.Distance(map.Zoom.Value * 0.5, map.Zoom.Unit);
}
else
{
d = new MapInfo.Geometry.Distance(map.Zoom.Value * 2, map.Zoom.Unit);
}
map.Zoom = d;
}
finally
{
System.IO.MemoryStream ms = model.GetMap(MapAlias, MapWidth, MapHeight, ExportFormat);
StreamImageToClient(ms);
}
}
}
(3)在页面加载处注册Command:
if
(Session.IsNewSession)
{
MapControlModel model = MapControlModel.SetDefaultModelInSession();
model.Commands.Add(new WheelZoom());
}
{
MapControlModel model = MapControlModel.SetDefaultModelInSession();
model.Commands.Add(new WheelZoom());
}
完毕,运行在页面地图上滚动鼠标中间键即可看到效果。