modules/freifunk: OSM in basics fixed thanks to Andreas Pittrich
[project/luci.git] / modules / freifunk / htdocs / luci-static / resources / osm.js
1 var map;
2 var layer_mapnik;
3 var layer_tah;
4 var layer_markers;
5 var PI = Math.PI;
6 var latfield = '';
7 var lonfield = '';
8 var latfield_id='';
9 var lonfield_id='';
10 var centerlon = 10;
11 var centerlat = 52;
12 var zoom = 6;
13
14 function lon2merc(lon) {
15     return 20037508.34 * lon / 180;
16 }
17
18 function lat2merc(lat) {
19         lat = Math.log(Math.tan( (90 + lat) * PI / 360)) / PI;
20         return 20037508.34 * lat;
21 }
22
23 function merc2lon(lon) {
24         return lon*180/20037508.34;
25 };
26
27 function merc2lat(lat) {
28         return Math.atan(Math.exp(lat*PI/20037508.34))*360/PI-90;
29 };
30
31 OpenLayers.Control.Click = OpenLayers.Class(OpenLayers.Control, {
32         defaultHandlerOptions: {
33                 'single': true,
34                 'double': false,
35                 'pixelTolerance': 0,
36                 'stopSingle': false,
37                 'stopDouble': false
38         },
39
40         initialize: function(options) {
41                 this.handlerOptions = OpenLayers.Util.extend(
42                         {}, this.defaultHandlerOptions
43                 );
44                 OpenLayers.Control.prototype.initialize.apply(
45                         this, arguments
46                 );
47                         this.handler = new OpenLayers.Handler.Click(
48                                 this, {
49                                         'click': this.trigger
50                         }, this.handlerOptions
51                 );
52         }, 
53
54         trigger: function(e) {
55                 var lonlat = map.getLonLatFromViewPortPx(e.xy); 
56                 lat=merc2lat(lonlat.lat);
57                 lon=merc2lon(lonlat.lon);
58                 if(parent.document.getElementById(latfield_id)==null){
59                         latfield=document.getElementById('osmlat');
60                 }else{
61                         latfield=parent.document.getElementById(latfield_id);
62                 }
63                 if(parent.document.getElementById(lonfield_id)==null){
64                         lonfield=document.getElementById('osmlon');
65                 }else{
66                         lonfield=parent.document.getElementById(lonfield_id);
67                 }
68                 latfield.value = lat;
69                 lonfield.value = lon;                                                           
70         }
71 });
72
73 function init(){                        
74         var field = window.name.substring(0, window.name.lastIndexOf("."));
75         if(parent.document.getElementById(field+".latfield")!=null){
76                 latfield_id = parent.document.getElementById(field+".latfield").value;  
77                 document.getElementById('osm').style.display="none";
78         }
79         if(parent.document.getElementById(field+".lonfield")!=null){
80                 lonfield_id = parent.document.getElementById(field+".lonfield").value;
81         }
82         if(parent.document.getElementById(field+".centerlat")!=null){
83                 centerlat =parseFloat(parent.document.getElementById(field+".centerlat").value);
84         }
85         if(parent.document.getElementById(field+".centerlon")!=null){
86                 centerlon = parseFloat(parent.document.getElementById(field+".centerlon").value);
87         }
88         if(parent.document.getElementById(field+".zoom")!=null){
89                 zoom = parseFloat(parent.document.getElementById(field+".zoom").value);
90         }
91 }
92
93 function drawmap() {
94         OpenLayers.Lang.setCode('de'); 
95         mapdiv=document.getElementById('map');
96         mapdiv.style.height=window.innerHeight+"px";
97         mapdiv.style.width=window.innerWidth+"px";
98         map = new OpenLayers.Map('map', {
99                 projection: new OpenLayers.Projection("EPSG:900913"),
100                 displayProjection: new OpenLayers.Projection("EPSG:4326"),
101                 controls: [
102                         new OpenLayers.Control.MouseDefaults(),
103                         new OpenLayers.Control.PanZoomBar()],
104                 maxExtent:
105                         new OpenLayers.Bounds(-20037508.34,-20037508.34, 20037508.34, 20037508.34),
106                 numZoomLevels: 18,
107                 maxResolution: 156543,
108                 units: 'meters'
109         });
110
111         layer_mapnik = new OpenLayers.Layer.OSM.Mapnik("Mapnik");
112
113         map.addLayers([layer_mapnik]);
114         var y =lat2merc(centerlat);
115         var x =lon2merc(centerlon);
116         map.setCenter(new OpenLayers.LonLat(x, y), zoom);
117         
118         // Check for geolocation support
119         if(navigator.geolocation){
120                 navigator.geolocation.getCurrentPosition(function(position){
121                         var y =lat2merc(position.coords.latitude);
122                         var x =lon2merc(position.coords.longitude);
123                         map.setCenter(new OpenLayers.LonLat(x, y), '17');
124                 });
125         }
126
127         var click = new OpenLayers.Control.Click();
128         map.addControl(click);
129         click.activate();
130 }