rodolpheg

Untitled

Feb 11th, 2020
309
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
text 3.84 KB | None | 0 0
  1. <!DOCTYPE html>
  2. <html>
  3. <head>
  4. <meta charset="UTF-8">
  5. <title></title>
  6. <head>
  7. <title>Un titre</title>
  8. <meta charset="UTF-8">
  9. <script src="https://unpkg.com/[email protected]/dist/leaflet.js"></script>
  10. <link rel="stylesheet" href="https://unpkg.com/[email protected]/dist/leaflet.css" />
  11. <!-- Les bibliothèques de Firebase -->
  12. <script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-app.js"></script>
  13. <script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-auth.js"></script>
  14. <script src="https://www.gstatic.com/firebasejs/7.8.1/firebase-database.js"></script>
  15. <style>
  16. html, body {height: 100%; margin: 0;}
  17. #Carte { width: 100%; height: 100%; }
  18. </style>
  19. </head>
  20. </head>
  21. <body>
  22. <div id='Carte'></div>
  23. <script>
  24.  
  25. // Ajoût de la configuration de la connexion à la base de donnée en ligne Firebase.
  26. var firebaseConfig = {
  27. apiKey: "AIzaSyDeajsI7o0U-Mo4XVSBMtuoGCS_Nn939b8",
  28. authDomain: "leaflet-3f59a.firebaseapp.com",
  29. databaseURL: "https://leaflet-3f59a.firebaseio.com",
  30. projectId: "leaflet-3f59a",
  31. storageBucket: "leaflet-3f59a.appspot.com",
  32. messagingSenderId: "841635639220",
  33. appId: "1:841635639220:web:0985db6b3ef82a43904ce1"
  34. };
  35. // initialise Firebase
  36. firebase.initializeApp(firebaseConfig);
  37. var database = firebase.database();
  38. ref = database.ref('Leaflet');
  39.  
  40. var point = {
  41. "type": "Feature",
  42. "properties": {
  43. "var": 2
  44. },
  45. "geometry": {
  46. "type": "Point",
  47. "coordinates": [
  48. -74.1796875,
  49. 49.38237278700955
  50. ]
  51. }
  52. }
  53. // Envoi de l'objet JSON (qui correspond aux spécifications GeoJSON dans ce cas particulier) vers la base de données FireBase :
  54. // ref.push(point);
  55.  
  56. // Fonction attribuant un style à l'entité qu'on lui passe
  57. function style(feature) {
  58. return {
  59. weight: 2,
  60. opacity: .9,
  61. color: feature.properties.couleur,
  62. dashArray: '3',
  63.  
  64. fillOpacity: 0.9,
  65. fillColor: feature.properties.couleur
  66. };
  67. }
  68.  
  69. function gotData (data) {
  70. var features = data.val();
  71. var keys = Object.keys(features);
  72. if (keys.length > 0) {
  73. // passe à travers toutes les entités de la base et les ajoute à la FeatureCollection :
  74. for (var i = 0; i < keys.length; i++) {
  75. console.log(features[keys[i]].geometry.type)
  76. // Puisque les cercles et ellipses ne font pas partie des spécifications GeoJSON, nous l'ajoutons cette option par l'intermédiaire d'un test de condition
  77. // cette condition signifie que "si le type de géométrie est 'Circle'", alors court-circuiter la fonction GeoJSON de Leaflet et dessiner un cercle d'une autre manière
  78. if (features[keys[i]].geometry.type == 'Circle') {
  79. L.circle(features[keys[i]].geometry.coordinates,{color: features[keys[i]].properties.couleur, radius: features[keys[i]].properties.var}).addTo(maCarte)
  80. }
  81. // si l'entité n'est pas un cercle, alors la visualiser selon les standards GeoJSON
  82. else {
  83. L.geoJSON(features[keys[i]],{style: style}).addTo(maCarte);
  84. }
  85. }
  86. }
  87. }
  88.  
  89. function errData (err) {
  90. console.log('Erreur',err);
  91. }
  92.  
  93. // créer la carte
  94. var fondDeCarte = L.tileLayer('https://api.mapbox.com/styles/v1/mapbox/outdoors-v11/tiles/{z}/{x}/{y}?access_token=pk.eyJ1IjoiZ29ucm8iLCJhIjoiY2szZzdhYzVwMDMxMjNkbjZodzA2ZG8wOSJ9.w_VkhpdmD-1pmMYcabUQwg')
  95. var maCarte = L.map('Carte', {
  96. center: [45.52,-73.63],
  97. zoom: 13,
  98. layers: fondDeCarte,
  99. zoomControl: false
  100. });
  101. maCarte.setView([0,0], 2);
  102.  
  103. ref.on('value',gotData,errData);
  104.  
  105. L.control.zoom({
  106. position:'topright'
  107. }).addTo(maCarte);
  108.  
  109. // Ajouter une échelle dynamique
  110. L.control.scale().addTo(maCarte);
  111.  
  112. </script>
  113. </body>
  114. </html>
Advertisement
Add Comment
Please, Sign In to add comment