map.js 3.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102
  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-blue.png',
  11. iconUrl: '../assets/leaflet/images/marker-icon.png',
  12. iconSize: [25, 41],
  13. iconAnchor: [12, 41],
  14. popupAnchor: [0, -28]
  15. });
  16. var seederIcon = L.icon({
  17. iconUrl: '../assets/leaflet/images/marker-icon-red.png',
  18. iconSize: [25, 41],
  19. iconAnchor: [12, 41],
  20. popupAnchor: [0, -28]
  21. });
  22. // Create map
  23. var map = L.map('map', {scrollWheelZoom: false}).setView([defaults.lat,defaults.lng], defaults.zoom);
  24. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  25. attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
  26. maxZoom: 18
  27. }).addTo(map);
  28. // Get JSON
  29. var GeoJsonPath = $('#map').data('json')
  30. $.getJSON(GeoJsonPath, function(data){
  31. function buildPopupContent(feature, layer) {
  32. feature.properties.popupContent = '';
  33. if (feature.properties.name) {
  34. feature.properties.popupContent += '<h2>#'+feature.id+': '+feature.properties.name+'</h2>';
  35. }
  36. else {
  37. feature.properties.popupContent += '<h2>#'+feature.id+'</h2>';
  38. }
  39. if (feature.properties.place) {
  40. feature.properties.popupContent += '<ul>';
  41. if (feature.properties.place.hasOwnProperty('floor')) feature.properties.popupContent += '<li>Étage: '+feature.properties.place.floor+'</li>';
  42. if (feature.properties.place.orientations[0]) feature.properties.popupContent += '<li>Orientation: '+feature.properties.place.orientations.join(', ')+'</li>';
  43. if (feature.properties.place.roof) feature.properties.popupContent += '<li>Accès au toit'+'</li>';
  44. feature.properties.popupContent += '</ul>';
  45. }
  46. if (feature.properties.comment) {
  47. feature.properties.popupContent += '<p>'+feature.properties.comment+'</p>';
  48. }
  49. layer.bindPopup(feature.properties.popupContent);
  50. }
  51. function drawSemiCircles(feature, layer) {
  52. if (feature.properties.place) {
  53. feature.properties.place.angles.map(function(angles) {
  54. // Strangely enough, we need to invert the coordinates.
  55. L.circle([feature.geometry.coordinates[1],
  56. feature.geometry.coordinates[0]], 150, {
  57. startAngle: angles[0],
  58. stopAngle: angles[1]
  59. }).addTo(map);
  60. });
  61. }
  62. }
  63. // Add to map
  64. var featureLayer = L.geoJson(data, {
  65. onEachFeature: function(feature, layer) {
  66. buildPopupContent(feature, layer);
  67. drawSemiCircles(feature, layer); },
  68. pointToLayer: function(feature, latlng) {
  69. var icon;
  70. if (feature.properties.contrib_type == 'connect') {
  71. icon = leecherIcon;
  72. } else {
  73. icon = seederIcon;
  74. }
  75. return L.marker(latlng, {icon: icon});
  76. }
  77. }).addTo(map);
  78. // Auto Zoom
  79. // Strange leaflet bug, we need to set a null timeout
  80. setTimeout(function () {
  81. map.fitBounds(featureLayer.getBounds())
  82. }, 2);
  83. });
  84. });