map.js 2.1 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758
  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.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. feature.properties.popupContent += '</ul>'
  30. }
  31. if (feature.properties.comment) {
  32. feature.properties.popupContent += '<p>'+feature.properties.comment+'</p>';
  33. }
  34. layer.bindPopup(feature.properties.popupContent);
  35. }
  36. // Add to map
  37. var featureLayer = L.geoJson(data, {
  38. onEachFeature: buildPopupContent
  39. }).addTo(map);
  40. // Auto Zoom
  41. // Strange leaflet bug, we need to set a null timeout
  42. setTimeout(function () {
  43. map.fitBounds(featureLayer.getBounds())
  44. }, 2);
  45. });
  46. });