]> git.openstreetmap.org Git - rails.git/blob - app/views/site/index.rhtml
f8c0f02f429686dc68c4f5650bf644fceb5f41bf
[rails.git] / app / views / site / index.rhtml
1 <%= render :partial => 'search' %>
2
3 <% if params['mlon'] and params['mlat'] %>
4 <% marker = true %>
5 <% mlon = params['mlon'] %> 
6 <% mlat = params['mlat'] %>
7 <% lon =  params['mlon'] %>
8 <% lat =  params['mlat']  %>
9 <% zoom =  params['zoom'] || '12' %>
10 <script type="text/javascript">
11   var mlat = <%= lat %>;
12   var mlon = <%= lon %>;
13 </script>
14 <% elsif @user and params['lon'].nil? and params['lat'].nil? %> 
15 <% lon =  @user.home_lon %>
16 <% lat =  @user.home_lat %>
17 <% zoom = '10' %>
18 <%else%>
19 <% lon =  params['lon'] || '-0.1' %>
20 <% lat =  params['lat'] || '51.5' %>
21 <% zoom =  params['zoom'] || '4' %>
22 <% end %>
23
24
25
26 <script type="text/javascript">
27   var lat = <%= lat %>;
28   var lon = <%= lon %>;
29   var zoom = <%= zoom %>;
30   var PI = 3.14159265358979323846;
31
32
33
34   <% if params['scale'] and params['scale'].length > 0 then %>
35   zoom = Math.log(360.0/(( <% print params['scale'].to_f() %> ) * 512.0)) / Math.log(2.0);
36   <% end %>
37   zoom = zoom;
38   lon = lon * 20037508.34 / 180;
39   lat = Math.log(Math.tan( (90 + lat) * PI / 360)) / (PI / 180);
40   lat = lat * 20037508.34 / 180;
41   <% if marker %>
42   mlon = mlon * 20037508.34 / 180;
43   mlat = Math.log(Math.tan( (90 + mlat) * PI / 360)) / (PI / 180);
44   mlat = mlat * 20037508.34 / 180;
45   <%end%>
46
47 </script>
48 <script type="text/javascript" src="/openlayers/OpenLayers.js"></script>
49
50 <script type="text/javascript">
51   <!--
52   var map, layer, markers;
53   var ie6 = ! ( window.addEventListener || window.XMLHttpRequest );
54
55   function init(){
56
57     OpenLayers.Util.onImageLoadError = function() {
58       this.src = OpenLayers.Util.getImagesLocation() + "404.png";
59     }
60     map = new OpenLayers.Map( "map", 
61         {maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), numZoomLevels:19, maxResolution:156543, units:'meters', projection: "EPSG:41001"} );
62     layer = new OpenLayers.Layer.TMS( "Mapnik", "http://tile.openstreetmap.org/", {type:'png', getURL:getTileURL} );
63     map.addLayer(layer);
64     layer = new OpenLayers.Layer.TMS( "Osmarender", "http://dev.openstreetmap.org/~ojw/Tiles/tile.php/", {type:'png', getURL:getTileURL} );
65     map.addLayer(layer);
66
67     //Marker stuff
68
69     <% if marker %>
70     markers = new OpenLayers.Layer.Markers("markers");
71     map.addLayer(markers); 
72
73     marker = new OpenLayers.Marker(new OpenLayers.LonLat(mlon,mlat));
74     markers.addMarker(marker);
75     <%end%>
76
77     map.setOptions({theme:''});
78     map.addControl(new OpenLayers.Control.LayerSwitcher());
79     map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
80
81     map.events.register("moveend", map, updateLocation);
82     document.getElementById( 'map_OpenLayers_ViewPort' ).style.position = 'absolute';
83     if( ie6 ) {
84       handleResize();
85     }
86     updateLocation();
87   }        
88
89   function getTileURL( bounds ) {
90     var res = this.map.getResolution();
91     var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
92     var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
93     var z = this.map.getZoom();
94     return this.url + z + "/" + x + "/" + y + "." + this.type; 
95   }
96
97   function updateLocation() {
98     var lonlat = map.getCenter();
99
100     var lon_deg = (lonlat.lon / 20037508.34) * 180;
101     var lat_deg = (lonlat.lat / 20037508.34) * 180;
102     var PI = 3.14159265358979323846;
103     lat_deg = 180/PI * (2 * Math.atan(Math.exp(lat_deg * PI / 180)) - PI / 2);
104     var zoom = map.getZoom();
105
106     updatelinks(lon_deg,lat_deg,zoom);
107   }
108
109   function getStyle( el, property ) {
110     var style;
111     if( el.currentStyle ) {
112         style = el.currentStyle[property];
113     } else if( window.getComputedStyle ) {
114         style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
115     } else {
116         style = el.style[property];
117     }
118     return style;
119   }
120   
121   function handleResize() {
122     var el = document.getElementById( 'map' );
123     var left = getStyle( el, 'left' );
124     var top = getStyle( el, 'top' );
125     var bottom = getStyle( el, 'bottom' );
126     var right = getStyle( el, 'right' );
127     var width = el.old_width;
128     if( ! width ) {
129         width = getStyle( el, 'width' );
130     }
131     var height = el.old_height;
132     if( ! height ) {
133         height = getStyle( el, 'height' );
134     }
135     var pwidth, pheight;
136     if( el.offsetParent.nodeName == 'BODY' || el.offsetParent.nodeName == 'HTML' ) {
137       if( typeof( window.innerWidth ) == 'number' ) {
138           pwidth = window.innerWidth;
139           height = window.innerHeight;
140       } else if( document.documentElement &&
141           ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
142           pwidth = document.documentElement.clientWidth;
143           pheight = document.documentElement.clientHeight;
144       } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
145           pwidth = document.body.clientWidth;
146           pheight = document.body.clientHeight;
147       }
148     } else {
149       pwidth = el.offsetParent.offsetWidth;
150       pheight = el.offsetParent.offsetHeight;
151     }
152     if( left != 'auto' && right != 'auto' && width == 'auto' ) {
153       el.old_width = width;
154       var new_width = (pwidth - el.offsetLeft - parseInt( right ) );
155       el.style.width = new_width + 'px';
156     }
157     if( top != 'auto' && bottom != 'auto' && height == 'auto' ) {
158       el.old_height = height;
159       var new_height = (pheight - el.offsetTop - parseInt( bottom ) );
160       el.style.height = new_height + 'px';
161     }
162     map.updateSize();
163     el.style.display = 'none';
164     setTimeout( function() {
165         el.style.display = '';
166         }, 200 );
167   }
168
169 if( ie6 ) {
170   window.onresize = handleResize;
171 }
172 window.onload = init;
173 // -->
174 </script>