map.js 3.5 KB

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