From 2c397321c9309eb9cd4dfed21375fc821a8d2d37 Mon Sep 17 00:00:00 2001 From: Tom Hughes Date: Wed, 14 Mar 2012 19:12:32 +0000 Subject: [PATCH] Refactor calculation of zoom level based on a scale --- app/helpers/application_helper.rb | 4 ++++ app/views/site/index.html.erb | 8 ++++---- app/views/user/_map.html.erb | 8 ++++---- 3 files changed, 12 insertions(+), 8 deletions(-) diff --git a/app/helpers/application_helper.rb b/app/helpers/application_helper.rb index ce0c5d67b..4aec9f5c1 100644 --- a/app/helpers/application_helper.rb +++ b/app/helpers/application_helper.rb @@ -125,6 +125,10 @@ module ApplicationHelper end end + def scale_to_zoom(scale) + Math.log(360.0 / (scale.to_f * 512.0)) / Math.log(2.0) + end + private def javascript_strings_for_key(key) diff --git a/app/views/site/index.html.erb b/app/views/site/index.html.erb index e268d5d3c..6cff7f133 100644 --- a/app/views/site/index.html.erb +++ b/app/views/site/index.html.erb @@ -151,11 +151,11 @@ end <% end %> <% else %> var centre = new OpenLayers.LonLat(<%= lon %>, <%= lat %>); + <% if params[:scale].to_f > 0 then -%> + var zoom = <%= scale_to_zoom params[:scale] %>; + <% else -%> var zoom = <%= zoom %>; - - <% if params[:scale] and params[:scale].length > 0 and params[:scale].to_f > 0 then %> - zoom = <%= Math.log(360.0 / (params[:scale].to_f * 512.0)) / Math.log(2.0) %>; - <% end %> + <% end -%> setMapCenter(centre, zoom); <% end %> diff --git a/app/views/user/_map.html.erb b/app/views/user/_map.html.erb index 71f3ad9e5..2d886527c 100644 --- a/app/views/user/_map.html.erb +++ b/app/views/user/_map.html.erb @@ -24,11 +24,11 @@ end function init(){ var centre = new OpenLayers.LonLat(<%= lon %>, <%= lat %>); + <% if params[:scale].to_f > 0 then -%> + var zoom = <%= scale_to_zoom params[:scale] %>; + <% else -%> var zoom = <%= zoom %>; - - <% if params[:scale] and params[:scale].length > 0 and params[:scale].to_f > 0 then %> - zoom = <%= Math.log(360.0 / (params[:scale].to_f * 512.0)) / Math.log(2.0) %>; - <% end %> + <% end -%> var map = createMap("map"); -- 2.43.2