map.js 4.8 KB

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