Skip to content

Self-hosted personal gallery application with online photo management and albums, supporting Live/Motion Photos, EXIF parsing, geolocation recognition, and an explore map.

fix(map): 修复地图反复加载问题

Timothy Yind892ce616fe9
diff --git a/app/components/map/ClusterPin.vue b/app/components/map/ClusterPin.vue
index 3a0df1bf..0ef6374e 100644
--- a/app/components/map/ClusterPin.vue
+++ b/app/components/map/ClusterPin.vue
@@ -8,9 +8,11 @@ const props = withDefaults(
defineProps<{
clusterPoint: ClusterPoint
clusterCount?: number
+ markerId?: string
}>(),
{
clusterCount: 6,
+ markerId: undefined,
},
)
@@ -46,7 +48,7 @@ const sizeDelta = computed(() => {
<template>
<MapboxDefaultMarker
:key="`marker-cluster-${representativePhoto.id}`"
- :marker-id="`marker-cluster-${representativePhoto.id}`"
+ :marker-id="`marker-cluster-${markerId || representativePhoto.id}`"
:lnglat="props.clusterPoint.geometry.coordinates"
:options="{}"
>
diff --git a/app/components/map/SinglePhotoPin.vue b/app/components/map/SinglePhotoPin.vue
index 8b157446..d0d2a523 100644
--- a/app/components/map/SinglePhotoPin.vue
+++ b/app/components/map/SinglePhotoPin.vue
@@ -8,9 +8,11 @@ const props = withDefaults(
defineProps<{
clusterPoint: ClusterPoint
isSelected?: boolean
+ markerId?: string
}>(),
{
isSelected: false,
+ markerId: undefined,
},
)
@@ -31,7 +33,7 @@ const onClick = () => {
<MapboxDefaultMarker
ref="markerRef"
:key="`marker-single-${marker.id}`"
- :marker-id="`marker-single-${marker.id}`"
+ :marker-id="`marker-single-${markerId || marker.id}`"
:lnglat="props.clusterPoint.geometry.coordinates"
:options="{}"
>
diff --git a/app/components/photo/Viewer.vue b/app/components/photo/Viewer.vue
index 75014bac..007c06be 100644
--- a/app/components/photo/Viewer.vue
+++ b/app/components/photo/Viewer.vue
@@ -648,7 +648,9 @@ const swiperModules = [Navigation, Keyboard, Virtual]
</span>
<span v-else>
{{
- isMobile ? $t('viewer.hint.mobile') : $t('viewer.hint.desktop')
+ isMobile
+ ? $t('viewer.hint.mobile')
+ : $t('viewer.hint.desktop')
}}
</span>
</motion.div>
@@ -686,31 +688,27 @@ const swiperModules = [Navigation, Keyboard, Virtual]
</div>
<!-- 缩略图导航 -->
- <Suspense>
- <GalleryThumbnail
- :current-index="currentIndex"
- :photos="photos"
- @index-change="emit('indexChange', $event)"
- />
- </Suspense>
+ <GalleryThumbnail
+ :current-index="currentIndex"
+ :photos="photos"
+ @index-change="emit('indexChange', $event)"
+ />
</div>
<!-- EXIF 面板 - 在桌面端始终显示,在移动端根据状态显示 -->
- <Suspense>
- <AnimatePresence v-if="isMobile">
- <InfoPanel
- v-if="showExifPanel && currentPhoto"
- :current-photo="currentPhoto"
- :exif-data="currentPhoto?.exif"
- :on-close="() => (showExifPanel = false)"
- />
- </AnimatePresence>
+ <AnimatePresence v-if="isMobile">
<InfoPanel
- v-else-if="currentPhoto"
+ v-if="showExifPanel && currentPhoto"
:current-photo="currentPhoto"
:exif-data="currentPhoto?.exif"
+ :on-close="() => (showExifPanel = false)"
/>
- </Suspense>
+ </AnimatePresence>
+ <InfoPanel
+ v-else-if="currentPhoto"
+ :current-photo="currentPhoto"
+ :exif-data="currentPhoto?.exif"
+ />
</div>
</motion.div>
</AnimatePresence>
diff --git a/app/pages/explore.vue b/app/pages/explore.vue
index d63c7c30..65b52116 100644
--- a/app/pages/explore.vue
+++ b/app/pages/explore.vue
@@ -9,7 +9,7 @@ useHead({
const route = useRoute()
const router = useRouter()
-const MAPID = 'explore-map'
+// const MAPID = 'explore-map'
const { photos } = usePhotos()
const photosWithLocation = computed(() => {
@@ -146,15 +146,19 @@ const onMapLoaded = (map: any) => {
if (photoId && typeof photoId === 'string') {
const photo = photosWithLocation.value.find((photo) => photo.id === photoId)
if (photo && photo.latitude && photo.longitude) {
- currentClusterPointId.value = photoId
- nextTick(() => {
+ setTimeout(() => {
map.flyTo({
center: [photo.longitude, photo.latitude],
zoom: 15,
essential: true,
duration: 2000,
})
- })
+ setTimeout(() => {
+ nextTick(() => {
+ currentClusterPointId.value = photoId
+ })
+ }, 2000)
+ }, 600)
}
}
@@ -190,6 +194,17 @@ const resetMap = () => {
duration: 1000,
})
}
+
+const generateRandomKey = () => {
+ return Math.random().toString(36).substring(2, 15)
+}
+
+onBeforeRouteLeave(() => {
+ if (mapInstance.value) {
+ mapInstance.value.remove()
+ mapInstance.value = null
+ }
+})
</script>
<template>
@@ -243,50 +258,63 @@ const resetMap = () => {
:transition="{ duration: 0.6, delay: 0.1 }"
class="w-full h-full"
>
- <MapboxMap
- class="w-full h-full"
- :map-id="MAPID"
- :options="{
- style: 'mapbox://styles/hoshinosuzumi/cmev0eujf01dw01pje3g9cmlg',
- zoom: mapViewState.zoom,
- center: [mapViewState.longitude, mapViewState.latitude],
- config: {
- basemap: {
- lightPreset: 'night',
- colorThemes: 'faded',
+ <ClientOnly>
+ <MapboxMap
+ class="w-full h-full"
+ :map-id="generateRandomKey()"
+ :options="{
+ style: 'mapbox://styles/hoshinosuzumi/cmev0eujf01dw01pje3g9cmlg',
+ zoom: mapViewState.zoom,
+ center: [mapViewState.longitude, mapViewState.latitude],
+ config: {
+ basemap: {
+ lightPreset: 'night',
+ colorThemes: 'faded',
+ },
},
- },
- attributionControl: false,
- language: $i18n.locale,
- }"
- @load="onMapLoaded"
- @zoom="onMapZoom"
- >
- <!-- Cluster pins -->
- <template v-if="!!mapInstance">
- <LazyMapClusterPin
- v-for="clusterPoint in clusterGroups"
- :key="`cluster-${clusterPoint.properties.marker?.id}`"
- :cluster-point="clusterPoint"
- @click="onMarkerPinClick"
- @close="onMarkerPinClose"
- />
- </template>
-
- <!-- Single photo pins -->
- <template v-if="!!mapInstance">
- <LazyMapSinglePhotoPin
- v-for="clusterPoint in singleMarkers"
- :key="`single-${clusterPoint.properties.marker?.id}`"
- :cluster-point="clusterPoint"
- :is-selected="
- clusterPoint.properties.marker?.id === currentClusterPointId
- "
- @click="onMarkerPinClick"
- @close="onMarkerPinClose"
- />
+ attributionControl: false,
+ language: $i18n.locale,
+ }"
+ @load="onMapLoaded"
+ @zoom="onMapZoom"
+ >
+ <!-- Cluster pins -->
+ <template v-if="!!mapInstance">
+ <MapClusterPin
+ v-for="clusterPoint in clusterGroups"
+ :key="`cluster-${clusterPoint.properties.marker?.id}`"
+ :cluster-point="clusterPoint"
+ :marker-id="generateRandomKey()"
+ @click="onMarkerPinClick"
+ @close="onMarkerPinClose"
+ />
+ </template>
+
+ <!-- Single photo pins -->
+ <template v-if="!!mapInstance">
+ <MapSinglePhotoPin
+ v-for="clusterPoint in singleMarkers"
+ :key="`single-${clusterPoint.properties.marker?.id}`"
+ :cluster-point="clusterPoint"
+ :is-selected="
+ clusterPoint.properties.marker?.id === currentClusterPointId
+ "
+ :marker-id="generateRandomKey()"
+ @click="onMarkerPinClick"
+ @close="onMarkerPinClose"
+ />
+ </template>
+ </MapboxMap>
+
+ <template #fallback>
+ <div class="w-full h-full flex items-center justify-center">
+ <Icon
+ name="tabler:map-pin-off"
+ class="size-10 text-gray-500 animate-pulse"
+ />
+ </div>
</template>
- </MapboxMap>
+ </ClientOnly>
</motion.div>
</div>
</template>