简体   繁体   中英

Openlayers stop zoomstart event

I work with Openlayers 2.x and I have zoomstart event

  map.events.register('zoomstart', map, function(e) {

            // 1. OpenLayers.Event.stop(event);
            // 2. return ;
            // 3. e.preventDefault();
        }
    });

My way (1,2,3) not working and event does not stop and change zoom level. Can anybody help me?

The zoom event is triggered by the zoomBarUp function in PanZoomBar control, see: http://trac.osgeo.org/openlayers/browser/trunk/openlayers/lib/OpenLayers/Control/PanZoomBar.js and the line

this.map.zoomTo(zoomLevel);

One way to prevent zoom for zoom levels above 13 would be to override this function, which you can do by adding your own version, either in a standalone js file or by using prototype within you OpenLayers init function, ie, after OpenLayers has loaded.

OpenLayers.Control.PanZoomBar.prototype.zoomBarUp = function(evt){

  //copy here the code from the actual function
  if (!OpenLayers.Event.isLeftClick(evt) && evt.type !== "touchend") {
        return;
  }
  //rest of code .....


 //put in your check for zoom level here before calling this.map.zoomTo(zoomLevel);
 if(this.map.zoom<13){
     this.map.zoomTo(zoomLevel);
        this.mouseDragStart = null;
        this.zoomStart = null;
        this.deltaY = 0;
        OpenLayers.Event.stop(evt);
  }};

There may be a more elegant way, but this should work.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM