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