Openlayers 2 取消鼠标缩放地图的功能
需要实现的功能:
取消鼠标缩放地图,即滚动鼠标的滚轮地图没有响应事件,只能用鼠标平移地图
版本:OpenLayers 2.13.1
测试代码直接用官方例子http://dev.openlayers.org/releases/OpenLayers-2.13.1/examples/osm.html
直接上代码
原始代码:
var map, layer; function init(){ map = new OpenLayers.Map( 'map'); layer = new OpenLayers.Layer.OSM( "Simple OSM Map"); map.addLayer(layer); map.setCenter( new OpenLayers.LonLat(-71.147, 42.472).transform( new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject() ), 12 ); }
修改后的代码:
var map, layer; function init(){ map = new OpenLayers.Map('map',{ controls: [ new OpenLayers.Control.Navigation({ 'zoomWheelEnabled': false }), new OpenLayers.Control.MousePosition(), new OpenLayers.Control.Zoom() ] } ); layer = new OpenLayers.Layer.OSM("Simple OSM Map"); map.addLayer(layer); map.setCenter( new OpenLayers.LonLat(-71.147, 42.472).transform( new OpenLayers.Projection("EPSG:4326"), map.getProjectionObject() ), 12 ); }
可见在map实例化时,将默认的controls修改一下即可
增加部分代码为:
controls: [ new OpenLayers.Control.Navigation({ 'zoomWheelEnabled': false }), new OpenLayers.Control.MousePosition(), new OpenLayers.Control.Zoom() ]