644d4b57381a045f86e7c88029630c13ee266ba2
[ikiwiki.git] / underlays / osm / ikiwiki / osm.js
1 // taken from http://stackoverflow.com/questions/901115/get-query-string-values-in-javascript
2 var urlParams = {};
3 (function () {
4         var e,
5         a = /\\+/g,  // Regex for replacing addition symbol with a space
6         r = /([^&=]+)=?([^&]*)/g,
7         d = function (s) { return decodeURIComponent(s.replace(a, " ")); },
8         q = window.location.search.substring(1);
9
10         while (e = r.exec(q))
11         urlParams[d(e[1])] = d(e[2]);
12 })();
13
14 function mapsetup(divname, options) {
15         div = document.getElementById(divname);
16         if (options.fullscreen) {
17                 permalink = 'permalink';
18                 div.style.top = 0;
19                 div.style.left = 0;
20                 div.style.position = 'absolute';
21                 div.style.width = '100%';
22                 div.style.height = '100%';
23         }
24         else {
25                 div.style.height = options.height;
26                 div.style.width = options.width;
27                 div.style.float = options.float;
28                 permalink = {base: options.href, title: "View larger map"};
29         }
30         map = new OpenLayers.Map(divname, {
31                 controls: [
32                         new OpenLayers.Control.Navigation(),
33                         new OpenLayers.Control.ScaleLine(),
34                         new OpenLayers.Control.Permalink(permalink)
35                 ],
36                 displayProjection: new OpenLayers.Projection("EPSG:4326"),
37                 numZoomLevels: 18
38         });
39
40         if (options.mapurl) {
41                 var newLayer = new OpenLayers.Layer.OSM("Local Tiles", options.mapurl, {numZoomLevels: 19, isBaseLayer: true});
42                 map.addLayer(newLayer);
43         } else {
44                 map.addLayer(new OpenLayers.Layer.OSM());
45         }
46
47         if (options.format == 'CSV') {
48                 pois = new OpenLayers.Layer.Text( "CSV",
49                         { location: options.csvurl,
50                           projection: map.displayProjection
51                         });
52         } else if (options.format == 'GeoJSON') {
53                 pois = new OpenLayers.Layer.Vector("GeoJSON", {
54                         protocol: new OpenLayers.Protocol.HTTP({
55                                 url: options.jsonurl,
56                                 format: new OpenLayers.Format.GeoJSON()
57                         }),
58                         strategies: [new OpenLayers.Strategy.Fixed()]
59                 });
60         } else {
61                 pois = new OpenLayers.Layer.Vector("KML", {
62                         protocol: new OpenLayers.Protocol.HTTP({
63                                 url: options.kmlurl,
64                                 format: new OpenLayers.Format.KML({
65                                         extractStyles: true,
66                                         extractAttributes: true
67                                 })
68                         }),
69                 strategies: [new OpenLayers.Strategy.Fixed()]});
70         }
71         map.addLayer(pois);
72         select = new OpenLayers.Control.SelectFeature(pois);
73         map.addControl(select);
74         select.activate();
75
76         pois.events.on({
77                 "featureselected": function (event) {
78                         var feature = event.feature;
79                         var content = '<h2><a href="' + feature.attributes.href + '">' +feature.attributes.name + "</a></h2>" + feature.attributes.description;
80                         popup = new OpenLayers.Popup.FramedCloud("chicken",
81                                 feature.geometry.getBounds().getCenterLonLat(),
82                                 new OpenLayers.Size(100,100),
83                                 content,
84                                 null, true, function () {select.unselectAll()});
85                         feature.popup = popup;
86                         map.addPopup(popup);
87                 },
88                 "featureunselected": function (event) {
89                         var feature = event.feature;
90                         if (feature.popup) {
91                                 map.removePopup(feature.popup);
92                                 feature.popup.destroy();
93                                 delete feature.popup;
94                         }
95                 }
96         });
97
98         if (options.editable) {
99                 vlayer = new OpenLayers.Layer.Vector( "Editable" );
100                 map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
101                 map.addLayer(vlayer);
102         }
103
104         if (options.fullscreen) {
105                 map.addControl(new OpenLayers.Control.PanZoomBar());
106                 map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));
107                 map.addControl(new OpenLayers.Control.MousePosition());
108                 map.addControl(new OpenLayers.Control.KeyboardDefaults());
109         } else {
110                 map.addControl(new OpenLayers.Control.ZoomPanel());
111         }
112
113         //Set start centrepoint and zoom    
114         if (!options.lat || !options.lon) {
115                 options.lat = urlParams['lat'];
116                 options.lon = urlParams['lon'];
117         }
118         if (!options.zoom) {
119                 options.zoom = urlParams['zoom'];
120         }
121         if (options.lat && options.lon) {
122                 var lat = options.lat;
123                 var lon = options.lon;
124                 var zoom= options.zoom || 10;
125                 center = new OpenLayers.LonLat( lon, lat ).transform(
126                         new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
127                         map.getProjectionObject() // to Spherical Mercator Projection
128                 );
129                 map.setCenter (center, zoom);
130         } else {
131                 pois.events.register("loadend", this, function () { map.zoomToExtent(pois.getDataExtent()); });
132         }
133 }