]> git.openstreetmap.org Git - rails.git/blob - app/views/site/index.rhtml
d5c7b9e0349aff4725017f003c88d426724f6441
[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="/javascripts/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 = "http://www.openstreetmap.org/javascripts/img/404.png";
59     }
60     map = new OpenLayers.Map( "map", 
61         {maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34), maxZoomLevel:18, 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.addControl(new OpenLayers.Control.LayerSwitcher());
78     map.setCenter(new OpenLayers.LonLat(lon, lat), zoom);
79
80     map.events.register("moveend", map, updateLocation);
81     document.getElementById( 'map_OpenLayers_ViewPort' ).style.position = 'absolute';
82     if( ie6 ) {
83       handleResize();
84     }
85     updateLocation();
86   }        
87
88   function getTileURL( bounds ) {
89     var res = this.map.getResolution();
90     var x = Math.round ((bounds.left - this.maxExtent.left) / (res * this.tileSize.w));
91     var y = Math.round ((this.maxExtent.top - bounds.top) / (res * this.tileSize.h));
92     var z = this.map.getZoom();
93     return this.url + z + "/" + x + "/" + y + "." + this.type; 
94   }
95
96   function updateLocation() {
97     var lonlat = map.getCenter();
98
99     var lon_deg = (lonlat.lon / 20037508.34) * 180;
100     var lat_deg = (lonlat.lat / 20037508.34) * 180;
101     var PI = 3.14159265358979323846;
102     lat_deg = 180/PI * (2 * Math.atan(Math.exp(lat_deg * PI / 180)) - PI / 2);
103     var zoom = map.getZoom();
104
105     updatelinks(lon_deg,lat_deg,zoom);
106   }
107
108   function getStyle( el, property ) {
109     var style;
110     if( el.currentStyle ) {
111         style = el.currentStyle[property];
112     } else if( window.getComputedStyle ) {
113         style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
114     } else {
115         style = el.style[property];
116     }
117     return style;
118   }
119   
120   function handleResize() {
121     var el = document.getElementById( 'map' );
122     var left = getStyle( el, 'left' );
123     var top = getStyle( el, 'top' );
124     var bottom = getStyle( el, 'bottom' );
125     var right = getStyle( el, 'right' );
126     var width = el.old_width;
127     if( ! width ) {
128         width = getStyle( el, 'width' );
129     }
130     var height = el.old_height;
131     if( ! height ) {
132         height = getStyle( el, 'height' );
133     }
134     var pwidth, pheight;
135     if( el.offsetParent.nodeName == 'BODY' || el.offsetParent.nodeName == 'HTML' ) {
136       if( typeof( window.innerWidth ) == 'number' ) {
137           pwidth = window.innerWidth;
138           height = window.innerHeight;
139       } else if( document.documentElement &&
140           ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
141           pwidth = document.documentElement.clientWidth;
142           pheight = document.documentElement.clientHeight;
143       } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
144           pwidth = document.body.clientWidth;
145           pheight = document.body.clientHeight;
146       }
147     } else {
148       pwidth = el.offsetParent.offsetWidth;
149       pheight = el.offsetParent.offsetHeight;
150     }
151     if( left != 'auto' && right != 'auto' && width == 'auto' ) {
152       el.old_width = width;
153       var new_width = (pwidth - el.offsetLeft - parseInt( right ) );
154       el.style.width = new_width + 'px';
155     }
156     if( top != 'auto' && bottom != 'auto' && height == 'auto' ) {
157       el.old_height = height;
158       var new_height = (pheight - el.offsetTop - parseInt( bottom ) );
159       el.style.height = new_height + 'px';
160     }
161     map.updateSize();
162     el.style.display = 'none';
163     setTimeout( function() {
164         el.style.display = '';
165         }, 200 );
166   }
167
168 if( ie6 ) {
169   window.onresize = handleResize;
170 }
171 window.onload = init;
172 // -->
173 </script>