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