Skip to content
This repository has been archived by the owner on Jun 10, 2024. It is now read-only.

Commit

Permalink
fix(types): correct typings for LMap
Browse files Browse the repository at this point in the history
Signed-off-by: GitHub <[email protected]>
  • Loading branch information
ferferga authored Mar 3, 2024
1 parent 43b329f commit dcdbebe
Show file tree
Hide file tree
Showing 3 changed files with 65 additions and 43 deletions.
5 changes: 3 additions & 2 deletions package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

5 changes: 3 additions & 2 deletions package.json
Original file line number Diff line number Diff line change
Expand Up @@ -38,9 +38,9 @@
"date-fns": "3.3.1",
"defu": "6.1.4",
"destr": "2.0.3",
"leaflet": "1.9.4",
"radix-vue": "1.4.9",
"vue": "3.4.21",
"leaflet": "1.9.4",
"vue-i18n": "9.9.1",
"vue-router": "4.3.0"
},
Expand All @@ -50,10 +50,11 @@
"@intlify/unplugin-vue-i18n": "2.0.0",
"@rollup/plugin-virtual": "3.0.2",
"@stylistic/eslint-plugin": "1.6.2",
"@types/geojson": "7946.0.14",
"@types/leaflet": "1.9.8",
"@types/node": "20.11.22",
"@typescript-eslint/eslint-plugin": "7.1.0",
"@typescript-eslint/parser": "7.1.0",
"@types/leaflet": "1.9.8",
"@vitejs/plugin-vue": "5.0.4",
"browserslist": "4.23.0",
"confusing-browser-globals": "1.0.11",
Expand Down
98 changes: 59 additions & 39 deletions src/components/LMap.vue
Original file line number Diff line number Diff line change
@@ -1,11 +1,13 @@
<template>
<div id="mapContainer" />
<div
ref="mapContainer"
class="mapContainer" />
</template>

<script setup lang="ts">
import { onMounted, onBeforeUnmount } from 'vue';
import { onBeforeUnmount, ref, watch } from 'vue';
import 'leaflet/dist/leaflet.css';
import L from 'leaflet';
import { map, icon, marker } from 'leaflet';
import Azul from '@/assets/pin/Pin_Azul.png';
import Dot from '@/assets/pin/dot.png';
Expand All @@ -17,45 +19,55 @@ interface MapMarker {
}
const props = defineProps<{ markers: Array<MapMarker> }>();
let map: ReturnType<typeof L> ;
let userLocationMarker: ReturnType<typeof L.marker>;
let watchId: number | null = null;
onMounted(() => {
console.log(props.markers);
createMapLayer();
});
const mapContainer = ref<HTMLDivElement>();
let mapInstance: ReturnType<typeof map> | undefined;
let watchId: number | undefined;
onBeforeUnmount(() => {
if (map) {
map.remove();
}
disposeMap();
if (watchId !== null) {
if (watchId !== undefined) {

Check notice

Code scanning / CodeQL

Unneeded defensive code Note

This guard always evaluates to false.
navigator.geolocation.clearWatch(watchId);
}
});
/**
* Coloca los marcadores en el mapa
*/
function setMarkers(): void {
if (mapInstance) {
for (const mapMarker of props.markers) {
const customIcon = L.icon({

Check failure on line 40 in src/components/LMap.vue

View workflow job for this annotation

GitHub Actions / Lint 🔬

'L' is not defined

Check failure on line 40 in src/components/LMap.vue

View workflow job for this annotation

GitHub Actions / Typecheck 🈯

'L' refers to a UMD global, but the current file is a module. Consider adding an import instead.
iconUrl: Azul,
iconSize: [22, 30],
iconAnchor: [11, 6]
});
marker([mapMarker.latitud, mapMarker.longitud], { icon: customIcon }).addTo(mapInstance).bindPopup(`Evento: ${mapMarker.description}`);
}
}
};
const createMapLayer = (): void => {
/**
* Crea la instancia de Leaflet e inicia la geolocalización
*/
function createMapLayer(): void {
if (navigator.geolocation) {
navigator.geolocation.getCurrentPosition(
(position) => {
const { latitude, longitude } = position.coords;
map = L.map('mapContainer').setView([latitude, longitude], 13);
L.tileLayer('https://{s}.tile.osm.org/{z}/{x}/{y}.png', {
attribution:
'&copy; <a href="https://osm.org/copyright">OpenStreetMap</a> contributors'
}).addTo(map);
mapInstance = map('mapContainer');
mapInstance.setView([latitude, longitude], 15);
const userIcon = L.icon({
const userIcon = icon({
iconUrl: Dot,
iconSize: [20, 20],
iconAnchor: [10, 10]
});
userLocationMarker = L.marker([latitude, longitude], { icon: userIcon }).addTo(map);
const userLocationMarker = marker([latitude, longitude], { icon: userIcon }).addTo(mapInstance);
userLocationMarker.bindPopup('Tu ubicación');
if (props.markers.length > 0) {
Expand All @@ -72,23 +84,31 @@ const createMapLayer = (): void => {
}
};
const setMarkers = (): void => {
for (const marker of props.markers) {
const customIcon = L.icon({
iconUrl: Azul,
iconSize: [22, 30],
iconAnchor: [11, 6]});
L.marker([marker.latitud, marker.longitud], { icon: customIcon }).addTo(map)
.bindPopup('Evento:'+marker.description);
/**
* Dispose the map instance
*/
function disposeMap(): void {
if (mapInstance) {
mapInstance.remove();
mapInstance = undefined;
}
};
}
/**
* Este watcher trackea cuando el elemento div cambia para crear el mapa (ahora mismo no cambia nunca
* solo en mount, pero es posible que en un futuro, dependiendo del dispositivo, cambiemos el objeto DOM
* al que haga referencia con un `<component :is="" ....>`)
*/
watch(mapContainer, () => {
disposeMap();
createMapLayer();
});
</script>

<style scoped>
#mapContainer {
width: 95%;
height: 90vh;
margin: 2% 2%;
}
</style>
.mapContainer {
width: 95%;
height: 90vh;
margin: 2% 2%;
}
</style>

0 comments on commit dcdbebe

Please sign in to comment.