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