map.js 3.1 KB

1234567891011121314151617181920212223242526272829303132333435363738394041424344454647484950515253545556575859606162636465666768697071727374757677787980818283
  1. $( document ).ready(function() {
  2. // Defaults
  3. defaults = {
  4. lat: 47.218371,
  5. lng: -1.553621,
  6. zoom: 13,
  7. }
  8. // Create map
  9. var map = L.map('map', {scrollWheelZoom: false}).setView([defaults.lat,defaults.lng], defaults.zoom);
  10. L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', {
  11. 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>',
  12. maxZoom: 18
  13. }).addTo(map);
  14. // Get JSON
  15. var GeoJsonPath = $('#map').data('json')
  16. $.getJSON(GeoJsonPath, function(data){
  17. function buildPopupContent(feature, layer) {
  18. feature.properties.popupContent = '';
  19. if (feature.properties.name) {
  20. feature.properties.popupContent += '<h2>#'+feature.id+': '+feature.properties.name+'</h2>';
  21. }
  22. else {
  23. feature.properties.popupContent += '<h2>#'+feature.id+'</h2>';
  24. }
  25. if (feature.properties.place) {
  26. feature.properties.popupContent += '<ul>';
  27. if (feature.properties.place.hasOwnProperty('floor')) feature.properties.popupContent += '<li>Étage: '+feature.properties.place.floor+'</li>';
  28. if (feature.properties.place.orientations[0]) feature.properties.popupContent += '<li>Orientation: '+feature.properties.place.orientations.join(', ')+'</li>';
  29. if (feature.properties.place.roof) feature.properties.popupContent += '<li>Accès au toit'+'</li>';
  30. feature.properties.popupContent += '</ul>'
  31. }
  32. if (feature.properties.comment) {
  33. feature.properties.popupContent += '<p>'+feature.properties.comment+'</p>';
  34. }
  35. layer.bindPopup(feature.properties.popupContent);
  36. }
  37. function drawSemiCircles(feature, layer) {
  38. orientations_to_angle = {
  39. 'N': 0,
  40. 'NO': -45,
  41. 'O': -90,
  42. 'SO': -135,
  43. 'S': 180,
  44. 'SE': 135,
  45. 'E': 90,
  46. 'NE': 45
  47. };
  48. if (feature.properties.place) {
  49. feature.properties.place.orientations.map(function(orien) {
  50. // Strangely enough, we need to invert the coordinates.
  51. L.circle([feature.geometry.coordinates[1],
  52. feature.geometry.coordinates[0]], 150)
  53. .setDirection(orientations_to_angle[orien], 45)
  54. .addTo(map);
  55. });
  56. }
  57. }
  58. // Add to map
  59. var featureLayer = L.geoJson(data, {
  60. onEachFeature: function(feature, layer) {
  61. buildPopupContent(feature, layer);
  62. drawSemiCircles(feature, layer); }
  63. }).addTo(map);
  64. // Auto Zoom
  65. // Strange leaflet bug, we need to set a null timeout
  66. setTimeout(function () {
  67. map.fitBounds(featureLayer.getBounds())
  68. }, 2);
  69. });
  70. });