]> sipb.mit.edu Git - ikiwiki.git/blob - underlays/osm/ikiwiki/osm.js
multiple osm fixes
[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
41         map.addLayer(new OpenLayers.Layer.OSM());
42         if (options.format == 'CSV') {
43                 pois = new OpenLayers.Layer.Text( "CSV",
44                         { location: options.csvurl,
45                           projection: map.displayProjection
46                         });
47         } else if (options.format == 'GeoJSON') {
48                 pois = new OpenLayers.Layer.Vector("GeoJSON", {
49                         protocol: new OpenLayers.Protocol.HTTP({
50                                 url: options.jsonurl,
51                                 format: new OpenLayers.Format.GeoJSON()
52                         }),
53                         strategies: [new OpenLayers.Strategy.Fixed()]
54                 });
55         } else {
56                 pois = new OpenLayers.Layer.Vector("KML", {
57                         protocol: new OpenLayers.Protocol.HTTP({
58                                 url: options.kmlurl,
59                                 format: new OpenLayers.Format.KML({
60                                         extractStyles: true,
61                                         extractAttributes: true
62                                 })
63                         }),
64                 strategies: [new OpenLayers.Strategy.Fixed()]});
65         }
66         map.addLayer(pois);
67         select = new OpenLayers.Control.SelectFeature(pois);
68         map.addControl(select);
69         select.activate();
70
71         pois.events.on({
72                 "featureselected": function (event) {
73                         var feature = event.feature;
74                         var content = '<h2><a href="' + feature.attributes.href + '">' +feature.attributes.name + "</a></h2>" + feature.attributes.description;
75                         popup = new OpenLayers.Popup.FramedCloud("chicken",
76                                 feature.geometry.getBounds().getCenterLonLat(),
77                                 new OpenLayers.Size(100,100),
78                                 content,
79                                 null, true, function () {select.unselectAll()});
80                         feature.popup = popup;
81                         map.addPopup(popup);
82                 },
83                 "featureunselected": function (event) {
84                         var feature = event.feature;
85                         if (feature.popup) {
86                                 map.removePopup(feature.popup);
87                                 feature.popup.destroy();
88                                 delete feature.popup;
89                         }
90                 }
91         });
92
93         if (options.editable) {
94                 vlayer = new OpenLayers.Layer.Vector( "Editable" );
95                 map.addControl(new OpenLayers.Control.EditingToolbar(vlayer));
96                 map.addLayer(vlayer);
97         }
98
99         if (options.fullscreen) {
100                 map.addControl(new OpenLayers.Control.PanZoomBar());
101                 map.addControl(new OpenLayers.Control.LayerSwitcher({'ascending':false}));
102                 map.addControl(new OpenLayers.Control.MousePosition());
103                 map.addControl(new OpenLayers.Control.KeyboardDefaults());
104         } else {
105                 map.addControl(new OpenLayers.Control.ZoomPanel());
106         }
107
108         //Set start centrepoint and zoom    
109         if (!options.lat || !options.lon) {
110                 options.lat = urlParams['lat'];
111                 options.lon = urlParams['lon'];
112         }
113         if (!options.zoom) {
114                 options.zoom = urlParams['zoom'];
115         }
116         if (options.lat && options.lon) {
117                 var lat = options.lat;
118                 var lon = options.lon;
119                 var zoom= options.zoom || 10;
120                 center = new OpenLayers.LonLat( lon, lat ).transform(
121                         new OpenLayers.Projection("EPSG:4326"), // transform from WGS 1984
122                         map.getProjectionObject() // to Spherical Mercator Projection
123                 );
124                 map.setCenter (center, zoom);
125         } else {
126                 pois.events.register("loadend", this, function () { map.zoomToExtent(pois.getDataExtent()); });
127         }
128 }