'use client' import React, { createContext, useContext, useRef, useState, ReactNode } from 'react' import type { Map } from 'maplibre-gl' // 定义MapContext的类型 interface MapContextType { mapRef: React.RefObject setMap: (map: Map) => void flyTo: (options: { center: [number, number]; zoom: number; duration?: number }) => void zoomIn: () => void zoomOut: () => void zoomTo: (zoom: number) => void reset: () => void isMapReady: boolean } // 创建Context const MapContext = createContext(undefined) // Provider组件的Props类型 interface MapProviderProps { children: ReactNode } // MapProvider组件 export function MapProvider({ children }: MapProviderProps) { const mapRef = useRef(null) const [isMapReady, setIsMapReady] = useState(false) const setMap = (map: Map) => { mapRef.current = map; setIsMapReady(true); } const flyTo = (options: { center: [number, number]; zoom: number; duration?: number }) => { if (mapRef.current) { mapRef.current.flyTo({ ...options, duration: options.duration || 1000 }) } } const zoomIn = () => { if (mapRef.current) { mapRef.current.zoomIn() } } const zoomOut = () => { if (mapRef.current) { mapRef.current.zoomOut() } } const zoomTo = (zoom: number) => { if (mapRef.current) { mapRef.current.zoomTo(zoom) } } const reset = () => { if (mapRef.current) { mapRef.current.flyTo({ center: [103.851959, 1.290270], zoom: 11, duration: 1000 }) } } const value: MapContextType = { mapRef, setMap, flyTo, zoomIn, zoomOut, zoomTo, reset, isMapReady } return ( {children} ) } // 自定义Hook用于使用MapContext export function useMap() { const context = useContext(MapContext) if (context === undefined) { throw new Error('useMap必须在MapProvider内部使用') } return context } // 便捷的Hook,直接返回map实例 export function useMapInstance() { const { mapRef } = useMap() return mapRef.current }