Adding animated zooming

This commit is contained in:
ahocevar
2012-12-23 18:47:03 +01:00
parent d2b3bded72
commit 21448d2fd5
17 changed files with 239 additions and 67 deletions

View File

@@ -238,8 +238,7 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, {
* evt - {Event}
*/
defaultDblClick: function (evt) {
var newCenter = this.map.getLonLatFromViewPortPx( evt.xy );
this.map.setCenter(newCenter, this.map.zoom + 1);
this.map.zoomTo(this.map.zoom + 1, evt.xy);
},
/**
@@ -249,8 +248,7 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, {
* evt - {Event}
*/
defaultDblRightClick: function (evt) {
var newCenter = this.map.getLonLatFromViewPortPx( evt.xy );
this.map.setCenter(newCenter, this.map.zoom - 1);
this.map.zoomTo(this.map.zoom - 1, evt.xy);
},
/**
@@ -264,22 +262,14 @@ OpenLayers.Control.Navigation = OpenLayers.Class(OpenLayers.Control, {
if (!this.map.fractionalZoom) {
deltaZ = Math.round(deltaZ);
}
var currentZoom = this.map.getZoom();
var newZoom = this.map.getZoom() + deltaZ;
var currentZoom = this.map.getZoom(),
newZoom = currentZoom + deltaZ;
newZoom = Math.max(newZoom, 0);
newZoom = Math.min(newZoom, this.map.getNumZoomLevels());
if (newZoom === currentZoom) {
return;
}
var size = this.map.getSize();
var deltaX = size.w/2 - evt.xy.x;
var deltaY = evt.xy.y - size.h/2;
var newRes = this.map.baseLayer.getResolutionForZoom(newZoom);
var zoomPoint = this.map.getLonLatFromPixel(evt.xy);
var newCenter = new OpenLayers.LonLat(
zoomPoint.lon + deltaX * newRes,
zoomPoint.lat + deltaY * newRes );
this.map.setCenter( newCenter, newZoom );
this.map.zoomTo(newZoom, evt.xy);
},
/**

View File

@@ -175,8 +175,7 @@ OpenLayers.Control.TouchNavigation = OpenLayers.Class(OpenLayers.Control, {
* evt - {Event}
*/
defaultDblClick: function (evt) {
var newCenter = this.map.getLonLatFromViewPortPx(evt.xy);
this.map.setCenter(newCenter, this.map.zoom + 1);
this.map.zoomTo(this.map.zoom + 1, evt.xy);
},
CLASS_NAME: "OpenLayers.Control.TouchNavigation"

View File

@@ -69,7 +69,8 @@ OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, {
*/
zoomBox: function (position) {
if (position instanceof OpenLayers.Bounds) {
var bounds;
var bounds,
targetCenterPx = position.getCenterPixel();
if (!this.out) {
var minXY = this.map.getLonLatFromPixel({
x: position.left,
@@ -87,8 +88,7 @@ OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, {
var zoomFactor = Math.min((this.map.size.h / pixHeight),
(this.map.size.w / pixWidth));
var extent = this.map.getExtent();
var center = this.map.getLonLatFromPixel(
position.getCenterPixel());
var center = this.map.getLonLatFromPixel(targetCenterPx);
var xmin = center.lon - (extent.getWidth()/2)*zoomFactor;
var xmax = center.lon + (extent.getWidth()/2)*zoomFactor;
var ymin = center.lat - (extent.getHeight()/2)*zoomFactor;
@@ -96,18 +96,27 @@ OpenLayers.Control.ZoomBox = OpenLayers.Class(OpenLayers.Control, {
bounds = new OpenLayers.Bounds(xmin, ymin, xmax, ymax);
}
// always zoom in/out
var lastZoom = this.map.getZoom();
this.map.zoomToExtent(bounds);
var lastZoom = this.map.getZoom(),
size = this.map.getSize(),
centerPx = {x: size.w / 2, y: size.h / 2},
zoom = this.map.getZoomForExtent(bounds),
oldRes = this.map.getResolution(),
newRes = this.map.getResolutionForZoom(zoom),
zoomOriginPx = {
x: targetCenterPx.x +
(targetCenterPx.x - centerPx.x) * newRes / oldRes,
y: targetCenterPx.y +
(targetCenterPx.y - centerPx.y) * newRes / oldRes
};
this.map.zoomTo(zoom, zoomOriginPx);
if (lastZoom == this.map.getZoom() && this.alwaysZoom == true){
this.map.zoomTo(lastZoom + (this.out ? -1 : 1));
}
} else if (this.zoomOnClick) { // it's a pixel
if (!this.out) {
this.map.setCenter(this.map.getLonLatFromPixel(position),
this.map.getZoom() + 1);
this.map.zoomTo(this.map.getZoom() + 1, position);
} else {
this.map.setCenter(this.map.getLonLatFromPixel(position),
this.map.getZoom() - 1);
this.map.zoomTo(this.map.getZoom() - 1, position);
}
}
},

View File

@@ -387,12 +387,6 @@ OpenLayers.Map = OpenLayers.Class({
*/
autoUpdateSize: true,
/**
* Property: panTween
* {<OpenLayers.Tween>} Animated panning tween object, see panTo()
*/
panTween: null,
/**
* APIProperty: eventListeners
* {Object} If set as an option at construction, the eventListeners
@@ -402,6 +396,12 @@ OpenLayers.Map = OpenLayers.Class({
*/
eventListeners: null,
/**
* Property: panTween
* {<OpenLayers.Tween>} Animated panning tween object, see panTo()
*/
panTween: null,
/**
* APIProperty: panMethod
* {Function} The Easing function to be used for tweening. Default is
@@ -419,6 +419,28 @@ OpenLayers.Map = OpenLayers.Class({
*/
panDuration: 50,
/**
* Property: zoomTween
* {<OpenLayers.Tween>} Animated zooming tween object, see zoomTo()
*/
zoomTween: null,
/**
* APIProperty: zoomMethod
* {Function} The Easing function to be used for tweening. Default is
* OpenLayers.Easing.Quad.easeOut. Setting this to 'null' turns off
* animated zooming.
*/
zoomMethod: OpenLayers.Easing.Quad.easeOut,
/**
* Property: zoomDuration
* {Integer} The number of steps to be passed to the
* OpenLayers.Tween.start() method when the map is zoomed.
* Default is 20.
*/
zoomDuration: 20,
/**
* Property: paddingForPopups
* {<OpenLayers.Bounds>} Outside margin of the popup. Used to prevent
@@ -586,6 +608,7 @@ OpenLayers.Map = OpenLayers.Class({
this.layerContainerDiv = OpenLayers.Util.createDiv(id);
this.layerContainerDiv.style.zIndex=this.Z_INDEX_BASE['Popup']-1;
this.layerContainerOriginPx = {x: 0, y: 0};
this.applyTransform();
this.viewPortDiv.appendChild(this.layerContainerDiv);
@@ -681,6 +704,13 @@ OpenLayers.Map = OpenLayers.Class({
this.setCenter(options.center, options.zoom);
}
}
if (this.panMethod) {
this.panTween = new OpenLayers.Tween(this.panMethod);
}
if (this.zoomMethod && this.applyTransform.transform) {
this.zoomTween = new OpenLayers.Tween(this.zoomMethod);
}
},
/**
@@ -749,6 +779,11 @@ OpenLayers.Map = OpenLayers.Class({
this.panTween.stop();
this.panTween = null;
}
// make sure zooming doesn't continue after destruction
if(this.zoomTween) {
this.zoomTween.stop();
this.zoomTween = null;
}
// map has been destroyed. dont do it again!
OpenLayers.Event.stopObserving(window, 'unload', this.unloadDestroy);
@@ -1664,10 +1699,7 @@ OpenLayers.Map = OpenLayers.Class({
* lonlat - {<OpenLayers.LonLat>}
*/
panTo: function(lonlat) {
if (this.panMethod && this.getExtent().scale(this.panRatio).containsLonLat(lonlat)) {
if (!this.panTween) {
this.panTween = new OpenLayers.Tween(this.panMethod);
}
if (this.panTween && this.getExtent().scale(this.panRatio).containsLonLat(lonlat)) {
var center = this.getCachedCenter();
// center will not change, don't do nothing
@@ -1718,7 +1750,12 @@ OpenLayers.Map = OpenLayers.Class({
* TBD: reconsider forceZoomChange in 3.0
*/
setCenter: function(lonlat, zoom, dragging, forceZoomChange) {
this.panTween && this.panTween.stop();
if (this.panTween) {
this.panTween.stop();
}
if (this.zoomTween) {
this.zoomTween.stop();
}
this.moveTo(lonlat, zoom, {
'dragging': dragging,
'forceZoomChange': forceZoomChange
@@ -2319,14 +2356,62 @@ OpenLayers.Map = OpenLayers.Class({
/**
* APIMethod: zoomTo
* Zoom to a specific zoom level
* Zoom to a specific zoom level. Zooming will be animated unless the map
* is configured with {zoomMethod: null}. To zoom without animation, use
* <setCenter> without a lonlat argument.
*
* Parameters:
* zoom - {Integer}
*/
zoomTo: function(zoom) {
if (this.isValidZoomLevel(zoom)) {
this.setCenter(null, zoom);
zoomTo: function(zoom, xy) {
// non-API arguments:
// xy - {<OpenLayers.Pixel>} optional zoom origin
var map = this;
if (map.isValidZoomLevel(zoom)) {
if (map.baseLayer.wrapDateLine) {
zoom = map.adjustZoom(zoom);
}
if (map.zoomTween) {
var currentRes = map.getResolution(),
targetRes = map.getResolutionForZoom(zoom),
start = {scale: 1},
end = {scale: currentRes / targetRes};
if (map.zoomTween.playing && map.zoomTween.duration < 3 * map.zoomDuration) {
// update the end scale, and reuse the running zoomTween
map.zoomTween.finish = {
scale: map.zoomTween.finish.scale * end.scale
}
} else {
if (!xy) {
var size = map.getSize();
xy = {x: size.w / 2, y: size.h / 2};
}
map.zoomTween.start(start, end, map.zoomDuration, {
minFrameRate: 50, // don't spend much time zooming
callbacks: {
eachStep: function(data) {
var containerOrigin = map.layerContainerOriginPx,
scale = data.scale,
dx = ((scale - 1) * (containerOrigin.x - xy.x)) | 0,
dy = ((scale - 1) * (containerOrigin.y - xy.y)) | 0;
map.applyTransform(containerOrigin.x + dx, containerOrigin.y + dy, scale);
},
done: function(data) {
map.applyTransform();
var resolution = map.getResolution() / data.scale,
zoom = map.getZoomForResolution(resolution, true)
map.moveTo(map.getZoomTargetCenter(xy, resolution), zoom, true);
}
}
});
}
} else {
var center = xy ?
map.getZoomTargetCenter(xy, map.getResolutionForZoom(zoom)) :
null;
map.setCenter(center, zoom);
}
}
},
@@ -2487,6 +2572,31 @@ OpenLayers.Map = OpenLayers.Class({
return px;
},
/**
* Method: getZoomTargetCenter
*
* Parameters:
* xy - {<OpenLayers.Pixel>} The zoom origin pixel location on the screen
* resolution - {Float} The resolution we want to get the center for
*
* Returns:
* {<OpenLayers.LonLat>} The location of the map center after the
* transformation described by the origin xy and the target resolution.
*/
getZoomTargetCenter: function (xy, resolution) {
var lonlat = null,
size = this.getSize(),
deltaX = size.w/2 - xy.x,
deltaY = xy.y - size.h/2,
zoomPoint = this.getLonLatFromPixel(xy);
if (zoomPoint) {
lonlat = new OpenLayers.LonLat(
zoomPoint.lon + deltaX * resolution,
zoomPoint.lat + deltaY * resolution
);
}
return lonlat;
},
//
// CONVENIENCE TRANSLATION FUNCTIONS FOR API

View File

@@ -148,6 +148,7 @@
var nav = new OpenLayers.Control.Navigation({zoomWheelEnabled: false});
var map = new OpenLayers.Map({
div: "map",
zoomMethod: null,
controls: [nav],
layers: [
new OpenLayers.Layer(null, {isBaseLayer: true})

View File

@@ -170,7 +170,7 @@
function test_clear(t) {
t.plan(7);
var map = new OpenLayers.Map("map");
var map = new OpenLayers.Map("map", {zoomMethod: null});
var layer = new OpenLayers.Layer(
"test", {isBaseLayer: true}
);

View File

@@ -77,7 +77,7 @@
function test_Control_PanZoomBar_onButtonClick (t) {
t.plan(2);
map = new OpenLayers.Map('map', {controls:[]});
map = new OpenLayers.Map('map', {controls:[], zoomMethod: null});
var layer = new OpenLayers.Layer.WMS("Test Layer",
"http://octo.metacarta.com/cgi-bin/mapserv?",
{map: "/mapdata/vmap_wms.map", layers: "basic"});
@@ -97,7 +97,8 @@
t.plan(1);
map = new OpenLayers.Map('map', {
controls: [],
fractionalZoom: true
fractionalZoom: true,
zoomMethod: null
});
var layer = new OpenLayers.Layer.WMS("Test Layer", "http://octo.metacarta.com/cgi-bin/mapserv?", {
map: "/mapdata/vmap_wms.map",
@@ -127,7 +128,8 @@
var map = new OpenLayers.Map('map', {
controls: [],
fractionalZoom: true
fractionalZoom: true,
zoomMethod: null,
});
var layer = new OpenLayers.Layer.WMS("Test Layer", "http://octo.metacarta.com/cgi-bin/mapserv?", {
map: "/mapdata/vmap_wms.map",

View File

@@ -22,7 +22,7 @@
control = new OpenLayers.Control.Scale('scale');
t.ok( control instanceof OpenLayers.Control.Scale, "new OpenLayers.Control returns object" );
map = new OpenLayers.Map('map');
map = new OpenLayers.Map('map', {zoomMethod: null});
layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
map.addLayer(layer);
map.zoomTo(0);
@@ -38,7 +38,7 @@
t.plan(2);
control = new OpenLayers.Control.Scale();
t.ok( control instanceof OpenLayers.Control.Scale, "new OpenLayers.Control returns object" );
map = new OpenLayers.Map('map');
map = new OpenLayers.Map('map', {zoomMethod: null});
layer = new OpenLayers.Layer.WMS('Test Layer', "http://octo.metacarta.com/cgi-bin/mapserv", {map: '/mapdata/vmap_wms.map', layers: 'basic', format: 'image/jpeg'});
map.addLayer(layer);
map.zoomTo(0);

View File

@@ -43,7 +43,8 @@
var map = new OpenLayers.Map({
div: "map",
layers: [new OpenLayers.Layer(null, {isBaseLayer: true})]
layers: [new OpenLayers.Layer(null, {isBaseLayer: true})],
zoomMethod: null
});
var control = new OpenLayers.Control.Zoom();
map.addControl(control);
@@ -60,7 +61,8 @@
var map = new OpenLayers.Map({
div: "map",
layers: [new OpenLayers.Layer(null, {isBaseLayer: true})]
layers: [new OpenLayers.Layer(null, {isBaseLayer: true})],
zoomMethod: null
});
var control = new OpenLayers.Control.Zoom();
map.addControl(control);

View File

@@ -672,7 +672,7 @@
}
function test_Layer_Grid_getTileBounds(t) {
t.plan(2);
var map = new OpenLayers.Map("map2");
var map = new OpenLayers.Map("map2", {zoomMethod: null});
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.WMS(name, url, params);
@@ -869,7 +869,8 @@
t.plan(11);
var map = new OpenLayers.Map('map', {
resolutions: [32, 16, 8, 4, 2, 1]
resolutions: [32, 16, 8, 4, 2, 1],
zoomMethod: null
});
var layer = new OpenLayers.Layer.WMS('', '', {}, {
isBaseLayer: true,
@@ -930,7 +931,8 @@
t.plan(4);
var map = new OpenLayers.Map('map', {
resolutions: [1, 0.5, 0.025]
resolutions: [1, 0.5, 0.025],
zoomMethod: null
});
var resolution;
var layer = new OpenLayers.Layer.WMS('', '', {}, {
@@ -974,7 +976,8 @@
t.plan(4);
var map = new OpenLayers.Map('map', {
resolutions: [1, 0.5, 0.025]
resolutions: [1, 0.5, 0.025],
zoomMethod: null
});
var resolution;
var layer = new OpenLayers.Layer.WMS('', '', {}, {
@@ -1156,7 +1159,7 @@
t.plan(4);
var map = new OpenLayers.Map('map');
var map = new OpenLayers.Map('map', {zoomMethod: null});
var layer = new OpenLayers.Layer.WMS('', '', {}, {
isBaseLayer: true,
singleTile: true,
@@ -1187,7 +1190,8 @@
//
var map = new OpenLayers.Map('map', {
resolutions: [32, 16, 8, 4, 2, 1]
resolutions: [32, 16, 8, 4, 2, 1],
zoomMethod: null
});
var layer = new OpenLayers.Layer.WMS(
"WMS",

View File

@@ -233,7 +233,7 @@
}
function test_Layer_KaMap_getTileBounds(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var map = new OpenLayers.Map("map", {zoomMethod: null});
var url = "http://octo.metacarta.com/cgi-bin/mapserv";
layer = new OpenLayers.Layer.KaMap(name, url, params);

View File

@@ -59,7 +59,7 @@
t.plan(6);
var map = new OpenLayers.Map("map");
var map = new OpenLayers.Map("map", {zoomMethod: null});
var layer = new OpenLayers.Layer.Markers("Base", {isBaseLayer: true});
map.addLayer(layer);
map.setCenter(new OpenLayers.LonLat(0, 0), 1);

View File

@@ -211,7 +211,8 @@
div: "map",
layers: [layer],
center: new OpenLayers.LonLat(0, 0),
zoom: 1
zoom: 1,
zoomMethod: null
});
t.eq(layer.features.length, 50, "50 features at zoom 1");

View File

@@ -245,7 +245,8 @@
layers: [layer],
projection: "EPSG:4326",
maxResolution: 0.3515625,
maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90)
maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
zoomMethod: null
});
map.setCenter(new OpenLayers.LonLat(-97.0, 38.0), 1);
t.eq(layer.getURL(new OpenLayers.Bounds(-135.0, 0.0, -90.0, 45.0)),

View File

@@ -216,6 +216,7 @@
t.plan(14);
var log = [];
map = new OpenLayers.Map('map', {
zoomMethod: null,
eventListeners: {
"movestart": function() {log.push("movestart");},
"move": function() {log.push("move");},
@@ -268,7 +269,7 @@
function test_Map_zoomend_event (t) {
t.plan(2);
map = new OpenLayers.Map('map');
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"});
@@ -1292,7 +1293,8 @@
extent = new OpenLayers.Bounds(8, 44.5, 19, 50);
var options = {
restrictedExtent: extent
restrictedExtent: extent,
zoomMethod: null
};
map = new OpenLayers.Map('map', options);
@@ -1330,7 +1332,7 @@
function test_zoomTo(t) {
t.plan(8);
var map = new OpenLayers.Map("map");
var map = new OpenLayers.Map("map", {zoomMethod: null});
map.addLayer(new OpenLayers.Layer(null, {
isBaseLayer: true
}));
@@ -1367,6 +1369,38 @@
map.destroy();
}
function test_zoomTo_animated(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
map.addLayer(new OpenLayers.Layer(null, {
isBaseLayer: true
}));
map.zoomToMaxExtent();
map.zoomTo(2);
map.zoomIn();
map.zoomOut();
map.zoomIn();
t.delay_call(2, function() {
t.eq(map.getZoom(), 3, '[fractionalZoom: false] zoomTo(2) - zoomIn() - zoomOut() - zoomIn()');
// now allow fractional zoom
map.fractionalZoom = true;
map.zoomTo(2.6);
map.zoomIn();
map.zoomOut();
map.zoomIn();
});
t.delay_call(4, function() {
t.eq(map.getZoom(), 3.6, '[fractionalZoom: true] zoomTo(2) - zoomIn() - zoomOut() - zoomIn()');
map.destroy();
});
}
function test_Map_getUnits(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
@@ -2167,6 +2201,24 @@
t.ok(center.equals(new OpenLayers.LonLat(-13.25, 56)), "Center is correct and not equal to maxExtent's center");
}
function test_getZoomTargetCenter(t) {
t.plan(1);
var map = new OpenLayers.Map({
div: 'map',
layers: [
new OpenLayers.Layer('', {isBaseLayer: true})
],
center: [0, 0],
zoom: 1
});
var ll = map.getZoomTargetCenter({x: 44, y: 22}, map.getMaxResolution());
t.eq(ll.toShortString(), "180, -90", "getZoomTargetCenter works.");
map.destroy();
}
function test_autoUpdateSize(t) {
t.plan(1);
OpenLayers.Event.unloadCache();

View File

@@ -51,7 +51,7 @@
});
// create a map with the layers and a center
var map = new OpenLayers.Map("map");
var map = new OpenLayers.Map("map", {zoomMethod: null});
map.addLayers([dummy, layer]);
map.zoomToMaxExtent();
@@ -206,7 +206,7 @@
function test_resFactor(t) {
t.plan(2);
var map = new OpenLayers.Map("map");
var map = new OpenLayers.Map("map", {zoomMethod: null});
var bbox = new OpenLayers.Strategy.BBOX();
var fakeProtocol = new OpenLayers.Protocol({
'read': function() {

View File

@@ -44,7 +44,8 @@
});
var map = new OpenLayers.Map('map', {
resolutions: [4, 2, 1],
maxExtent: new OpenLayers.Bounds(-40, -40, 40, 40)
maxExtent: new OpenLayers.Bounds(-40, -40, 40, 40),
zoomMethod: null,
});
map.addLayer(layer);