本指南说明如何将大屏幕地图从Leaflet替换为高德地图(AMap),实现国内地图展示功能。
src/client/big-shadcn/components/ChinaAMap.tsx - 高德地图组件src/client/big-shadcn/components/AMapLoader.tsx - 地图加载器src/client/big-shadcn/data/chinaCities.ts - 中国城市数据src/client/big-shadcn/hooks/useChinaMapData.ts - 中国地图数据Hooksrc/client/big-shadcn/config/mapConfig.ts - 地图配置src/client/big-shadcn/pages/HomePage.tsx - 替换地图组件在项目根目录创建或修改 .env 文件:
VITE_GAODE_MAP_KEY=您的高德地图API密钥
如果暂时无法获取API密钥,可以使用默认配置测试:
// 在 mapConfig.ts 中修改
export const mapConfig = {
gaodeApiKey: '您的高德地图API密钥' // 替换为实际密钥
};
interface ChinaCityData {
code: string; // 城市代码
name: string; // 城市名称
lat: number; // 纬度
lng: number; // 经度
province: string; // 所属省份
dailyNewJobs?: number; // 每日新增岗位
totalJobs?: number; // 累计岗位
dailyNewTalents?: number; // 每日新增人才
totalTalents?: number; // 累计人才
}
import ChinaAMap from '../components/ChinaAMap';
import { AMapLoader } from '../components/AMapLoader';
<AMapLoader apiKey={mapConfig.gaodeApiKey}>
<ChinaAMap data={cities} />
</AMapLoader>
// 修改地图配置
export const mapConfig = {
gaodeApiKey: '您的密钥',
defaultCenter: [116.4074, 39.9042], // 北京
defaultZoom: 6,
mapStyle: 'amap://styles/darkblue'
};
在 chinaCities.ts 中添加新的城市数据:
{ code: '440100', name: '广州', lat: 23.1291, lng: 113.2644, province: '广东' }
修改 mapConfig.ts 中的标记配置:
markerConfig: {
minRadius: 5,
maxRadius: 50,
colors: {
high: '#ff0000',
medium: '#ffff00',
low: '#00ff00'
}
}
使用真实API替换模拟数据:
// 替换 useChinaMapData 中的 queryFn
queryFn: async () => {
const response = await fetch('/api/china-data');
return response.json();
}
src/client/big-shadcn/components/ChinaAMap.tsx - 主要地图组件src/client/big-shadcn/components/AMapLoader.tsx - 地图加载器src/client/big-shadcn/config/mapConfig.ts - 配置管理src/client/big-shadcn/data/chinaCities.ts - 城市数据