map.js 3.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108
  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. if (feature.properties.name) {
  40. feature.properties.popupContent += '<h2>#'+feature.id+': '+feature.properties.name+'</h2>';
  41. }
  42. else {
  43. feature.properties.popupContent += '<h2>#'+feature.id+'</h2>';
  44. }
  45. if (feature.properties.place) {
  46. feature.properties.popupContent += '<ul>';
  47. if (feature.properties.place.hasOwnProperty('floor')) feature.properties.popupContent += '<li>Étage: '+feature.properties.place.floor+'</li>';
  48. if (feature.properties.place.orientations[0]) feature.properties.popupContent += '<li>Orientation: '+feature.properties.place.orientations.join(', ')+'</li>';
  49. if (feature.properties.place.roof) feature.properties.popupContent += '<li>Accès au toit'+'</li>';
  50. feature.properties.popupContent += '</ul>';
  51. }
  52. if (feature.properties.comment) {
  53. feature.properties.popupContent += '<p>'+feature.properties.comment+'</p>';
  54. }
  55. layer.bindPopup(feature.properties.popupContent);
  56. }
  57. function drawSemiCircles(feature, layer) {
  58. if (feature.properties.place) {
  59. feature.properties.place.angles.map(function(angles) {
  60. // Strangely enough, we need to invert the coordinates.
  61. L.circle([feature.geometry.coordinates[1],
  62. feature.geometry.coordinates[0]], 150, {
  63. startAngle: angles[0],
  64. stopAngle: angles[1]
  65. }).addTo(map);
  66. });
  67. }
  68. }
  69. // Add to map
  70. var featureLayer = L.geoJson(data, {
  71. onEachFeature: function(feature, layer) {
  72. buildPopupContent(feature, layer);
  73. drawSemiCircles(feature, layer); },
  74. pointToLayer: function(feature, latlng) {
  75. var icon;
  76. if (feature.properties.contrib_type == 'connect') {
  77. icon = leecherIcon;
  78. } else {
  79. icon = seederIcon;
  80. }
  81. return L.marker(latlng, {icon: icon});
  82. }
  83. }).addTo(map);
  84. // Auto Zoom
  85. // Strange leaflet bug, we need to set a null timeout
  86. setTimeout(function () {
  87. map.fitBounds(featureLayer.getBounds())
  88. }, 2);
  89. });
  90. });