leaflet.js 1.4 KB

123456789101112131415161718192021222324252627282930313233343536373839
  1. import { loadResource } from "../../static/utils/resources.js";
  2. export default {
  3. template: "<div></div>",
  4. props: {
  5. map_options: Object,
  6. },
  7. async mounted() {
  8. const promisses = [
  9. loadResource("https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.css"),
  10. loadResource("https://cdnjs.cloudflare.com/ajax/libs/leaflet/1.6.0/leaflet.js"),
  11. ];
  12. if (this.map_options.drawControl) {
  13. promisses.push(loadResource("https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.css"));
  14. promisses.push(loadResource("https://cdnjs.cloudflare.com/ajax/libs/leaflet.draw/1.0.4/leaflet.draw.js"));
  15. }
  16. await Promise.all(promisses);
  17. this.map = L.map(this.$el, this.map_options);
  18. this.map.on("moveend", (e) => this.$emit("moveend", e.target.getCenter()));
  19. this.map.on("zoomend", (e) => this.$emit("zoomend", e.target.getZoom()));
  20. if (this.map_options.drawControl) {
  21. var drawnItems = new L.FeatureGroup();
  22. this.map.addLayer(drawnItems);
  23. }
  24. const connectInterval = setInterval(async () => {
  25. if (window.socket.id === undefined) return;
  26. this.$emit("init", { socket_id: window.socket.id });
  27. clearInterval(connectInterval);
  28. }, 100);
  29. },
  30. updated() {
  31. this.map.setView(L.latLng(this.map_options.center.lat, this.map_options.center.lng), this.map_options.zoom);
  32. },
  33. methods: {
  34. add_layer(layer) {
  35. L[layer.type](...layer.args).addTo(this.map);
  36. },
  37. },
  38. };