138cf744451a7ddb0e89b6fa504d41c6488b35a0
[plewww.git] / misc / googlemap.js
1 /*
2  * performs the equivalent of <body onload=create_map onunload=GUnload>
3  * needs the allSites global var that is suppoesd to have been defined 
4  * in /var/www/html/sites/plc-sites.js by plc-map.py
5  */
6
7 window.onDomReady = DomReady;
8
9 //Setup the event
10 function DomReady(fn) {
11   //W3C
12   if(document.addEventListener) {
13       document.addEventListener("DOMContentLoaded", fn, false);
14   } else {
15     //IE
16     document.onreadystatechange = function(){readyState(fn)}
17   }
18 }
19
20 //IE execute function
21 function readyState(fn) {
22   // Thierry: initial version from the internet read 
23   // if(document.readyState == "interactive") 
24   // I have noticed that 
25   // (*) on first load, I was hitting this point only once with complete
26   // (*) on reload I get here twice with "interactive" and then "complete"
27   // so as a quick'n dirty way :
28   if(document.readyState == "complete") {
29     fn();
30   }
31 }
32
33 window.onDomReady(create_map);
34
35 /* initial center */
36 centerLat=52;
37 centerLon=15;
38 /* initial zoom level */
39 initZoom=4;
40
41 function decode_utf8( s )
42 {
43   return decodeURIComponent( escape( s ) );
44 }
45
46 function create_marker (map, point, site) {
47   var marker=new GMarker(point);
48   var html='<a href="/db/sites/index.php?id=' + site.site_id + '">' + decode_utf8(site.name) + '</a>\n';
49   html += '<br><a href="/db/nodes/index.php?site_id=' + site.site_id +'">' + site.nb_nodes + ' Nodes</a>\n';
50   if (site.peername) {
51     html += '<br> <a href="/db/peers/index.php?id=' + site.peer_id + '">' + decode_utf8(site.peername) + '</a>\n';
52   }
53   /* display site name with url on info window - triggers on click */
54   GEvent.addListener(marker, 'click', function() {marker.openInfoWindowHtml(html);});
55   /* double click - clear info window */
56   GEvent.addListener(marker, 'dblclick', function() {marker.closeInfoWindow();});
57   /* required before setImage can be called */
58   map.addOverlay(marker);
59   /* set different layouts for local/foreign */
60   /* google originals are in http://maps.google.com/mapfiles/ms/icons/blue-dot.png or blue.png or ... */
61   if (!site.peername) {
62     /* local */
63     marker.setImage('/misc/google-ple.png');
64   } else {
65     marker.setImage('/misc/google-plc.png');
66   }
67   return marker;
68 }
69
70 function create_map() {
71   if (GBrowserIsCompatible()) {
72     var map = new GMap2(document.getElementById("googlemap"));
73     map.setCenter(new GLatLng(centerLat, centerLon), initZoom);
74     map.addControl(new GLargeMapControl());
75     map.addControl(new GMapTypeControl());
76     map.addControl(new GOverviewMapControl());
77     /*var geocoder = new GClientGeocoder();*/
78     map.setMapType(G_SATELLITE_MAP);
79     
80     
81     for (i=0;i<allSites.length;i++) {
82       var site=allSites[i];
83       /* discard unspecified sites, and sites with no nodes */
84       if ( (site.nb_nodes != 0) && ( (site.lat!=0.0) || (site.lon!=0.0)) ) {
85           var point = new GLatLng(site.lat,site.lon);
86           create_marker(map,point,site);
87       }
88     }
89   }
90 }
91
92 window.onunload=GUnload;
93
94