leaflet.js 1.8 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152
  1. import { loadResource } from "../../static/utils/resources.js";
  2. export default {
  3. template: "<div></div>",
  4. props: {
  5. map_options: Object,
  6. resource_path: String,
  7. },
  8. async mounted() {
  9. await this.$nextTick(); // NOTE: wait for window.path_prefix to be set
  10. await Promise.all([
  11. loadResource(window.path_prefix + `${this.resource_path}/leaflet/leaflet.css`),
  12. loadResource(window.path_prefix + `${this.resource_path}/leaflet/leaflet.js`),
  13. ]);
  14. if (this.map_options.drawControl) {
  15. await Promise.all([
  16. loadResource(window.path_prefix + `${this.resource_path}/leaflet-draw/leaflet.draw.css`),
  17. loadResource(window.path_prefix + `${this.resource_path}/leaflet-draw/leaflet.draw.js`),
  18. ]);
  19. }
  20. this.map = L.map(this.$el, this.map_options);
  21. for (const type of ["click", "dblclick", "mousedown", "mouseup", "mouseover", "mouseout", "mousemove"]) {
  22. this.map.on(type, (e) => {
  23. this.$emit(`map-${type}`, {
  24. type: type,
  25. latlng: e.latlng,
  26. layerPoint: e.layerPoint,
  27. containerPoint: e.containerPoint,
  28. });
  29. });
  30. }
  31. this.map.on("moveend", (e) => this.$emit("moveend", e.target.getCenter()));
  32. this.map.on("zoomend", (e) => this.$emit("zoomend", e.target.getZoom()));
  33. if (this.map_options.drawControl) {
  34. var drawnItems = new L.FeatureGroup();
  35. this.map.addLayer(drawnItems);
  36. }
  37. const connectInterval = setInterval(async () => {
  38. if (window.socket.id === undefined) return;
  39. this.$emit("init", { socket_id: window.socket.id });
  40. clearInterval(connectInterval);
  41. }, 100);
  42. },
  43. updated() {
  44. this.map.setView(L.latLng(this.map_options.center.lat, this.map_options.center.lng), this.map_options.zoom);
  45. },
  46. methods: {
  47. add_layer(layer) {
  48. L[layer.type](...layer.args).addTo(this.map);
  49. },
  50. },
  51. };