在react、vue框架中使用高德地图功能
安装
npm install @amap/amap-jsapi-loader --save
新建map.tsx文件(vue使用同例)
import { useEffect } from "react";
import AMapLoader from '@amap/amap-jsapi-loader';
const Home = ()=>{
let map: any = null;
const initMap = ()=>{
AMapLoader.load({
key:"高德地图Key",
version:"2.0",
plugins:['AMap.MapType','AMap.ElasticMarker'],
}).then((AMap)=>{
map = new AMap.Map("container",{
viewMode:"3D",
pitch:15,
terrain: true,
zoom:8,
center:[109.40,33.92],
mapStyle: 'amap://styles/darkblue',
});
map.addControl(new AMap.MapType());
}).catch(e=>{
console.log(e);
})
};
const initFun = ()=>{
initMap();
}
useEffect(()=>{
initFun()
return()=>{
map?.destroy();
}
},[])
return <>
<div>
<div id="container" style={{
width: "1000px",
height: "1000px",
backgroundColor: "red"
}}></div>
</div>
</>
}
export default Map;
效果