]> sipb.mit.edu Git - ikiwiki.git/blob - underlays/osm/ikiwiki/osm.js
Correct minor spelling error
[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                 maxExtent: new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
38                 projection: "EPSG:900913",
39                 units: "m",
40                 maxResolution: 156543.0339,
41                 numZoomLevels: 19
42         });
43
44         for (x in options.layers) {
45                 layer = options.layers[x];
46                 console.log("setting up layer: " + layer);
47                 if (layer.indexOf("Google") >= 0) {
48                         if (options.google_apikey && options.google_apikey != 'null') {
49                                 var gtype = G_NORMAL_MAP;
50                                 if (layer.indexOf("Satellite") >= 0) {
51                                         gtype = G_SATELLITE_MAP;
52                                 } else if (layer.indexOf("Hybrid") >= 0) {
53                                         gtype = G_HYBRID_MAP // the normal map overlaying the satellite photographs
54                                 } else if (layer.indexOf("Physical") >= 0) {
55                                         gtype = G_PHYSICAL_MAP // terrain information
56                                 }
57                                 // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html
58                                 googleLayer = new OpenLayers.Layer.Google(
59                                         layer,
60                                         {type: gtype,
61                                          'sphericalMercator': true,
62                                          'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
63                                          projection: new OpenLayers.Projection("EPSG:3857")}
64                                 );
65                                 map.addLayer(googleLayer);
66                         } else {
67                                 console.log("no API key defined for Google layer, skipping");
68                         }
69                 } else if (layer == 'OSM') { // OSM default layer
70                         map.addLayer(new OpenLayers.Layer.OSM("OSM (Mapnik)"));
71                 } else { // assumed to be a URL
72                         text = layer.match(/([^.\/]*\.[^.\/]*(\/[^\$]*)?)\/.*$/i) // take the first two parts of the FQDN and everything before the first $
73                         map.addLayer(new OpenLayers.Layer.OSM("OSM (" + text[1]  + ")", layer));
74                 }
75         }
76
77         if (options.format == 'CSV') {
78                 pois = new OpenLayers.Layer.Text( "CSV",
79                         { location: options.csvurl,
80                           projection: new OpenLayers.Projection("EPSG:4326")
81                         });
82         } else if (options.format == 'GeoJSON') {
83                 pois = new OpenLayers.Layer.Vector("GeoJSON", {
84                         protocol: new OpenLayers.Protocol.HTTP({
85                                 url: options.jsonurl,
86                                 format: new OpenLayers.Format.GeoJSON()
87                         }),
88                         strategies: [new OpenLayers.Strategy.Fixed()],
89                         projection: new OpenLayers.Projection("EPSG:4326")
90                 });
91         } else {
92                 pois = new OpenLayers.Layer.Vector("KML", {
93                         protocol: new OpenLayers.Protocol.HTTP({
94                                 url: options.kmlurl,
95                                 format: new OpenLayers.Format.KML({
96                                         extractStyles: true,
97                                         extractAttributes: true
98                                 })
99                         }),
100                         strategies: [new OpenLayers.Strategy.Fixed()],
101                         projection: new OpenLayers.Projection("EPSG:4326")
102                 });
103         }
104         map.addLayer(pois);
105         select = new OpenLayers.Control.SelectFeature(pois);
106         map.addControl(select);
107         select.activate();
108
109         pois.events.on({
110                 "featureselected": function (event) {
111                         var feature = event.feature;
112                         var content = '<h2><a href="' + feature.attributes.href + '">' +feature.attributes.name + "</a></h2>" + feature.attributes.description;
113                         popup = new OpenLayers.Popup.FramedCloud("chicken",
114                                 feature.geometry.getBounds().getCenterLonLat(),
115                                 new OpenLayers.Size(100,100),
116                                 content,
117                                 null, true, function () {select.unselectAll()});
118                         feature.popup = popup;
119                         map.addPopup(popup);
120                 },
121                 "featureunselected": function (event) {
122                         var feature = event.feature;
123                         if (feature.popup) {
124                                 map.removePopup(feature.popup);
125                                 feature.popup.destroy();
126                                 delete feature.popup;
127                         }
128                 }
129         });
130
131         if (options.editable) {
132                 vlayer = new OpenLayers.Layer.Vector( "Editable" );
133                 map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
134                 map.addLayer(vlayer);
135         }
136
137         if (options.fullscreen) {
138                 map.addControl(new OpenLayers.Control.PanZoomBar());
139                 map.addControl(new OpenLayers.Control.LayerSwitcher());
140                 map.addControl(new OpenLayers.Control.MousePosition());
141                 map.addControl(new OpenLayers.Control.KeyboardDefaults());
142         } else {
143                 map.addControl(new OpenLayers.Control.ZoomPanel());
144         }
145
146         //Set start centrepoint and zoom    
147         if (!options.lat || !options.lon) {
148                 options.lat = urlParams['lat'];
149                 options.lon = urlParams['lon'];
150         }
151         if (!options.zoom) {
152                 options.zoom = urlParams['zoom'];
153         }
154         if (options.lat && options.lon) {
155                 var lat = options.lat;
156                 var lon = options.lon;
157                 var zoom= options.zoom || 10;
158                 center = new OpenLayers.LonLat( lon, lat ).transform(
159                         new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
160                         map.getProjectionObject() // to Spherical Mercator Projection
161                 );
162                 map.setCenter (center, zoom);
163         } else {
164                 pois.events.register("loadend", this, function () { map.zoomToExtent(pois.getDataExtent()); });
165         }
166 }