map.js 4.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140
  1. $( document ).ready(function() {
  2. // Defaults
  3. defaults = {
  4. lat: 47.218371,
  5. lng: -1.553621,
  6. zoom: 13,
  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. // We add current time to bypass browser cache
  37. $.getJSON(GeoJsonPath + '?time=' + Date.now(), function(data){
  38. function buildPopupContent(feature, layer) {
  39. feature.properties.popupContent = '';
  40. var featureIdLink = '<a href="#' + feature.id + '">#' + feature.id + '</a>';
  41. if (feature.properties.name) {
  42. feature.properties.popupContent += '<h2>'+featureIdLink+': '+feature.properties.name+'</h2>';
  43. }
  44. else {
  45. feature.properties.popupContent += '<h2>'+ featureIdLink +'</h2>';
  46. }
  47. if (feature.properties.place) {
  48. feature.properties.popupContent += '<ul>';
  49. if (feature.properties.place.hasOwnProperty('floor')) feature.properties.popupContent += '<li>Étage: '+feature.properties.place.floor+'</li>';
  50. if (feature.properties.place.orientations[0]) feature.properties.popupContent += '<li>Orientation: '+feature.properties.place.orientations.join(', ')+'</li>';
  51. if (feature.properties.place.roof) feature.properties.popupContent += '<li>Accès au toit'+'</li>';
  52. feature.properties.popupContent += '</ul>';
  53. }
  54. if (feature.properties.comment) {
  55. feature.properties.popupContent += '<p>'+feature.properties.comment+'</p>';
  56. }
  57. layer.bindPopup(feature.properties.popupContent);
  58. layer.id = feature.id;
  59. }
  60. function drawSemiCircles(feature, layer) {
  61. if (feature.properties.place) {
  62. feature.properties.place.angles.map(function(angles) {
  63. // Strangely enough, we need to invert the coordinates.
  64. L.circle([feature.geometry.coordinates[1],
  65. feature.geometry.coordinates[0]], 150, {
  66. startAngle: angles[0],
  67. stopAngle: angles[1]
  68. }).addTo(map);
  69. });
  70. }
  71. }
  72. // Add to map
  73. var featureLayer = L.geoJson(data, {
  74. onEachFeature: function(feature, layer) {
  75. buildPopupContent(feature, layer);
  76. drawSemiCircles(feature, layer);
  77. },
  78. pointToLayer: function(feature, latlng) {
  79. var icon;
  80. if (feature.properties.contrib_type == 'connect') {
  81. icon = leecherIcon;
  82. } else {
  83. icon = seederIcon;
  84. }
  85. return L.marker(latlng, {icon: icon});
  86. }
  87. }).addTo(map);
  88. function openMarker(id) {
  89. for (var i in featureLayer._layers) {
  90. if (featureLayer._layers[i].id == id) {
  91. // Get desired marker
  92. var thisMarker = featureLayer._layers[i];
  93. // Center map on marker and zoom
  94. map.panTo(thisMarker.getLatLng());
  95. map.setZoom(16);
  96. // Open popup
  97. thisMarker.openPopup();
  98. }
  99. }
  100. }
  101. // Open popup if hash is present.
  102. if (window.location.hash) {
  103. var id = window.location.hash.substr(-1);
  104. openMarker(id);
  105. }
  106. else {
  107. // Auto Zoom
  108. // Strange leaflet bug, we need to set a null timeout
  109. setTimeout(function () {
  110. map.fitBounds(featureLayer.getBounds())
  111. }, 2);
  112. }
  113. // Bind window hash change
  114. window.onhashchange = function() {
  115. var id = window.location.hash.substr(-1);
  116. openMarker(id);
  117. }
  118. });
  119. });