map.js 5.7 KB

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