Javascript OpenLayers in front of a zoom event listener

I am trying to configure OpenLayers so as not to display the vector layer just before starting the zoom and reappear after the zoom is complete. I have a portion of the end of the zoom already set as follows:

map = new OpenLayers.Map('map_element', { eventListeners: { "zoomend": mapEvent}}); function mapEvent(event) { if(event.type == "zoomend") { hide_vector_layer(); } } 

But I do not see any event listeners to start increasing in the documentation. There is a "movestart" that covers moving, panning, and zooming. Unfortunately, I cannot use "movestart" because I do not want the layer to disappear during panning. You might think that there will be a "zoomstart", since there is a "zoomend".

The reason I'm trying to do this is because I don’t like the way the vector layer scales at a different speed when using Google Maps as the base layer. It doesn’t look right, it seems that all functions are inaccurate, even if they land in the right place after scaling is complete.

Any suggestions?

+8
javascript google-maps zooming openlayers
source share
5 answers

For this purpose, you must override the MoveTo and moveByPx methods for OpenLayers.Map to eliminate the movestart event trigger for any action other than scaling.

+2
source share

It's easy to add the "BeforeZoom" event to OpenLayers here. Just add the code below where you created your map object.

 map.zoomToProxy = map.zoomTo; map.zoomTo = function (zoom,xy){ //Your Before Zoom Actions //If you want zoom to go through call map.zoomToProxy(zoom,xy); //else do nothing and map wont zoom }; 

How it works:

For any scaling activity, the OpenLayers API ultimately calls the zoomTo function. Therefore, before overriding it, we copy this function into a new function called zoomToProxy. We redefine it and add our conditional scaling logic. If we want scaling to happen, we just call the new proxy function :)

+8
source share

I had the same problem as OP, and I tried to solve it with drnextgis solution. But, unfortunately, this did not fully work: the zoomChanged property in OpenLayers.Map.moveTo evaluates to true not only when changing the zoom level, but also when changing the map size.

My map was 100% of the user's browser window, so if they resized the window, the event will be fired. This was undesirable for me, because I only wanted to trigger an event if the zoom level really changed. My solution was to create a new event called "zoomstart", which I inserted at the beginning of OpenLayers.Map.moveTo. Here is the code:

 var getZoom = this.getZoom(); if ( !!getZoom && !!zoom && this.isValidZoomLevel(zoom) && getZoom != zoom ) this.events.triggerEvent("zoomstart", zoom); 

This code will pass a new zoom level to the event listener that is registered on zoomstart , and in my case I define map limitedExtent and do other things based on the new zoom level.

Peace be with you.

+2
source share

movestart handles zoomstart. To determine if there is a zoom effect, try:

  map.events.register("movestart",map, function(e) { if(e.zoomChanged) { //zoom start code here } }); 
+1
source share

The Shaunak solution works very well for me. I want to limit the scaling to below 11, so I edited its code as

 if (zoom > 11) { map.zoomToProxy(zoom, xy); } 
0
source share

All Articles