(props)
| 32 | } |
| 33 | let mouseTool: any = null |
| 34 | const SelectMap: React.FC<IProps> = (props) => { |
| 35 | const { closeMap, afterSelect, option, clickComponent } = props |
| 36 | const rectangle = useRef(null) |
| 37 | const [lnglatRange, setLnglatRange] = useState([]) // 当前选择的左上角和右下角坐标 |
| 38 | const [zoom, setZoom] = useState(0) // 当前地图缩放 |
| 39 | const [center, setCenter] = useState([]) // 当前地图中心 |
| 40 | const [drawType, setDrawType] = useState<string>('move') // 是否绘制矩形 |
| 41 | const [loading, setLoading] = useState<boolean>(false) // 获取3dBuildings数据loading |
| 42 | useEffect(() => { |
| 43 | const asyncFun = async () => { |
| 44 | // 为地图注册click事件获取鼠标点击出的经纬度坐标 |
| 45 | const data = await request(`https://restapi.amap.com/v3/geocode/geo?key=502123a4f08bd95be55b49ae6eabd728&address=上海外滩`, { |
| 46 | method: 'get', |
| 47 | prefix: '', |
| 48 | credentials: false // 带上cookie会请求不成功 |
| 49 | }) |
| 50 | const center = data.geocodes[0].location.split(',') |
| 51 | const map = new window.AMap.Map('map', { |
| 52 | zoom: 15, |
| 53 | pitch: 70.41138037735848, |
| 54 | center, |
| 55 | })// 高德地图 |
| 56 | // 通过插件方式引入 AMap.MouseTool 工具 |
| 57 | map.plugin(["AMap.MouseTool"], async () => { |
| 58 | // 在地图中添加MouseTool插件 |
| 59 | mouseTool = new AMap.MouseTool(map) |
| 60 | mouseTool.on('draw', async (e) => { |
| 61 | if (rectangle.current) { |
| 62 | map.remove(rectangle.current); |
| 63 | } |
| 64 | map.setFitView(e.obj); // 地图适应框框 |
| 65 | if (map.getZoom() < 14) { |
| 66 | map.setZoom(14) // 14级以下没有高程数据 |
| 67 | } |
| 68 | rectangle.current = e.obj |
| 69 | const startLnglat = gcj02towgs84(e.obj.getPath()[0].lng, e.obj.getPath()[0].lat); // 0 左上角 gcj02坐标 高德坐标转wgs84坐标 |
| 70 | const endLnglat = gcj02towgs84(e.obj.getPath()[2].lng, e.obj.getPath()[2].lat); // 2 右下角 |
| 71 | setLnglatRange([startLnglat, endLnglat]) |
| 72 | setZoom(map.getZoom()) |
| 73 | setCenter(map.getCenter()) |
| 74 | }) |
| 75 | }); |
| 76 | |
| 77 | map.plugin(["AMap.Autocomplete", "AMap.PlaceSearch"], () => { |
| 78 | // 输入提示 |
| 79 | const autoOptions = { |
| 80 | input: "tipinput" |
| 81 | }; |
| 82 | const auto = new AMap.Autocomplete(autoOptions); |
| 83 | const placeSearch = new AMap.PlaceSearch({ |
| 84 | map |
| 85 | }); // 构造地点查询类 |
| 86 | AMap.event.addListener(auto, "select", select);// 注册监听,当选中某条记录时会触发 |
| 87 | function select(e) { |
| 88 | placeSearch.setCity(e.poi.adcode); |
| 89 | placeSearch.search(e.poi.name); // 关键字查询查询 |
| 90 | } |
| 91 | }); |
nothing calls this directly
no test coverage detected