|
| 1 | +'use client' |
| 2 | + |
| 3 | +import * as React from 'react' |
| 4 | +import Map, { Marker, Popup, NavigationControl, ScaleControl, GeolocateControl, FullscreenControl } from 'react-map-gl/maplibre' |
| 5 | +import 'maplibre-gl/dist/maplibre-gl.css' |
| 6 | +import { useTheme } from 'next-themes' |
| 7 | +import type { ImageType } from '~/types' |
| 8 | +import Image from 'next/image' |
| 9 | +import Link from 'next/link' |
| 10 | +import { Card, CardContent } from '~/components/ui/card' |
| 11 | +import { Button } from '~/components/ui/button' |
| 12 | +import { ExternalLink, X } from 'lucide-react' |
| 13 | + |
| 14 | +interface MapViewProps { |
| 15 | + images: ImageType[] |
| 16 | +} |
| 17 | + |
| 18 | +export function MapView({ images }: MapViewProps) { |
| 19 | + const { resolvedTheme } = useTheme() |
| 20 | + const [popupInfo, setPopupInfo] = React.useState<ImageType | null>(null) |
| 21 | + |
| 22 | + // 过滤无效坐标并转换类型 |
| 23 | + const validImages = React.useMemo(() => { |
| 24 | + return images.filter(img => { |
| 25 | + const lat = parseFloat(img.lat || '') |
| 26 | + const lon = parseFloat(img.lon || '') |
| 27 | + return !isNaN(lat) && !isNaN(lon) && lat >= -90 && lat <= 90 && lon >= -180 && lon <= 180 |
| 28 | + }) |
| 29 | + }, [images]) |
| 30 | + |
| 31 | + // 根据主题切换地图样式 |
| 32 | + const mapStyle = React.useMemo(() => { |
| 33 | + return resolvedTheme === 'dark' |
| 34 | + ? 'https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json' |
| 35 | + : 'https://basemaps.cartocdn.com/gl/positron-gl-style/style.json' |
| 36 | + }, [resolvedTheme]) |
| 37 | + |
| 38 | + return ( |
| 39 | + <div className="w-full h-full relative"> |
| 40 | + <style jsx global>{` |
| 41 | + .map-popup .maplibregl-popup-content { |
| 42 | + padding: 0 !important; |
| 43 | + background: none !important; |
| 44 | + box-shadow: none !important; |
| 45 | + border: none !important; |
| 46 | + } |
| 47 | + .map-popup .maplibregl-popup-tip { |
| 48 | + border-bottom-color: var(--card) !important; |
| 49 | + border-top-color: var(--card) !important; |
| 50 | + border-left-color: var(--card) !important; |
| 51 | + border-right-color: var(--card) !important; |
| 52 | + } |
| 53 | + /* 针对移动端或某些情况下的关闭按钮残留 */ |
| 54 | + .map-popup .maplibregl-popup-close-button { |
| 55 | + display: none !important; |
| 56 | + } |
| 57 | + `}</style> |
| 58 | + <Map |
| 59 | + initialViewState={{ |
| 60 | + longitude: 0, |
| 61 | + latitude: 20, |
| 62 | + zoom: 1.5 |
| 63 | + }} |
| 64 | + style={{ width: '100%', height: '100%' }} |
| 65 | + mapStyle={mapStyle} |
| 66 | + attributionControl={true} |
| 67 | + > |
| 68 | + <GeolocateControl position="top-left" /> |
| 69 | + <FullscreenControl position="top-left" /> |
| 70 | + <NavigationControl position="top-left" /> |
| 71 | + <ScaleControl /> |
| 72 | + |
| 73 | + {validImages.map((image) => { |
| 74 | + const lat = parseFloat(image.lat!) |
| 75 | + const lon = parseFloat(image.lon!) |
| 76 | + |
| 77 | + return ( |
| 78 | + <Marker |
| 79 | + key={image.id} |
| 80 | + longitude={lon} |
| 81 | + latitude={lat} |
| 82 | + anchor="bottom" |
| 83 | + onClick={(e) => { |
| 84 | + // 阻止事件冒泡,避免点击 Marker 时地图同时也响应 |
| 85 | + e.originalEvent.stopPropagation() |
| 86 | + setPopupInfo(image) |
| 87 | + }} |
| 88 | + > |
| 89 | + <div |
| 90 | + className="group relative cursor-pointer transform transition-all duration-300 hover:scale-110 hover:z-10" |
| 91 | + title={image.title || 'View Photo'} |
| 92 | + > |
| 93 | + <div className="relative h-10 w-10 overflow-hidden rounded-full border-2 border-white bg-white shadow-lg dark:border-gray-800 dark:bg-gray-800"> |
| 94 | + <Image |
| 95 | + src={image.preview_url || image.url || ''} |
| 96 | + alt={image.title || 'Photo'} |
| 97 | + fill |
| 98 | + className="object-cover" |
| 99 | + sizes="40px" |
| 100 | + /> |
| 101 | + </div> |
| 102 | + {/* 箭头装饰 */} |
| 103 | + <div className="absolute -bottom-1 left-1/2 h-3 w-3 -translate-x-1/2 rotate-45 border-r-2 border-b-2 border-white bg-white dark:border-gray-800 dark:bg-gray-800"></div> |
| 104 | + </div> |
| 105 | + </Marker> |
| 106 | + ) |
| 107 | + })} |
| 108 | + |
| 109 | + {popupInfo && ( |
| 110 | + <Popup |
| 111 | + anchor="top" |
| 112 | + longitude={parseFloat(popupInfo.lon!)} |
| 113 | + latitude={parseFloat(popupInfo.lat!)} |
| 114 | + onClose={() => setPopupInfo(null)} |
| 115 | + closeButton={false} // 使用自定义关闭按钮 |
| 116 | + className="map-popup" |
| 117 | + maxWidth="300px" |
| 118 | + > |
| 119 | + <Card className="w-64 overflow-hidden border border-border bg-card shadow-xl"> |
| 120 | + <div className="relative aspect-[4/3] w-full overflow-hidden"> |
| 121 | + <Image |
| 122 | + src={popupInfo.preview_url || popupInfo.url || ''} |
| 123 | + alt={popupInfo.title || 'Photo'} |
| 124 | + fill |
| 125 | + className="object-cover" |
| 126 | + /> |
| 127 | + <Button |
| 128 | + variant="ghost" |
| 129 | + size="icon" |
| 130 | + className="absolute right-1 top-1 h-6 w-6 rounded-full bg-black/50 text-white hover:bg-black/70 z-10" |
| 131 | + onClick={(e) => { |
| 132 | + e.stopPropagation() |
| 133 | + setPopupInfo(null) |
| 134 | + }} |
| 135 | + > |
| 136 | + <X className="h-4 w-4" /> |
| 137 | + </Button> |
| 138 | + </div> |
| 139 | + <CardContent className="p-3"> |
| 140 | + <h3 className="font-semibold truncate text-sm mb-1">{popupInfo.title || 'Untitled'}</h3> |
| 141 | + {popupInfo.exif && typeof popupInfo.exif === 'object' && ( |
| 142 | + <p className="text-xs text-muted-foreground truncate"> |
| 143 | + {/* @ts-ignore */} |
| 144 | + {popupInfo.exif.model || 'Unknown Camera'} |
| 145 | + </p> |
| 146 | + )} |
| 147 | + <div className="mt-3 flex justify-end"> |
| 148 | + <Link href={`/preview/${popupInfo.id}`} passHref> |
| 149 | + <Button size="sm" variant="outline" className="h-7 text-xs"> |
| 150 | + 查看详情 <ExternalLink className="ml-1 h-3 w-3" /> |
| 151 | + </Button> |
| 152 | + </Link> |
| 153 | + </div> |
| 154 | + </CardContent> |
| 155 | + </Card> |
| 156 | + </Popup> |
| 157 | + )} |
| 158 | + </Map> |
| 159 | + </div> |
| 160 | + ) |
| 161 | +} |
0 commit comments