You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

39 lines
1.5 KiB

2 months ago
  1. import { Controller } from '@hotwired/stimulus';
  2. import 'leaflet';
  3. export default class extends Controller {
  4. static values = {
  5. geojson: String,
  6. }
  7. connect() {
  8. var map = L.map(this.element);
  9. L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
  10. maxZoom: 19,
  11. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
  12. }).addTo(map);
  13. var layer = L.featureGroup();
  14. var geojsons = JSON.parse(this.geojsonValue);
  15. if (geojsons.length > 0) {
  16. geojsons.forEach(function (geojson) {
  17. const feature0 = geojson.features[0].properties;
  18. L.geoJSON(geojson, {
  19. style: function (feature) {
  20. var color = 'blue';
  21. switch (feature.properties.color) {
  22. case 'danger' : color = '#dc3545'; break
  23. case 'warning' : color = '#ffc107'; break
  24. case 'success' : color = '#198754'; break
  25. }
  26. return {color: color};
  27. }
  28. }).bindTooltip(feature0.name).addTo(layer).on('click', function (event) {
  29. window.location.href = event.layer.feature.properties.url;
  30. });
  31. });
  32. layer.addTo(map);
  33. console.log(layer.getBounds().toBBoxString());
  34. map.fitBounds(layer.getBounds());
  35. }
  36. }
  37. }