Skip to content

Commit

Permalink
zoomStart event and close-to-synced zoom for EventPane layers
Browse files Browse the repository at this point in the history
With a zoomStart event we can notify EventPane layers like Google of
a starting zoom. This gives the Google layer a chance to start its built-in
zoom animation at the same time that the OpenLayers map starts its
animiation, resulting in a better sync between Google and OpenLayers zoom
animation.
  • Loading branch information
ahocevar committed Aug 7, 2013
1 parent 128062a commit 58e3450
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 7 deletions.
4 changes: 3 additions & 1 deletion examples/spherical-mercator.html
Original file line number Diff line number Diff line change
Expand Up @@ -54,7 +54,9 @@ <h1 id="title">OpenLayers Spherical Mercator Example</h1>
div: "map",
projection: "EPSG:900913",
displayProjection: "EPSG:4326",
numZoomLevels: 18
numZoomLevels: 18,
// approximately match Google's zoom animation
zoomDuration: 10
});

// create Google Mercator layers
Expand Down
18 changes: 18 additions & 0 deletions lib/OpenLayers/Layer/EventPane.js
Original file line number Diff line number Diff line change
Expand Up @@ -126,6 +126,8 @@ OpenLayers.Layer.EventPane = OpenLayers.Class(OpenLayers.Layer, {
if (this.mapObject == null) {
this.loadWarningMessage();
}

this.map.events.register('zoomstart', this, this.onZoomStart);
},

/**
Expand All @@ -137,11 +139,27 @@ OpenLayers.Layer.EventPane = OpenLayers.Class(OpenLayers.Layer, {
* map - {<OpenLayers.Map>}
*/
removeMap: function(map) {
this.map.events.unregister('zoomstart', this, this.onZoomStart);

if (this.pane && this.pane.parentNode) {
this.pane.parentNode.removeChild(this.pane);
}
OpenLayers.Layer.prototype.removeMap.apply(this, arguments);
},

/**
* Method: onZoomStart
*
* Parameters:
* evt - zoomstart event object with center and zoom properties.
*/
onZoomStart: function(evt) {
if (this.mapObject != null) {
var center = this.getMapObjectLonLatFromOLLonLat(evt.center);
var zoom = this.getMapObjectZoomFromOLZoom(evt.zoom);
this.setMapObjectCenter(center, zoom, false);
}
},

/**
* Method: loadWarningMessage
Expand Down
8 changes: 7 additions & 1 deletion lib/OpenLayers/Layer/Google/v3.js
Original file line number Diff line number Diff line change
Expand Up @@ -13,7 +13,13 @@
*
* Mixin providing functionality specific to the Google Maps API v3.
*
* To use this layer, you must include the GMaps v3 API in your html.
* To use this layer, you must include the GMaps v3 API in your html. To match
* Google's zoom animation better with OpenLayers animated zooming, configure
* your map with a zoomDuration of 10:
*
* (code)
* new OpenLayers.Map('map', {zoomDuration: 10});
* (end)
*
* Note that this layer configures the google.maps.map object with the
* "disableDefaultUI" option set to true. Using UI controls that the Google
Expand Down
15 changes: 12 additions & 3 deletions lib/OpenLayers/Map.js
Original file line number Diff line number Diff line change
Expand Up @@ -82,6 +82,9 @@ OpenLayers.Map = OpenLayers.Class({
* zoom has changed.
* move - triggered after each drag, pan, or zoom
* moveend - triggered after a drag, pan, or zoom completes
* zoomstart - triggered when a zoom starts. Listeners receive an object
* with *center* and *zoom* properties, for the target center and zoom
* leve.
* zoomend - triggered after a zoom completes
* mouseover - triggered after mouseover the map
* mouseout - triggered after mouseout the map
Expand Down Expand Up @@ -2393,6 +2396,15 @@ OpenLayers.Map = OpenLayers.Class({
if (map.baseLayer.wrapDateLine) {
zoom = map.adjustZoom(zoom);
}
var center = xy ?
map.getZoomTargetCenter(xy, map.getResolutionForZoom(zoom)) :
map.getCenter();
if (center) {
map.events.triggerEvent('zoomstart', {
center: center,
zoom: zoom
});
}
if (map.zoomTween) {
var currentRes = map.getResolution(),
targetRes = map.getResolutionForZoom(zoom),
Expand Down Expand Up @@ -2428,9 +2440,6 @@ OpenLayers.Map = OpenLayers.Class({
});
}
} else {
var center = xy ?
map.getZoomTargetCenter(xy, map.getResolutionForZoom(zoom)) :
null;
map.setCenter(center, zoom);
}
}
Expand Down
8 changes: 6 additions & 2 deletions tests/Map.html
Original file line number Diff line number Diff line change
Expand Up @@ -267,14 +267,18 @@
map.destroy();
}

function test_Map_zoomend_event (t) {
t.plan(2);
function test_Map_zoomstart_zoomend_event (t) {
t.plan(4);

map = new OpenLayers.Map('map', {zoomMethod: null});
var baseLayer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
map.addLayer(baseLayer);
map.events.register("zoomstart", {count: 0}, function() {
this.count++;
t.ok(true, "zoomstart event was triggered " + this.count + " times");
});
map.events.register("zoomend", {count: 0}, function() {
this.count++;
t.ok(true, "zoomend event was triggered " + this.count + " times");
Expand Down

0 comments on commit 58e3450

Please sign in to comment.