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.

71 lines
2.7 KiB

4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
4 months ago
  1. import { Controller } from '@hotwired/stimulus';
  2. import 'leaflet';
  3. export default class extends Controller {
  4. static values = {
  5. geojson: String,
  6. icon: String,
  7. }
  8. connect() {
  9. const simpleIcon = L.icon({
  10. iconUrl: this.iconValue,
  11. iconSize: [16, 16],
  12. iconAnchor: [8, 16],
  13. popupAnchor: [0, 0],
  14. });
  15. const iconHtml = `
  16. <svg xmlns="http://www.w3.org/2000/svg" width="16" height="16" class="bi bi-geo-alt-fill" viewBox="0 0 16 16">
  17. <path fill="currentColor" d="M8 16s6-5.686 6-10A6 6 0 0 0 2 6c0 4.314 6 10 6 10m0-7a3 3 0 1 1 0-6 3 3 0 0 1 0 6"/>
  18. </svg>
  19. `;
  20. const icons = {
  21. 'danger': L.divIcon({ html: iconHtml, className: 'svg-icon text-danger', iconSize: [16, 16], iconAnchor: [8, 16], }),
  22. 'warning': L.divIcon({ html: iconHtml, className: 'svg-icon text-warning', iconSize: [16, 16], iconAnchor: [8, 16], }),
  23. 'success': L.divIcon({ html: iconHtml, className: 'svg-icon text-success', iconSize: [16, 16], iconAnchor: [8, 16], }),
  24. };
  25. var map = L.map(this.element);
  26. L.tileLayer('https://tile.openstreetmap.org/{z}/{x}/{y}.png', {
  27. maxZoom: 19,
  28. attribution: '&copy; <a href="http://www.openstreetmap.org/copyright">OpenStreetMap</a>'
  29. }).addTo(map);
  30. var layer = L.featureGroup();
  31. var geojsons = JSON.parse(this.geojsonValue);
  32. if (geojsons.length > 0) {
  33. geojsons.forEach(function (geojson) {
  34. const feature0 = geojson.features[0].properties;
  35. const polygon = L.geoJSON(geojson, {
  36. style: function (feature) {
  37. var color = 'blue';
  38. switch (feature.properties.color) {
  39. case 'danger' : color = '#dc3545'; break
  40. case 'warning' : color = '#ffc107'; break
  41. case 'success' : color = '#198754'; break
  42. }
  43. return {color: color};
  44. }
  45. }).bindTooltip(feature0.name).addTo(layer).on('click', function (event) {
  46. window.location.href = event.layer.feature.properties.url;
  47. });
  48. L.marker(polygon.getBounds().getCenter(), {
  49. icon: icons[feature0.color],
  50. title: feature0.name,
  51. clickUrl: feature0.url,
  52. }).addTo(layer).on('click', function (event) {
  53. window.location.href = event.target.options.clickUrl;
  54. });
  55. });
  56. layer.addTo(map);
  57. map.fitBounds(layer.getBounds());
  58. }
  59. }
  60. }