map.js 5.0 KB

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