388d625f15039aea6734bb782910b8e6b6612135
[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: new OpenLayers.Projection("EPSG:4326")
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                         projection: new OpenLayers.Projection("EPSG:4326")
60                 });
61         } else {
62                 pois = new OpenLayers.Layer.Vector("KML", {
63                         protocol: new OpenLayers.Protocol.HTTP({
64                                 url: options.kmlurl,
65                                 format: new OpenLayers.Format.KML({
66                                         extractStyles: true,
67                                         extractAttributes: true
68                                 })
69                         }),
70                         strategies: [new OpenLayers.Strategy.Fixed()],
71                         projection: new OpenLayers.Projection("EPSG:4326")
72                 });
73         }
74         map.addLayer(pois);
75         select = new OpenLayers.Control.SelectFeature(pois);
76         map.addControl(select);
77         select.activate();
78
79         pois.events.on({
80                 "featureselected": function (event) {
81                         var feature = event.feature;
82                         var content = '<h2><a href="' + feature.attributes.href + '">' +feature.attributes.name + "</a></h2>" + feature.attributes.description;
83                         popup = new OpenLayers.Popup.FramedCloud("chicken",
84                                 feature.geometry.getBounds().getCenterLonLat(),
85                                 new OpenLayers.Size(100,100),
86                                 content,
87                                 null, true, function () {select.unselectAll()});
88                         feature.popup = popup;
89                         map.addPopup(popup);
90                 },
91                 "featureunselected": function (event) {
92                         var feature = event.feature;
93                         if (feature.popup) {
94                                 map.removePopup(feature.popup);
95                                 feature.popup.destroy();
96                                 delete feature.popup;
97                         }
98                 }
99         });
100
101         if (options.editable) {
102                 vlayer = new OpenLayers.Layer.Vector( "Editable" );
103                 map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
104                 map.addLayer(vlayer);
105         }
106
107         if (options.fullscreen) {
108                 map.addControl(new OpenLayers.Control.PanZoomBar());
109                 map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));
110                 map.addControl(new OpenLayers.Control.MousePosition());
111                 map.addControl(new OpenLayers.Control.KeyboardDefaults());
112         } else {
113                 map.addControl(new OpenLayers.Control.ZoomPanel());
114         }
115
116         //Set start centrepoint and zoom    
117         if (!options.lat || !options.lon) {
118                 options.lat = urlParams['lat'];
119                 options.lon = urlParams['lon'];
120         }
121         if (!options.zoom) {
122                 options.zoom = urlParams['zoom'];
123         }
124         if (options.lat && options.lon) {
125                 var lat = options.lat;
126                 var lon = options.lon;
127                 var zoom= options.zoom || 10;
128                 center = new OpenLayers.LonLat( lon, lat ).transform(
129                         new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
130                         map.getProjectionObject() // to Spherical Mercator Projection
131                 );
132                 map.setCenter (center, zoom);
133         } else {
134                 pois.events.register("loadend", this, function () { map.zoomToExtent(pois.getDataExtent()); });
135         }
136 }