summaryrefslogtreecommitdiff
path: root/map.js
blob: 0d59bcedd631b5b3b8fbb74865f9f42652b74957 (plain)
  1. var map = null; //global map object
  2. // base config
  3. var attribOSM = '&copy; <a href="//openstreetmap.org">OpenStreetMap</a>-bidragydere, <a href="//creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>';
  4. var OSMLayer = L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { attribution: attribOSM } );
  5. var Box = L.latLngBounds( [[54.5, 8.0],[57.8, 15.3]] );
  6. // GeoJSON feature styling and grouping
  7. function typecolor(type) {
  8. if (type == "center") { return ('darkgreen'); }
  9. else if (type == 'center-begin') { return ('green'); }
  10. else if (type == 'member') { return ('orange'); }
  11. else if (type == 'diploma') { return ('red'); }
  12. else if (type == 'diploma-aspiring') { return ('blue'); }
  13. else { return ('black'); };
  14. };
  15. function returnFeatureMarker(feature, latlng) {
  16. var hasPopup = (feature.properties.location && feature.properties.type);
  17. return L.circleMarker(latlng, {
  18. title: feature.properties.title,
  19. zIndexOffset: 1000*feature.properties.priority || 0,
  20. clickable: hasPopup,
  21. keyboard: hasPopup,
  22. color: typecolor(feature.properties.type)
  23. });
  24. };
  25. function addFeaturePopup(feature, layer) {
  26. if (feature.properties.location && feature.properties.title) {
  27. layer.bindPopup('<strong>'+feature.properties.title+'</strong><br>'+feature.properties.location);
  28. }
  29. };
  30. var center = L.geoJson([],{
  31. pointToLayer: returnFeatureMarker,
  32. onEachFeature: addFeaturePopup,
  33. filter: function(feature, layer) {
  34. return (feature.properties.type == "center");
  35. }
  36. });
  37. var center_begin = L.geoJson([],{
  38. pointToLayer: returnFeatureMarker,
  39. onEachFeature: addFeaturePopup,
  40. filter: function(feature, layer) {
  41. return (feature.properties.type == "center-begin");
  42. }
  43. });
  44. var member = L.geoJson([],{
  45. pointToLayer: returnFeatureMarker,
  46. onEachFeature: addFeaturePopup,
  47. filter: function(feature, layer) {
  48. return (feature.properties.type == "member");
  49. }
  50. });
  51. var diploma = L.geoJson([],{
  52. pointToLayer: returnFeatureMarker,
  53. onEachFeature: addFeaturePopup,
  54. filter: function(feature, layer) {
  55. return (feature.properties.type == "diploma");
  56. }
  57. });
  58. var diploma_aspiring = L.geoJson([],{
  59. pointToLayer: returnFeatureMarker,
  60. onEachFeature: addFeaturePopup,
  61. filter: function(feature, layer) {
  62. return (feature.properties.type == "diploma-aspiring");
  63. }
  64. });
  65. var unknown = L.geoJson([],{
  66. pointToLayer: returnFeatureMarker,
  67. onEachFeature: addFeaturePopup,
  68. filter: function(feature, layer) {
  69. return (feature.properties.type == null);
  70. }
  71. });
  72. var overlayMaps = {
  73. "LAND Centre": center,
  74. "LAND startere": center_begin,
  75. "Medlemmer": member,
  76. "Diplomholdere": diploma,
  77. "Diplomaspiranter": diploma_aspiring,
  78. "Øvrige": unknown
  79. };
  80. function overlayCallback(data) {
  81. center.addData(data).addTo(map);
  82. center_begin.addData(data).addTo(map);
  83. member.addData(data).addTo(map);
  84. diploma.addData(data).addTo(map);
  85. diploma_aspiring.addData(data).addTo(map);
  86. unknown.addData(data);
  87. };
  88. // position popup
  89. function round(n,d) {
  90. return Math.round(Math.pow(10,d)*n)/Math.pow(10,d)
  91. };
  92. function lngLatString(latLng) {
  93. return round(latLng.lng,5) + ", " + round(latLng.lat,5)
  94. };
  95. var popup = L.popup();
  96. function positionPopup(e) {
  97. popup
  98. .setLatLng(e.latlng)
  99. .setContent("Position (long, lat):<br>" + lngLatString(e.latlng))
  100. .openOn(map);
  101. }
  102. // http://mathiasbynens.be/notes/xhr-responsetype-json
  103. var getJSON = function(url, successHandler, errorHandler) {
  104. var xhr = typeof XMLHttpRequest != 'undefined'
  105. ? new XMLHttpRequest()
  106. : new ActiveXObject('Microsoft.XMLHTTP');
  107. var supportsJSON = (function() {
  108. if (typeof XMLHttpRequest == 'undefined') {
  109. return false;
  110. }
  111. xhr.open('get', '/', true);
  112. try {
  113. // some browsers throw when setting `responseType` to an unsupported value
  114. xhr.responseType = 'json';
  115. } catch(error) {
  116. return false;
  117. }
  118. return 'response' in xhr && xhr.responseType == 'json';
  119. }());
  120. xhr.open('get', url, true);
  121. if (supportsJSON) {
  122. xhr.responseType = 'json';
  123. }
  124. xhr.onreadystatechange = function() {
  125. var status;
  126. var data;
  127. // http://xhr.spec.whatwg.org/#dom-xmlhttprequest-readystate
  128. if (xhr.readyState == 4) { // `DONE`
  129. status = xhr.status;
  130. if (status == 200) {
  131. successHandler && successHandler(
  132. supportsJSON
  133. ? xhr.response
  134. : JSON.parse(xhr.responseText)
  135. );
  136. } else {
  137. errorHandler && errorHandler(status);
  138. }
  139. }
  140. };
  141. xhr.send();
  142. };
  143. // create base map when DOM is ready, and then add features and controls
  144. function initMap() {
  145. map = L.map('content', { layers: [OSMLayer] });
  146. map.attributionControl.setPrefix(false);
  147. map.fitBounds(Box);
  148. // micro-polyfill - http://mathiasbynens.be/notes/document-head
  149. var head = document.head || document.getElementsByTagName('head')[0];
  150. // load all rel=points links in html header as GeoJSON
  151. var links = head.getElementsByTagName("link")
  152. for (i=0;i< links.length;i++) {
  153. if (links[i].rel == "points") {
  154. getJSON(links[i].href, function (data) {
  155. overlayCallback(data);
  156. });
  157. }
  158. };
  159. L.control.layers([],overlayMaps).addTo(map);
  160. L.control.scale({ imperial: false }).addTo(map);
  161. map.on('contextmenu', positionPopup);
  162. };
  163. yepnope([
  164. {
  165. test : window.JSON,
  166. // https://code.google.com/p/json-sans-eval/
  167. // (json2: less secure; json3: larger - unneeded features)
  168. nope : 'json-minified.js',
  169. }, {
  170. test: document.addEventListener,
  171. nope: 'EventListener.js',
  172. complete: function() {
  173. document.addEventListener("DOMContentLoaded", initMap, false)
  174. }
  175. }
  176. ]);