]> git.openstreetmap.org Git - rails.git/blob - app/views/site/index.rhtml
Unconfuse OpenLayer's base location detection.
[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 <% elsif @user and params['lon'].nil? and params['lat'].nil? %> 
11 <% lon =  @user.home_lon %>
12 <% lat =  @user.home_lat %>
13 <% zoom = '10' %>
14 <%else%>
15 <% lon =  params['lon'] || '-0.1' %>
16 <% lat =  params['lat'] || '51.5' %>
17 <% zoom =  params['zoom'] || '4' %>
18 <% end %>
19
20 <script type="text/javascript" src="/openlayers/OpenLayers.js"></script>
21 <%= javascript_include_tag 'map.js' %>
22
23 <script type="text/javascript">
24   <!--
25   var ie6 = ! ( window.addEventListener || window.XMLHttpRequest );
26   var map;
27
28   function init(){
29     var centre = lonLatToMercator(new OpenLayers.LonLat(<%= lon %>, <%= lat %>));
30     var zoom = <%= zoom %>;
31
32     <% if params['scale'] and params['scale'].length > 0 then %>
33     zoom = scaleToZoom(<%= params['scale'].to_f() %>);
34     <% end %>
35
36     var map = createMap("map", centre, zoom);
37
38     <% if marker %>
39     addMarkerToMap(lonLatToMercator(new OpenLayers.LonLat(<%= mlon %>, <%= mlat %>)));
40     <% end %>
41
42     map.events.register("moveend", map, updateLocation);
43     updateLocation();
44
45     document.getElementById('map_OpenLayers_ViewPort').style.position = 'absolute';
46     if ( ie6 ) {
47       handleResize();
48     }
49   }        
50
51   function updateLocation() {
52     var lonlat = mercatorToLonLat(map.getCenter());
53     var zoom = map.getZoom();
54
55     updatelinks(lonlat.lon, lonlat.lat, zoom);
56   }
57
58   function getStyle( el, property ) {
59     var style;
60     if( el.currentStyle ) {
61         style = el.currentStyle[property];
62     } else if( window.getComputedStyle ) {
63         style = document.defaultView.getComputedStyle(el,null).getPropertyValue(property);
64     } else {
65         style = el.style[property];
66     }
67     return style;
68   }
69   
70   function handleResize() {
71     var el = document.getElementById( 'map' );
72     var left = getStyle( el, 'left' );
73     var top = getStyle( el, 'top' );
74     var bottom = getStyle( el, 'bottom' );
75     var right = getStyle( el, 'right' );
76     var width = el.old_width;
77     if( ! width ) {
78         width = getStyle( el, 'width' );
79     }
80     var height = el.old_height;
81     if( ! height ) {
82         height = getStyle( el, 'height' );
83     }
84     var pwidth, pheight;
85     if( el.offsetParent.nodeName == 'BODY' || el.offsetParent.nodeName == 'HTML' ) {
86       if( typeof( window.innerWidth ) == 'number' ) {
87           pwidth = window.innerWidth;
88           height = window.innerHeight;
89       } else if( document.documentElement &&
90           ( document.documentElement.clientWidth || document.documentElement.clientHeight ) ) {
91           pwidth = document.documentElement.clientWidth;
92           pheight = document.documentElement.clientHeight;
93       } else if( document.body && ( document.body.clientWidth || document.body.clientHeight ) ) {
94           pwidth = document.body.clientWidth;
95           pheight = document.body.clientHeight;
96       }
97     } else {
98       pwidth = el.offsetParent.offsetWidth;
99       pheight = el.offsetParent.offsetHeight;
100     }
101     if( left != 'auto' && right != 'auto' && width == 'auto' ) {
102       el.old_width = width;
103       var new_width = (pwidth - el.offsetLeft - parseInt( right ) );
104       el.style.width = new_width + 'px';
105     }
106     if( top != 'auto' && bottom != 'auto' && height == 'auto' ) {
107       el.old_height = height;
108       var new_height = (pheight - el.offsetTop - parseInt( bottom ) );
109       el.style.height = new_height + 'px';
110     }
111     map.updateSize();
112     el.style.display = 'none';
113     setTimeout( function() {
114         el.style.display = '';
115         }, 200 );
116   }
117
118   if ( ie6 ) {
119     window.onresize = handleResize;
120   }
121
122   window.onload = init;
123 // -->
124 </script>