|
@@ -1,6 +1,6 @@
|
|
|
<template>
|
|
|
<div class="map-container">
|
|
|
- <div id="yztMap" class="map-container"></div>
|
|
|
+ <div id="aMap" class="map-container"></div>
|
|
|
<!-- 右下工具 -->
|
|
|
<div v-show="mapState.showScale" class="zoom-text">{{ mapState.zoom }}级</div>
|
|
|
<div class="right-tool">
|
|
@@ -20,14 +20,18 @@
|
|
|
|
|
|
<script setup lang="ts" name="Map">
|
|
|
import { onMounted, onUnmounted, reactive, watch, defineProps, withDefaults, ref } from 'vue';
|
|
|
-import AMapLoader from '@amap/amap-jsapi-loader';
|
|
|
import QuickZoom from './quickZoom.vue';
|
|
|
+import { useAMap } from '@/hooks/AMap/useAMap';
|
|
|
+import { useHistory } from '@/hooks/useHistory';
|
|
|
+import { useMouseTool } from '@/hooks/AMap/useMouseTool';
|
|
|
+import { useRuler } from '@/hooks/AMap/useRuler';
|
|
|
|
|
|
interface Props {
|
|
|
activeMap: string;
|
|
|
drawing: boolean;
|
|
|
color: string;
|
|
|
drawType: string;
|
|
|
+ graphicsType: string;
|
|
|
}
|
|
|
|
|
|
const props = withDefaults(defineProps<Props>(), {});
|
|
@@ -43,166 +47,80 @@ const mapState = reactive({
|
|
|
showScale: true
|
|
|
});
|
|
|
|
|
|
-let map, scale, nowLayer, ruler, mouseTool;
|
|
|
-const isRanging = ref(false);
|
|
|
-
|
|
|
-// 监听地图类型变化
|
|
|
-watch(
|
|
|
- () => props.activeMap,
|
|
|
- (value, oldValue) => {
|
|
|
+let AMap, map, scale;
|
|
|
+const { historyStack, currentIndex, pushHistory, undo } = useHistory();
|
|
|
+// 鼠标绘制工具
|
|
|
+const { initMouseTool, drawGraphics, setColor, setDrawType, setGraphicsType } = useMouseTool({
|
|
|
+ color: props.color,
|
|
|
+ drawType: props.drawType,
|
|
|
+ graphicsType: props.graphicsType,
|
|
|
+ onDrawCompleted: (data) => {
|
|
|
+ pushHistory(data);
|
|
|
+ }
|
|
|
+});
|
|
|
+// 测距工具
|
|
|
+const { initRuler, isRanging, toggleRangingTool } = useRuler();
|
|
|
+// 初始化地图
|
|
|
+const { getAMap, getMap, switchMap, addMarker, clearMarker, getMarkers } = useAMap({
|
|
|
+ key: '30d3d8448efd68cb0b284549fd41adcf', // 申请好的Web端开发者Key,首次调用 load 时必填
|
|
|
+ version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
|
|
|
+ plugins: ['AMap.Scale', 'AMap.RangingTool', 'AMap.MouseTool', 'AMap.PolygonEditor'],
|
|
|
+ pitch: mapState.isThreeDimensional ? 45 : 0,
|
|
|
+ zoom: mapState.zoom,
|
|
|
+ center: [mapState.center[0], mapState.center[1]],
|
|
|
+ dragEnable: true,
|
|
|
+ scrollWheel: true,
|
|
|
+ showScale: true,
|
|
|
+ enableMouseTool: true,
|
|
|
+ onLoadCompleted: () => {
|
|
|
+ AMap = getAMap();
|
|
|
+ map = getMap();
|
|
|
if (!['logical', 'vectorgraph'].includes(props.activeMap)) {
|
|
|
switchMap(props.activeMap);
|
|
|
} else {
|
|
|
- map.removeLayer(nowLayer);
|
|
|
+ map.removeLayer();
|
|
|
}
|
|
|
+ map.on('zoomchange', zoomChangeHandler);
|
|
|
+ initMouseTool(map, AMap);
|
|
|
+ initRuler(map, AMap);
|
|
|
+ }
|
|
|
+});
|
|
|
+// 监听地图类型变化
|
|
|
+watch(
|
|
|
+ () => props.activeMap,
|
|
|
+ (value, oldValue) => {
|
|
|
+ switchMap(props.activeMap);
|
|
|
}
|
|
|
);
|
|
|
-
|
|
|
-// 初始化事件
|
|
|
-const initMap = () => {
|
|
|
- AMapLoader.load({
|
|
|
- key: '30d3d8448efd68cb0b284549fd41adcf', // 申请好的Web端开发者Key,首次调用 load 时必填
|
|
|
- version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
|
|
|
- plugins: ['AMap.Scale', 'AMap.RangingTool', 'AMap.MouseTool']
|
|
|
- }).then((AMap) => {
|
|
|
- map = new AMap.Map('yztMap', {
|
|
|
- // 是否为3D地图模式
|
|
|
- viewMode: '3D',
|
|
|
- pitch: mapState.isThreeDimensional ? 45 : 0,
|
|
|
- // 初始化地图级别
|
|
|
- zoom: mapState.zoom,
|
|
|
- // 初始化地图中心点位置
|
|
|
- center: [mapState.center[0], mapState.center[1]],
|
|
|
- // 是否可拖拽
|
|
|
- dragEnable: true,
|
|
|
- // 是否允许通过鼠标滚轮来缩放
|
|
|
- scrollWheel: true
|
|
|
- });
|
|
|
- // 初始化绘制工具
|
|
|
- mouseTool = new AMap.MouseTool(map);
|
|
|
- mouseTool.on('draw', function (event) {
|
|
|
- // event.obj 为绘制出来的覆盖物对象
|
|
|
- console.log('覆盖物对象绘制完成', event);
|
|
|
- mouseTool.close();
|
|
|
- emits('update:drawing', false);
|
|
|
- });
|
|
|
- if (!['logical', 'vectorgraph'].includes(props.activeMap)) {
|
|
|
- switchMap(props.activeMap);
|
|
|
- }
|
|
|
- // else {
|
|
|
- // map.removeLayer();
|
|
|
- // }
|
|
|
-
|
|
|
- scale = new AMap.Scale();
|
|
|
- if (mapState.showScale) {
|
|
|
- map.addControl(scale);
|
|
|
- }
|
|
|
-
|
|
|
- // 自定义测距工具样式
|
|
|
- const startMarkerOptions = {
|
|
|
- icon: new AMap.Icon({
|
|
|
- size: new AMap.Size(19, 31), // 图标大小
|
|
|
- imageSize: new AMap.Size(19, 31),
|
|
|
- image: '//webapi.amap.com/theme/v1.3/markers/b/start.png'
|
|
|
- }),
|
|
|
- offset: new AMap.Pixel(-9, -31)
|
|
|
- };
|
|
|
- const endMarkerOptions = {
|
|
|
- icon: new AMap.Icon({
|
|
|
- size: new AMap.Size(19, 31), // 图标大小
|
|
|
- imageSize: new AMap.Size(19, 31),
|
|
|
- image: '//webapi.amap.com/theme/v1.3/markers/b/end.png'
|
|
|
- }),
|
|
|
- offset: new AMap.Pixel(-9, -31)
|
|
|
- };
|
|
|
- const midMarkerOptions = {
|
|
|
- icon: new AMap.Icon({
|
|
|
- size: new AMap.Size(19, 31), // 图标大小
|
|
|
- imageSize: new AMap.Size(19, 31),
|
|
|
- image: '//webapi.amap.com/theme/v1.3/markers/b/mid.png'
|
|
|
- }),
|
|
|
- offset: new AMap.Pixel(-9, -31)
|
|
|
- };
|
|
|
- const lineOptions = {
|
|
|
- strokeStyle: 'solid',
|
|
|
- strokeColor: '#FF33FF',
|
|
|
- strokeOpacity: 1,
|
|
|
- strokeWeight: 2
|
|
|
- };
|
|
|
- const rulerOptions = {
|
|
|
- startMarkerOptions: startMarkerOptions,
|
|
|
- midMarkerOptions: midMarkerOptions,
|
|
|
- endMarkerOptions: endMarkerOptions,
|
|
|
- lineOptions: lineOptions
|
|
|
- };
|
|
|
-
|
|
|
- // 初始化自定义样式测距工具
|
|
|
- ruler = new AMap.RangingTool(map, rulerOptions);
|
|
|
-
|
|
|
- map.on('zoomchange', zoomChangeHandler);
|
|
|
- });
|
|
|
-};
|
|
|
-
|
|
|
-// 切换测距工具状态
|
|
|
-const toggleRangingTool = () => {
|
|
|
- if (ruler) {
|
|
|
- if (isRanging.value) {
|
|
|
- ruler.turnOff(); // 关闭测距工具
|
|
|
- } else {
|
|
|
- ruler.turnOn(); // 启动测距工具
|
|
|
+// 监听是否开启绘制
|
|
|
+watch(
|
|
|
+ () => props.drawing,
|
|
|
+ (value) => {
|
|
|
+ if (value) {
|
|
|
+ drawGraphics(props.graphicsType);
|
|
|
}
|
|
|
- isRanging.value = !isRanging.value; // 切换状态
|
|
|
}
|
|
|
-};
|
|
|
-
|
|
|
-// 切换地图
|
|
|
-const switchMap = (type) => {
|
|
|
- mapState.zoom = 11;
|
|
|
- if (type === 'satellite') {
|
|
|
- const satellite = new AMap.TileLayer.Satellite();
|
|
|
- map.addLayer(satellite);
|
|
|
- nowLayer = satellite;
|
|
|
- } else if (type === 'satellite2') {
|
|
|
- let queryParamsArr = [
|
|
|
- {
|
|
|
- serviceCode: 'YZT1715739306532'
|
|
|
- },
|
|
|
- {
|
|
|
- serviceCode: 'YZT1695608158269',
|
|
|
- REQUEST: 'GetCapabilities'
|
|
|
- }
|
|
|
- ];
|
|
|
- console.log(queryParamsArr[0].serviceCode);
|
|
|
- var wms = new AMap.TileLayer.WMTS({
|
|
|
- // url: 'http://t0.tianditu.gov.cn/img_c/wmts',
|
|
|
- url: 'http://10.181.7.236:9988/api/oneShare/proxyHandler/mm/' + queryParamsArr[0].serviceCode,
|
|
|
- // blend: true,
|
|
|
- tileSize: 256,
|
|
|
- params: {
|
|
|
- // Layer: "img",
|
|
|
- // Version: "1.0.0",
|
|
|
- // Format: "tiles",
|
|
|
- // TileMatrixSet: "w",
|
|
|
- // STYLE: "default",
|
|
|
- // tk: 'd31190edaa4190af833234ab7b1a6f27'
|
|
|
- SERVICE: 'WMTS',
|
|
|
- // REQUEST: 'GetTile',
|
|
|
- VERSION: '1.0.0',
|
|
|
- LAYER: 'gds_2m_kj202401',
|
|
|
- STYLE: 'default',
|
|
|
- // TILEMATRIXSET: 'default028mm',
|
|
|
- // TILEMATRIX: '9',
|
|
|
- // TILEROW: '97',
|
|
|
- // TILECOL: '413',
|
|
|
- Format: 'image/jpgpng'
|
|
|
- }
|
|
|
- });
|
|
|
- wms.setMap(map);
|
|
|
+);
|
|
|
+watch(
|
|
|
+ () => props.color,
|
|
|
+ () => {
|
|
|
+ setColor(props.color);
|
|
|
}
|
|
|
-};
|
|
|
-
|
|
|
+);
|
|
|
+watch(
|
|
|
+ () => props.drawType,
|
|
|
+ () => {
|
|
|
+ setDrawType(props.drawType);
|
|
|
+ }
|
|
|
+);
|
|
|
+watch(
|
|
|
+ () => props.graphicsType,
|
|
|
+ () => {
|
|
|
+ setGraphicsType(props.graphicsType);
|
|
|
+ }
|
|
|
+);
|
|
|
// 缩放级别变化
|
|
|
-const zoomChangeHandler = (event) => {
|
|
|
+const zoomChangeHandler = () => {
|
|
|
mapState.zoom = map.getZoom();
|
|
|
};
|
|
|
|
|
@@ -243,69 +161,15 @@ const switchThreeDimensional = () => {
|
|
|
const pitch = mapState.isThreeDimensional ? 45 : 0;
|
|
|
map.setPitch(pitch);
|
|
|
};
|
|
|
-let markers = {};
|
|
|
-// 实例化点标记
|
|
|
-const addMarker = (item) => {
|
|
|
- // 点标记显示内容,HTML要素字符串
|
|
|
- var markerContent =
|
|
|
- '' +
|
|
|
- '<div class="custom-content-marker" style="display: flex;align-items: center;flex-direction: column;width: 100px;color: #000;font-size: 16px;">' +
|
|
|
- '<div>' +
|
|
|
- item.label +
|
|
|
- '</div>' +
|
|
|
- ' <img src="//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-red.png" style="width: 20px;">' +
|
|
|
- '</div>';
|
|
|
- let marker = new AMap.Marker({
|
|
|
- size: new AMap.Size(25, 34),
|
|
|
- content: markerContent,
|
|
|
- position: [item.center[0], item.center[1]],
|
|
|
- offset: new AMap.Pixel(-13, -30)
|
|
|
- });
|
|
|
- marker.setMap(map);
|
|
|
- if (markers[item.id]) {
|
|
|
- markers[item.id].push(marker);
|
|
|
- } else {
|
|
|
- markers[item.id] = [marker];
|
|
|
- }
|
|
|
-};
|
|
|
-// 清除所有标加
|
|
|
-const clearMarker = (id) => {
|
|
|
- if (!markers[id]) return;
|
|
|
- markers[id].forEach((marker) => {
|
|
|
- marker.remove();
|
|
|
- });
|
|
|
- delete markers[id];
|
|
|
-};
|
|
|
-const getMarkers = () => {
|
|
|
- return markers;
|
|
|
-};
|
|
|
|
|
|
-// 监听是否开启绘制
|
|
|
-watch(
|
|
|
- () => props.drawing,
|
|
|
- (value) => {
|
|
|
- if (value) {
|
|
|
- drawPolygon();
|
|
|
- }
|
|
|
- }
|
|
|
-);
|
|
|
-
|
|
|
-const drawPolygon = () => {
|
|
|
- mouseTool.polygon({
|
|
|
- strokeColor: props.color,
|
|
|
- strokeOpacity: 1,
|
|
|
- strokeWeight: 2,
|
|
|
- fillColor: props.color,
|
|
|
- fillOpacity: props.drawType === '1' ? 0 : 0.5,
|
|
|
- strokeStyle: 'solid'
|
|
|
- });
|
|
|
+// 处理撤销
|
|
|
+const handleUndo = () => {
|
|
|
+ undo();
|
|
|
+ map.clearMap();
|
|
|
+ console.log(historyStack.value.length, currentIndex.value);
|
|
|
};
|
|
|
|
|
|
-defineExpose({ addMarker, getMarkers, clearMarker });
|
|
|
-// 加载事件
|
|
|
-onMounted(() => {
|
|
|
- initMap();
|
|
|
-});
|
|
|
+defineExpose({ addMarker, getMarkers, clearMarker, handleUndo });
|
|
|
|
|
|
// 卸载事件
|
|
|
onUnmounted(() => {
|