2019-03-22 17:35:07 +01:00
|
|
|
<template>
|
2022-07-12 10:55:28 +02:00
|
|
|
<div class="map-container">
|
2020-02-18 08:57:00 +01:00
|
|
|
<l-map
|
|
|
|
:zoom="mergedOptions.zoom"
|
|
|
|
:style="`height: ${mergedOptions.height}; width: ${mergedOptions.width}`"
|
|
|
|
class="leaflet-map"
|
|
|
|
:center="[lat, lon]"
|
|
|
|
@click="clickMap"
|
|
|
|
@update:zoom="updateZoom"
|
2021-08-12 10:54:55 +02:00
|
|
|
:options="{ zoomControl: false }"
|
2022-09-20 16:53:26 +02:00
|
|
|
ref="mapComponent"
|
|
|
|
@ready="onMapReady"
|
2020-02-18 08:57:00 +01:00
|
|
|
>
|
2022-07-12 10:55:28 +02:00
|
|
|
<l-tile-layer :url="tiles?.endpoint" :attribution="attribution">
|
2020-11-30 10:24:11 +01:00
|
|
|
</l-tile-layer>
|
2021-08-12 10:54:55 +02:00
|
|
|
<l-control-zoom
|
|
|
|
position="topleft"
|
|
|
|
:zoomInTitle="$t('Zoom in')"
|
|
|
|
:zoomOutTitle="$t('Zoom out')"
|
|
|
|
></l-control-zoom>
|
2020-02-18 08:57:00 +01:00
|
|
|
<l-marker
|
|
|
|
:lat-lng="[lat, lon]"
|
|
|
|
@add="openPopup"
|
|
|
|
@update:latLng="updateDraggableMarkerPosition"
|
|
|
|
:draggable="!readOnly"
|
|
|
|
>
|
2022-09-20 16:53:26 +02:00
|
|
|
<l-icon>
|
|
|
|
<MapMarker :size="48" class="text-mbz-purple" />
|
|
|
|
</l-icon>
|
|
|
|
<l-popup v-if="popupMultiLine" :options="{ offset: new Point(22, 8) }">
|
2020-11-30 10:24:11 +01:00
|
|
|
<span v-for="line in popupMultiLine" :key="line"
|
|
|
|
>{{ line }}<br
|
|
|
|
/></span>
|
2020-02-18 08:57:00 +01:00
|
|
|
</l-popup>
|
|
|
|
</l-marker>
|
|
|
|
</l-map>
|
2022-09-20 16:53:26 +02:00
|
|
|
<CrosshairsGps ref="locationIcon" class="hidden" />
|
2020-02-18 08:57:00 +01:00
|
|
|
</div>
|
2019-03-22 17:35:07 +01:00
|
|
|
</template>
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
<script lang="ts" setup>
|
2022-09-20 16:53:26 +02:00
|
|
|
import {
|
|
|
|
LatLng,
|
|
|
|
LeafletMouseEvent,
|
|
|
|
LeafletEvent,
|
|
|
|
Control,
|
|
|
|
DomUtil,
|
|
|
|
Map,
|
|
|
|
Point,
|
|
|
|
} from "leaflet";
|
2020-02-18 08:57:00 +01:00
|
|
|
import "leaflet/dist/leaflet.css";
|
2021-08-12 10:54:55 +02:00
|
|
|
import {
|
|
|
|
LMap,
|
|
|
|
LTileLayer,
|
|
|
|
LMarker,
|
|
|
|
LPopup,
|
|
|
|
LIcon,
|
|
|
|
LControlZoom,
|
2022-07-12 10:55:28 +02:00
|
|
|
} from "@vue-leaflet/vue-leaflet";
|
|
|
|
import { computed, nextTick, onMounted, ref } from "vue";
|
|
|
|
import { useMapTiles } from "@/composition/apollo/config";
|
|
|
|
import { useI18n } from "vue-i18n";
|
2022-09-20 16:53:26 +02:00
|
|
|
import Locatecontrol from "leaflet.locatecontrol";
|
|
|
|
import CrosshairsGps from "vue-material-design-icons/CrosshairsGps.vue";
|
|
|
|
import MapMarker from "vue-material-design-icons/MapMarker.vue";
|
2022-07-12 10:55:28 +02:00
|
|
|
|
|
|
|
const props = withDefaults(
|
|
|
|
defineProps<{
|
|
|
|
readOnly?: boolean;
|
|
|
|
coords: string;
|
|
|
|
marker?: { text: string | string[]; icon: string };
|
|
|
|
options?: Record<string, unknown>;
|
|
|
|
updateDraggableMarkerCallback?: (latlng: LatLng, zoom: number) => void;
|
|
|
|
}>(),
|
|
|
|
{
|
|
|
|
readOnly: true,
|
2019-03-22 17:35:07 +01:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
);
|
|
|
|
|
|
|
|
const defaultOptions: {
|
|
|
|
zoom: number;
|
|
|
|
height: string;
|
|
|
|
width: string;
|
|
|
|
} = {
|
|
|
|
zoom: 15,
|
|
|
|
height: "100%",
|
|
|
|
width: "100%",
|
|
|
|
};
|
|
|
|
|
|
|
|
const zoom = ref(defaultOptions.zoom);
|
|
|
|
|
2022-09-20 16:53:26 +02:00
|
|
|
const mapComponent = ref();
|
|
|
|
const mapObject = ref<Map>();
|
|
|
|
const locateControl = ref<Control.Locate>();
|
|
|
|
const locationIcon = ref();
|
|
|
|
|
|
|
|
const locationIconHTML = computed(() => locationIcon.value?.$el.innerHTML);
|
|
|
|
|
|
|
|
onMounted(async () => {
|
2022-07-12 10:55:28 +02:00
|
|
|
// this part resolve an issue where the markers would not appear
|
2022-09-20 16:53:26 +02:00
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
// eslint-disable-next-line no-underscore-dangle
|
2022-07-12 10:55:28 +02:00
|
|
|
// delete Icon.Default.prototype._getIconUrl;
|
|
|
|
// Icon.Default.mergeOptions({
|
|
|
|
// iconRetinaUrl: require("leaflet/dist/images/marker-icon-2x.png"),
|
|
|
|
// iconUrl: require("leaflet/dist/images/marker-icon.png"),
|
|
|
|
// shadowUrl: require("leaflet/dist/images/marker-shadow.png"),
|
|
|
|
// });
|
|
|
|
});
|
|
|
|
|
2022-09-20 16:53:26 +02:00
|
|
|
const onMapReady = async () => {
|
|
|
|
mapObject.value = mapComponent.value.leafletObject;
|
|
|
|
mountLocateControl();
|
|
|
|
};
|
|
|
|
|
|
|
|
const mountLocateControl = () => {
|
|
|
|
if (canDoGeoLocation.value && mapObject.value) {
|
|
|
|
// eslint-disable-next-line @typescript-eslint/ban-ts-comment
|
|
|
|
// @ts-ignore
|
|
|
|
locateControl.value = new Locatecontrol({
|
|
|
|
strings: { title: t("Show me where I am") as string },
|
|
|
|
position: "topleft",
|
|
|
|
drawCircle: false,
|
|
|
|
drawMarker: false,
|
|
|
|
createButtonCallback(container: HTMLElement | undefined, options: any) {
|
|
|
|
const link = DomUtil.create(
|
|
|
|
"a",
|
|
|
|
"leaflet-bar-part leaflet-bar-part-single",
|
|
|
|
container
|
|
|
|
);
|
|
|
|
link.title = options.strings.title;
|
|
|
|
link.href = "#";
|
|
|
|
link.setAttribute("role", "button");
|
|
|
|
|
|
|
|
const icon = DomUtil.create(
|
|
|
|
"span",
|
|
|
|
"material-design-icon rss-icon",
|
|
|
|
link
|
|
|
|
);
|
|
|
|
icon.setAttribute("aria-hidden", "true");
|
|
|
|
icon.setAttribute("role", "img");
|
|
|
|
icon.insertAdjacentHTML("beforeend", locationIconHTML.value);
|
|
|
|
return { link, icon };
|
|
|
|
},
|
|
|
|
...props.options,
|
|
|
|
}) as Control.Locate;
|
|
|
|
locateControl.value?.addTo(mapObject.value);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const openPopup = async (event: LeafletEvent): Promise<void> => {
|
|
|
|
await nextTick();
|
|
|
|
event.target.openPopup();
|
|
|
|
};
|
|
|
|
|
|
|
|
const mergedOptions = computed((): Record<string, unknown> => {
|
|
|
|
return { ...defaultOptions, ...props.options };
|
|
|
|
});
|
|
|
|
|
|
|
|
const lat = computed((): number => {
|
|
|
|
return Number.parseFloat(props.coords?.split(";")[1]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const lon = computed((): number => {
|
|
|
|
return Number.parseFloat(props.coords.split(";")[0]);
|
|
|
|
});
|
|
|
|
|
|
|
|
const popupMultiLine = computed((): Array<string | undefined> => {
|
|
|
|
if (Array.isArray(props.marker?.text)) {
|
|
|
|
return props.marker?.text as string[];
|
2019-11-08 19:37:14 +01:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
return [props.marker?.text];
|
|
|
|
});
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const clickMap = (event: LeafletMouseEvent): void => {
|
|
|
|
updateDraggableMarkerPosition(event.latlng);
|
|
|
|
};
|
2019-04-03 17:29:03 +02:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const updateDraggableMarkerPosition = (e: LatLng): void => {
|
|
|
|
if (props.updateDraggableMarkerCallback) {
|
|
|
|
props.updateDraggableMarkerCallback(e, zoom.value);
|
2020-02-18 08:57:00 +01:00
|
|
|
}
|
2022-07-12 10:55:28 +02:00
|
|
|
};
|
2020-02-18 08:57:00 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const updateZoom = (newZoom: number): void => {
|
|
|
|
zoom.value = newZoom;
|
|
|
|
};
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const { tiles } = useMapTiles();
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const { t } = useI18n({ useScope: "global" });
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const attribution = computed((): string => {
|
|
|
|
return tiles.value?.attribution ?? t("© The OpenStreetMap Contributors");
|
|
|
|
});
|
2019-11-08 19:37:14 +01:00
|
|
|
|
2022-07-12 10:55:28 +02:00
|
|
|
const canDoGeoLocation = computed((): boolean => {
|
|
|
|
return window.isSecureContext;
|
|
|
|
});
|
2019-03-22 17:35:07 +01:00
|
|
|
</script>
|
2019-04-03 17:29:03 +02:00
|
|
|
<style lang="scss" scoped>
|
2020-02-18 08:57:00 +01:00
|
|
|
div.map-container {
|
2021-11-08 11:03:02 +01:00
|
|
|
height: 100%;
|
2020-02-18 08:57:00 +01:00
|
|
|
width: 100%;
|
2019-04-03 17:29:03 +02:00
|
|
|
|
2020-02-18 08:57:00 +01:00
|
|
|
.leaflet-map {
|
|
|
|
z-index: 20;
|
|
|
|
}
|
|
|
|
}
|
2019-04-03 17:29:03 +02:00
|
|
|
</style>
|
2022-09-20 16:53:26 +02:00
|
|
|
<style>
|
|
|
|
@import "leaflet.locatecontrol/dist/L.Control.Locate.css";
|
|
|
|
</style>
|