Map组件增加cesium地图
parent
025e1442a6
commit
76c204e16d
|
|
@ -1,6 +1,8 @@
|
||||||
import { Button, Col, Form, Input, Modal, Row, Select, Spin } from "antd";
|
import { Button, Col, Form, Input, Modal, Row, Select, Spin } from "antd";
|
||||||
import { useEffect, useRef, useState } from "react";
|
import { useEffect, useRef, useState } from "react";
|
||||||
import { dynamicLoadJs } from "../../utils";
|
import { dynamicLoadCss, dynamicLoadJs } from "../../utils";
|
||||||
|
import CesiumMap from "./CesiumMap";
|
||||||
|
import "./index.less";
|
||||||
|
|
||||||
/**
|
/**
|
||||||
* 定位组件弹窗
|
* 定位组件弹窗
|
||||||
|
|
@ -15,6 +17,7 @@ const MapSelector = (props) => {
|
||||||
area = "",
|
area = "",
|
||||||
showArea = false,
|
showArea = false,
|
||||||
disable = false,
|
disable = false,
|
||||||
|
type = "baidu",
|
||||||
} = props;
|
} = props;
|
||||||
|
|
||||||
const mapContainerRef = useRef(null);
|
const mapContainerRef = useRef(null);
|
||||||
|
|
@ -33,8 +36,87 @@ const MapSelector = (props) => {
|
||||||
setCurrentArea(area || "");
|
setCurrentArea(area || "");
|
||||||
}, [longitude, latitude, area]);
|
}, [longitude, latitude, area]);
|
||||||
|
|
||||||
|
// 百度地图初始化
|
||||||
|
const initBaiDuMap = () => {
|
||||||
|
if (!window?.BMapGL?.Map) {
|
||||||
|
setTimeout(() => {
|
||||||
|
initBaiDuMap();
|
||||||
|
}, 50);
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
const map = new window.BMapGL.Map(mapContainerRef.current);
|
||||||
|
mapInstanceRef.current = map;
|
||||||
|
|
||||||
|
map.centerAndZoom(
|
||||||
|
new window.BMapGL.Point(
|
||||||
|
longitude || window.mapLongitude,
|
||||||
|
latitude || window.mapLatitude,
|
||||||
|
),
|
||||||
|
16,
|
||||||
|
);
|
||||||
|
|
||||||
|
map.enableScrollWheelZoom(true);
|
||||||
|
|
||||||
|
// 如果有初始坐标,添加标记
|
||||||
|
if (longitude && latitude) {
|
||||||
|
const point = new window.BMapGL.Point(longitude, latitude);
|
||||||
|
const marker = new window.BMapGL.Marker(point);
|
||||||
|
map.addOverlay(marker);
|
||||||
|
}
|
||||||
|
|
||||||
|
// 添加点击事件
|
||||||
|
if (!disable) {
|
||||||
|
map.addEventListener("click", (event) => {
|
||||||
|
map.clearOverlays();
|
||||||
|
const point = new window.BMapGL.Point(event.latlng.lng, event.latlng.lat);
|
||||||
|
const marker = new window.BMapGL.Marker(point);
|
||||||
|
map.addOverlay(marker);
|
||||||
|
setCurrentLatitude(event.latlng.lat);
|
||||||
|
setCurrentLongitude(event.latlng.lng);
|
||||||
|
});
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// Cesium地图初始化
|
||||||
|
const initCesiumMap = () => {
|
||||||
|
const { init, flyTo, addMarkPoint, getLongitudeAndLatitude } = new CesiumMap();
|
||||||
|
const { viewer } = init();
|
||||||
|
mapInstanceRef.current = viewer;
|
||||||
|
flyTo({ longitude: longitude || window.mapLongitude, latitude: latitude || window.mapLatitude, height: 900000 });
|
||||||
|
if (longitude && latitude) {
|
||||||
|
addMarkPoint({ longitude, latitude });
|
||||||
|
}
|
||||||
|
getLongitudeAndLatitude((error, coords) => {
|
||||||
|
if (error)
|
||||||
|
return;
|
||||||
|
const { longitude, latitude } = coords;
|
||||||
|
setCurrentLatitude(latitude);
|
||||||
|
setCurrentLongitude(longitude);
|
||||||
|
});
|
||||||
|
};
|
||||||
|
|
||||||
// 初始化地图
|
// 初始化地图
|
||||||
const initMap = async () => {
|
const initMap = () => {
|
||||||
|
setLoading(true);
|
||||||
|
|
||||||
|
if (mapContainerRef.current) {
|
||||||
|
// 只有在没有地图实例时才创建新地图
|
||||||
|
if (!mapInstanceRef.current) {
|
||||||
|
if (type === "baidu") {
|
||||||
|
initBaiDuMap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (type === "cesium") {
|
||||||
|
initCesiumMap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
setLoading(false);
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
|
// 加载百度地图资源
|
||||||
|
const loadBaiDuMap = async () => {
|
||||||
if (!window.BMapGL) {
|
if (!window.BMapGL) {
|
||||||
if (window?.base?.loadDynamicResource) {
|
if (window?.base?.loadDynamicResource) {
|
||||||
await window.base.loadDynamicResource({
|
await window.base.loadDynamicResource({
|
||||||
|
|
@ -42,54 +124,57 @@ const MapSelector = (props) => {
|
||||||
type: "script",
|
type: "script",
|
||||||
attr: { type: "text/javascript" },
|
attr: { type: "text/javascript" },
|
||||||
});
|
});
|
||||||
|
initMap();
|
||||||
}
|
}
|
||||||
else {
|
else {
|
||||||
await dynamicLoadJs("https://api.map.baidu.com/api?v=1.0&type=webgl&ak=OElqFYoKiAH8KFtph8ftLKF5NlNrbCUr&callback=initialize");
|
await dynamicLoadJs("https://api.map.baidu.com/api?v=1.0&type=webgl&ak=OElqFYoKiAH8KFtph8ftLKF5NlNrbCUr&callback=initialize");
|
||||||
|
initMap();
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
initMap();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
setLoading(true);
|
// 加载Cesium地图资源
|
||||||
|
const loadCesiumMap = async () => {
|
||||||
// 确保DOM已经渲染
|
if (!window.Cesium) {
|
||||||
await new Promise(resolve => setTimeout(resolve, 100));
|
if (window?.base?.loadDynamicResource) {
|
||||||
|
await window.base.loadDynamicResource({
|
||||||
if (mapContainerRef.current) {
|
url: "https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Cesium.js",
|
||||||
// 只有在没有地图实例时才创建新地图
|
type: "script",
|
||||||
if (!mapInstanceRef.current) {
|
attr: { type: "text/javascript" },
|
||||||
const map = new window.BMapGL.Map(mapContainerRef.current);
|
});
|
||||||
mapInstanceRef.current = map;
|
await window.base.loadDynamicResource({
|
||||||
|
url: "https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Widgets/widgets.css",
|
||||||
map.centerAndZoom(
|
type: "link",
|
||||||
new window.BMapGL.Point(
|
attr: { rel: "stylesheet", type: "text/css" },
|
||||||
longitude || "119.69457721306945",
|
});
|
||||||
latitude || "39.940504336846665",
|
initMap();
|
||||||
),
|
|
||||||
16,
|
|
||||||
);
|
|
||||||
|
|
||||||
map.enableScrollWheelZoom(true);
|
|
||||||
|
|
||||||
// 如果有初始坐标,添加标记
|
|
||||||
if (longitude && latitude) {
|
|
||||||
const point = new window.BMapGL.Point(longitude, latitude);
|
|
||||||
const marker = new window.BMapGL.Marker(point);
|
|
||||||
map.addOverlay(marker);
|
|
||||||
}
|
|
||||||
|
|
||||||
// 添加点击事件
|
|
||||||
if (!disable) {
|
|
||||||
map.addEventListener("click", (event) => {
|
|
||||||
map.clearOverlays();
|
|
||||||
const point = new window.BMapGL.Point(event.latlng.lng, event.latlng.lat);
|
|
||||||
const marker = new window.BMapGL.Marker(point);
|
|
||||||
map.addOverlay(marker);
|
|
||||||
setCurrentLatitude(event.latlng.lat);
|
|
||||||
setCurrentLongitude(event.latlng.lng);
|
|
||||||
});
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
else {
|
||||||
|
await dynamicLoadJs("https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Cesium.js");
|
||||||
|
await dynamicLoadCss("https://cesium.com/downloads/cesiumjs/releases/1.91/Build/Cesium/Widgets/widgets.css");
|
||||||
|
initMap();
|
||||||
|
}
|
||||||
|
}
|
||||||
|
else {
|
||||||
|
initMap();
|
||||||
|
}
|
||||||
|
};
|
||||||
|
|
||||||
setLoading(false);
|
// 加载地图资源
|
||||||
|
const loadMap = () => {
|
||||||
|
if (!window.mapLongitude && !window.mapLatitude) {
|
||||||
|
console.error("请在window设置变量 mapLongitude 和 mapLatitude,以供地图初始化坐标使用");
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (type === "baidu") {
|
||||||
|
loadBaiDuMap();
|
||||||
|
return;
|
||||||
|
}
|
||||||
|
if (type === "cesium") {
|
||||||
|
loadCesiumMap();
|
||||||
}
|
}
|
||||||
};
|
};
|
||||||
|
|
||||||
|
|
@ -114,6 +199,10 @@ const MapSelector = (props) => {
|
||||||
// 关闭弹窗
|
// 关闭弹窗
|
||||||
const handleClose = () => {
|
const handleClose = () => {
|
||||||
setLocalSearch("");
|
setLocalSearch("");
|
||||||
|
if (mapInstanceRef.current) {
|
||||||
|
mapInstanceRef.current.destroy();
|
||||||
|
mapInstanceRef.current = null;
|
||||||
|
}
|
||||||
if (onClose)
|
if (onClose)
|
||||||
onClose();
|
onClose();
|
||||||
};
|
};
|
||||||
|
|
@ -133,7 +222,7 @@ const MapSelector = (props) => {
|
||||||
if (visible) {
|
if (visible) {
|
||||||
// 延迟初始化地图,确保DOM完全渲染
|
// 延迟初始化地图,确保DOM完全渲染
|
||||||
initTimer = setTimeout(() => {
|
initTimer = setTimeout(() => {
|
||||||
initMap();
|
loadMap();
|
||||||
}, 100);
|
}, 100);
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|
@ -201,7 +290,7 @@ const MapSelector = (props) => {
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
{
|
{
|
||||||
!disable && (
|
(!disable && type === "baidu") && (
|
||||||
<Row gutter={24}>
|
<Row gutter={24}>
|
||||||
<Col span={12}>
|
<Col span={12}>
|
||||||
<Form.Item label="关键字搜索">
|
<Form.Item label="关键字搜索">
|
||||||
|
|
@ -236,6 +325,7 @@ const MapSelector = (props) => {
|
||||||
</Form>
|
</Form>
|
||||||
<div
|
<div
|
||||||
ref={mapContainerRef}
|
ref={mapContainerRef}
|
||||||
|
id="map_container"
|
||||||
style={{ width: "100%", height: "500px", position: "relative" }}
|
style={{ width: "100%", height: "500px", position: "relative" }}
|
||||||
>
|
>
|
||||||
<Spin size="large" tip="地图正在加载中..." spinning={loading}>
|
<Spin size="large" tip="地图正在加载中..." spinning={loading}>
|
||||||
|
|
|
||||||
Loading…
Reference in New Issue