{"id":22,"date":"2011-11-30T09:48:40","date_gmt":"2011-11-30T09:48:40","guid":{"rendered":"http:\/\/www.spatially-oriented.com\/blog\/?p=22"},"modified":"2012-05-05T22:21:23","modified_gmt":"2012-05-05T20:21:23","slug":"animated-zoom-with-openlayers","status":"publish","type":"post","link":"https:\/\/www.spatially-oriented.com\/blog\/2011\/11\/30\/animated-zoom-with-openlayers\/","title":{"rendered":"Animated zoom with OpenLayers"},"content":{"rendered":"<p>So I was dabbling with some OpenLayers stuff at work and stumbled upon an <a title=\"Development Example of tweening\" href=\"http:\/\/openlayers.org\/dev\/tests\/manual\/tween.html\" target=\"_blank\">example<\/a> of the Tweening <a title=\"Tweening docs\" href=\"http:\/\/dev.openlayers.org\/apidocs\/files\/OpenLayers\/Tween-js.html\" target=\"_blank\">class <\/a>of OpenLayers. Instantly, I got a nice idea.<\/p>\n<p>Let&#8217;s go build a Google Earth-like zoom with OpenLayers!<\/p>\n<p><!--more--><\/p>\n<p>The tweening class is actually pretty basic. Well, to be honest I wouldn&#8217;t be able to build it, but the idea of it is pretty basic nonetheless.<\/p>\n<p>You have point A and point B, you specify a duration \/ number of steps in between and it will calculate a path between the two points.<\/p>\n<p>Next you just start it and it will alert you every time the point changes on the path.<\/p>\n<p>So, the example linked above moves the box, but what if we moved the map&#8217;s center itself? We have an OpenLayers.Map override where I added the following functions:<\/p>\n<p><code lang=\"javascript[lines]\">    currentTween: null,<br \/>\n    ZoomToCoordinate: function (xcoord, ycoord) {<br \/>\n        if (this.currentTween != null) {<br \/>\n            this.currentTween.stop();<br \/>\n            this.currentTween = null;<br \/>\n        }<\/p>\n<p>        var center = this.getCenter();<br \/>\n        var from = {<br \/>\n            x: center.lon,<br \/>\n            y: center.lat<br \/>\n        };<br \/>\n        var to = {<br \/>\n            x: xcoord,<br \/>\n            y: ycoord<br \/>\n        }<br \/>\n        var duration = 20;<br \/>\n        var callbacks = {<br \/>\n            eachStep: this.ZoomStep<br \/>\n        };<br \/>\n        var navigationTween = new OpenLayers.Tween(<br \/>\n              OpenLayers.Easing.Quad.easeInOut<br \/>\n        );<br \/>\n        navigationTween.map = this;<\/p>\n<p>        this.currentTween = navigationTween;<\/p>\n<p>        navigationTween.start(<br \/>\n              from,<br \/>\n              to,<br \/>\n              duration,<br \/>\n              { callbacks: callbacks }<br \/>\n        );<br \/>\n    },<\/p>\n<p>    ZoomStep: function (value) {<br \/>\n        this.map.setCenter(<br \/>\n               new OpenLayers.LonLat(<br \/>\n                       value.x, value.y<br \/>\n               ),<br \/>\n               this.map.getCenter(),<br \/>\n               false,<br \/>\n               false<br \/>\n        );<br \/>\n    },<\/code><\/p>\n<pre>&nbsp;<\/pre>\n<p>Next up will be trying to get a google earth style zooming. First it should zoom out a bit, then slide, then zoom in again. This should drastically lower the number of tiles Openlayers fetches. More on that later&#8230;<\/p>\n<p><strong>Update 30\/11\/2011<\/strong>: I tried to create a 3 part tween: zoom Out, Slide, Zoom In, but I think the zooming in and out takes too much time \/ effort for a smooth animation. You never really see what is happening, unless you set the step timing (Tween.INTERVAL) to half a second or or more. (although I didn&#8217;t try it out yet&#8230;)<\/p>\n<p>Also, sliding large area&#8217;s of your map doesn&#8217;t really help the user as well. Plus you might actually overflow your wms\/wfs server with requests&#8230; So be carefull, there are some baby dragons on this path!<\/p>\n","protected":false},"excerpt":{"rendered":"<p>So I was dabbling with some OpenLayers stuff at work and stumbled upon an example of the Tweening class of OpenLayers. Instantly, I got a nice idea. Let&#8217;s go build a Google Earth-like zoom with OpenLayers!<\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"jetpack_post_was_ever_published":false,"_jetpack_newsletter_access":"","_jetpack_dont_email_post_to_subs":false,"_jetpack_newsletter_tier_id":0,"_jetpack_memberships_contains_paywalled_content":false,"_jetpack_memberships_contains_paid_content":false,"footnotes":"","jetpack_publicize_message":"","jetpack_publicize_feature_enabled":true,"jetpack_social_post_already_shared":false,"jetpack_social_options":{"image_generator_settings":{"template":"highway","default_image_id":0,"font":"","enabled":false},"version":2}},"categories":[5,4],"tags":[22,25],"class_list":["post-22","post","type-post","status-publish","format-standard","hentry","category-gis","category-tech","tag-openlayers","tag-tween"],"jetpack_publicize_connections":[],"jetpack_featured_media_url":"","jetpack_sharing_enabled":true,"jetpack_shortlink":"https:\/\/wp.me\/p2n8yP-m","_links":{"self":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts\/22","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/comments?post=22"}],"version-history":[{"count":5,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions"}],"predecessor-version":[{"id":24,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/posts\/22\/revisions\/24"}],"wp:attachment":[{"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/media?parent=22"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/categories?post=22"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.spatially-oriented.com\/blog\/wp-json\/wp\/v2\/tags?post=22"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}