map.js 5.4 KB

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