]> sipb.mit.edu Git - ikiwiki.git/blob - underlays/osm/ikiwiki/osm.js
9269bd8993250581b598cfd49abcef903ac04896
[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: 18
42         });
43
44         if (options.mapurl) {
45                 var newLayer = new OpenLayers.Layer.OSM("Local Tiles", options.mapurl, {numZoomLevels: 19, isBaseLayer: true});
46                 map.addLayer(newLayer);
47         } else {
48                 map.addLayer(new OpenLayers.Layer.OSM());
49         }
50
51         // this nightmare is possible through http://docs.openlayers.org/library/spherical_mercator.html
52         if (options.google_apikey && options.google_apikey != 'null') {
53                 googleLayer = new OpenLayers.Layer.Google(
54                         "Google Hybrid",
55                         {type: G_HYBRID_MAP,
56                          'sphericalMercator': true,
57                          'maxExtent': new OpenLayers.Bounds(-20037508.34,-20037508.34,20037508.34,20037508.34),
58                          projection: new OpenLayers.Projection("EPSG:3857")}
59                 );
60                 map.addLayer(googleLayer);
61         }
62         if (options.format == 'CSV') {
63                 pois = new OpenLayers.Layer.Text( "CSV",
64                         { location: options.csvurl,
65                           projection: new OpenLayers.Projection("EPSG:4326")
66                         });
67         } else if (options.format == 'GeoJSON') {
68                 pois = new OpenLayers.Layer.Vector("GeoJSON", {
69                         protocol: new OpenLayers.Protocol.HTTP({
70                                 url: options.jsonurl,
71                                 format: new OpenLayers.Format.GeoJSON()
72                         }),
73                         strategies: [new OpenLayers.Strategy.Fixed()],
74                         projection: new OpenLayers.Projection("EPSG:4326")
75                 });
76         } else {
77                 pois = new OpenLayers.Layer.Vector("KML", {
78                         protocol: new OpenLayers.Protocol.HTTP({
79                                 url: options.kmlurl,
80                                 format: new OpenLayers.Format.KML({
81                                         extractStyles: true,
82                                         extractAttributes: true
83                                 })
84                         }),
85                         strategies: [new OpenLayers.Strategy.Fixed()],
86                         projection: new OpenLayers.Projection("EPSG:4326")
87                 });
88         }
89         map.addLayer(pois);
90         select = new OpenLayers.Control.SelectFeature(pois);
91         map.addControl(select);
92         select.activate();
93
94         pois.events.on({
95                 "featureselected": function (event) {
96                         var feature = event.feature;
97                         var content = '<h2><a href="' + feature.attributes.href + '">' +feature.attributes.name + "</a></h2>" + feature.attributes.description;
98                         popup = new OpenLayers.Popup.FramedCloud("chicken",
99                                 feature.geometry.getBounds().getCenterLonLat(),
100                                 new OpenLayers.Size(100,100),
101                                 content,
102                                 null, true, function () {select.unselectAll()});
103                         feature.popup = popup;
104                         map.addPopup(popup);
105                 },
106                 "featureunselected": function (event) {
107                         var feature = event.feature;
108                         if (feature.popup) {
109                                 map.removePopup(feature.popup);
110                                 feature.popup.destroy();
111                                 delete feature.popup;
112                         }
113                 }
114         });
115
116         if (options.editable) {
117                 vlayer = new OpenLayers.Layer.Vector( "Editable" );
118                 map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
119                 map.addLayer(vlayer);
120         }
121
122         if (options.fullscreen) {
123                 map.addControl(new OpenLayers.Control.PanZoomBar());
124                 map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));
125                 map.addControl(new OpenLayers.Control.MousePosition());
126                 map.addControl(new OpenLayers.Control.KeyboardDefaults());
127         } else {
128                 map.addControl(new OpenLayers.Control.ZoomPanel());
129         }
130
131         //Set start centrepoint and zoom    
132         if (!options.lat || !options.lon) {
133                 options.lat = urlParams['lat'];
134                 options.lon = urlParams['lon'];
135         }
136         if (!options.zoom) {
137                 options.zoom = urlParams['zoom'];
138         }
139         if (options.lat && options.lon) {
140                 var lat = options.lat;
141                 var lon = options.lon;
142                 var zoom= options.zoom || 10;
143                 center = new OpenLayers.LonLat( lon, lat ).transform(
144                         new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
145                         map.getProjectionObject() // to Spherical Mercator Projection
146                 );
147                 map.setCenter (center, zoom);
148         } else {
149                 pois.events.register("loadend", this, function () { map.zoomToExtent(pois.getDataExtent()); });
150         }
151 }