react引入高德地图并初始化卫星地图
1.安装依赖
yarn add react-amap @amap/amap-jsapi-loader
2.初始化地图
import AMapLoader from "@amap/amap-jsapi-loader" ;
import { FC , useEffect, useRef, useState } from "react" ;
const HomeRight = ( ) => {
const mymap : any = useRef ( null ) ;
useEffect ( ( ) => {
AMapLoader. load ( {
key : "你的key" ,
version : "2.0" ,
plugins : [ ] ,
} )
. then ( initMap)
. catch ( ( e : any) => {
console. log ( e) ;
} ) ;
return ( ) => {
mymap. current. destroy ( ) ;
} ;
} , [ ] )
const initMap = ( ) => {
AMap. plugin (
[
"AMap.ToolBar" ,
"AMap.Scale" ,
"AMap.HawkEye" ,
"AMap.ControlBar" ,
"AMap.MapType" ,
"AMap.Geolocation" ,
"AMap.ContextMenu" ,
"AMap.AutoComplete" ,
"AMap.PlaceSearch" ,
] ,
function ( ) {
const satelliteLayer = new AMap. TileLayer. Satellite ( ) ;
const map = new AMap. Map ( "myMap" , {
resizeEnable : true ,
expandZoomRange : true ,
zooms : [ 4 , 20 ] ,
center : [ 116.397428 , 39.90923 ] ,
layers : [ satelliteLayer] ,
zoom : 5 ,
} ) ;
mymap. current = map;
}
) ;
} ;
return (
< div id= "myMap" style= { { width : "100%" , height : "100%" } } > < / div>
)
} ;