map.js 6.1 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172
  1. $( document ).ready(function() {
  2. // Defaults
  3. defaults = {
  4. lat: parseFloat($('#map').attr("start_lat")),
  5. lng: parseFloat($('#map').attr("start_lon")),
  6. zoom: $('#map').attr("start_zoom"),
  7. }
  8. // Icons
  9. var leecherIcon = L.icon({
  10. iconUrl: '../assets/leaflet/images/marker-icon.png',
  11. iconSize: [25, 41],
  12. iconAnchor: [12, 41],
  13. popupAnchor: [0, -28]
  14. });
  15. var seederIcon = L.icon({
  16. iconUrl: '../assets/leaflet/images/marker-icon-red.png',
  17. iconSize: [25, 41],
  18. iconAnchor: [12, 41],
  19. popupAnchor: [0, -28]
  20. });
  21. var anyIcon = L.icon({
  22. iconUrl: '../assets/leaflet/images/marker-icon-purple.png',
  23. iconSize: [25, 41],
  24. iconAnchor: [12, 41],
  25. popupAnchor: [0, -28]
  26. });
  27. var pointOfInterestIcon = L.icon({
  28. iconUrl: '../assets/leaflet/images/marker-icon-yellow.png',
  29. iconSize: [25, 41],
  30. iconAnchor: [12, 41],
  31. popupAnchor: [0, -28]
  32. });
  33. // Create map
  34. var map = L.map('map', {scrollWheelZoom: false}).setView([defaults.lat,defaults.lng], defaults.zoom);
  35. L.tileLayer('//{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  36. attribution: 'Map data &copy; <a href="//openstreetmap.org">OpenStreetMap</a> contributors, <a href="//creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="//mapbox.com">Mapbox</a>',
  37. maxZoom: 18
  38. }).addTo(map);
  39. // Add scale control
  40. L.control.scale({
  41. position: 'bottomleft',
  42. metric: true,
  43. imperial: false,
  44. maxWidth: 200
  45. }).addTo(map);
  46. // Get JSON
  47. var GeoJsonPath = $('#map').data('json')
  48. $.getJSON(GeoJsonPath, function(data){
  49. function buildPopupContent(feature, layer) {
  50. feature.properties.popupContent = '';
  51. var featureIdLink = '<a href="#' + feature.id + '">#' + feature.id + '</a>';
  52. if (feature.properties.name) {
  53. feature.properties.popupContent += '<h2>'+featureIdLink+': '+feature.properties.name+'</h2>';
  54. }
  55. else {
  56. feature.properties.popupContent += '<h2>'+ featureIdLink +'</h2>';
  57. }
  58. if (feature.properties.place) {
  59. feature.properties.popupContent += '<ul>';
  60. if (feature.properties.place.floor) {
  61. feature.properties.popupContent += '<li>Étage: '+feature.properties.place.floor+'</li>';
  62. }
  63. if (feature.properties.place.orientations && feature.properties.place.orientations[0]) feature.properties.popupContent += '<li>Orientation: '+feature.properties.place.orientations.join(', ')+'</li>';
  64. if (feature.properties.place.roof) feature.properties.popupContent += '<li>Accès au toit'+'</li>';
  65. feature.properties.popupContent += '</ul>';
  66. }
  67. if (feature.properties.email || feature.properties.phone) {
  68. feature.properties.popupContent += '<ul>';
  69. if (feature.properties.email) {
  70. feature.properties.popupContent += '<li> Email : '+feature.properties.email + '</li>';
  71. }
  72. if (feature.properties.phone) {
  73. feature.properties.popupContent += '<li> Phone : '+feature.properties.phone + '</li>';
  74. }
  75. }
  76. if (feature.properties.comment) {
  77. feature.properties.popupContent += '<p>'+feature.properties.comment+'</p>';
  78. }
  79. layer.bindPopup(feature.properties.popupContent);
  80. layer.id = feature.id;
  81. }
  82. function drawSemiCircles(feature, layer) {
  83. if (feature.properties.place && feature.properties.place.angles) {
  84. feature.properties.place.angles.map(function(angles) {
  85. // Strangely enough, we need to invert the coordinates.
  86. L.circle([feature.geometry.coordinates[1],
  87. feature.geometry.coordinates[0]], 150, {
  88. startAngle: angles[0],
  89. stopAngle: angles[1]
  90. }).addTo(map);
  91. });
  92. }
  93. }
  94. // Add to map
  95. var featureLayer = L.geoJson(data, {
  96. onEachFeature: function(feature, layer) {
  97. buildPopupContent(feature, layer);
  98. drawSemiCircles(feature, layer);
  99. },
  100. pointToLayer: function(feature, latlng) {
  101. var icon;
  102. if (feature.properties.contrib_type == 'connect') {
  103. icon = leecherIcon;
  104. }
  105. else if (feature.properties.contrib_type == 'share') {
  106. icon = seederIcon;
  107. }
  108. else if (feature.properties.contrib_type == 'any') {
  109. icon = anyIcon;
  110. }
  111. else if (feature.properties.contrib_type == 'pointofinterest') {
  112. icon = pointOfInterestIcon;
  113. }
  114. return L.marker(latlng, {icon: icon});
  115. }
  116. }).addTo(map);
  117. function openMarker(id) {
  118. for (var i in featureLayer._layers) {
  119. if (featureLayer._layers[i].id == id) {
  120. // Get desired marker
  121. var thisMarker = featureLayer._layers[i];
  122. // Center map on marker and zoom
  123. map.panTo(thisMarker.getLatLng());
  124. map.setZoom(16);
  125. // Open popup
  126. thisMarker.openPopup();
  127. }
  128. }
  129. }
  130. // Open popup if hash is present.
  131. if (window.location.hash) {
  132. var id = window.location.hash.slice(1);
  133. openMarker(id);
  134. }
  135. else {
  136. // Auto Zoom
  137. // Strange leaflet bug, we need to set a null timeout
  138. setTimeout(function () {
  139. map.fitBounds(featureLayer.getBounds())
  140. }, 2);
  141. }
  142. // Bind window hash change
  143. window.onhashchange = function() {
  144. var id = window.location.hash.slice(1);
  145. openMarker(id);
  146. }
  147. });
  148. });