index.vue 4.7 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206
  1. <template>
  2. <div ref="containerRef" class="map-container">
  3. <div ref="mapRef" id="YztMap" class="map-container" :style="{ width: width, height: height }"></div>
  4. </div>
  5. </template>
  6. <script setup lang="ts">
  7. import 'ol/ol.css';
  8. import { olMap } from '@/utils/olMap/olMap';
  9. interface Props {
  10. activeMap: string;
  11. drawing: boolean;
  12. color: string;
  13. drawType: string;
  14. graphicsType: string;
  15. }
  16. const props = withDefaults(defineProps<Props>(), {});
  17. const emits = defineEmits(['update:drawing', 'selectGraphics']);
  18. const mapRef = ref(null);
  19. const mapState = reactive({
  20. center: [110.93154257997, 21.669064031332],
  21. zoom: 9,
  22. minZoom: 6,
  23. maxZoom: 16,
  24. isThreeDimensional: false,
  25. // 是否显示比例尺
  26. showScale: true
  27. });
  28. const containerRef = ref();
  29. const width = ref('100%');
  30. const height = ref('100%');
  31. let yztMap, map;
  32. // 监听是否开启绘制
  33. watch(
  34. () => props.drawing,
  35. (value) => {
  36. if (value) {
  37. map.drawGraphics(props.graphicsType);
  38. } else {
  39. map.closeDraw();
  40. }
  41. }
  42. );
  43. const init = () => {
  44. map = new olMap({
  45. dom: mapRef.value,
  46. id: 'YZT1715739306532',
  47. center: mapState.center,
  48. zoom: mapState.zoom,
  49. minZoom: mapState.minZoom,
  50. maxZoom: mapState.maxZoom,
  51. drawTool: {
  52. use: true,
  53. color: props.color,
  54. drawType: props.drawType,
  55. graphicsType: props.graphicsType,
  56. // 绘制完成事件
  57. onDrawCompleted: (data, overlaysData, obj) => {
  58. emits('selectGraphics', data, overlaysData.length.toString());
  59. // 点击空间分析
  60. obj.on('click', function () {
  61. // 没在编辑时
  62. if (!props.drawing) {
  63. emits('selectGraphics', data);
  64. }
  65. });
  66. }
  67. },
  68. // 加载完成事件
  69. onLoadCompleted: (map) => {
  70. yztMap = map;
  71. // initMouseTool(map);
  72. handleResize();
  73. }
  74. });
  75. };
  76. // 设置地图层级
  77. const setMapZoom = (value) => {
  78. if (!yztMap) return;
  79. const view = yztMap.map.getView();
  80. if (value === 1) {
  81. view.setCenter([113.280637, 23.125178]);
  82. view.setZoom(7);
  83. } else if (value === 2) {
  84. view.setCenter([110.93154257997, 21.6690640313328]);
  85. view.setZoom(11);
  86. } else if (value === 3) {
  87. view.setCenter([110.93154257997, 21.669064031332]);
  88. view.setZoom(12);
  89. } else if (value === 4) {
  90. view.setCenter([110.93154257997, 21.669064031332]);
  91. view.setZoom(15);
  92. } else if (value === 5) {
  93. view.setCenter([110.93154257997, 21.669064031332]);
  94. view.setZoom(16);
  95. }
  96. };
  97. // 切换2D、3D
  98. const switchThreeDimensional = () => {
  99. const view = yztMap.map.getView();
  100. mapState.isThreeDimensional = !mapState.isThreeDimensional;
  101. const pitch = mapState.isThreeDimensional ? 45 : 0;
  102. view.setPitch(pitch);
  103. };
  104. const handleResize = () => {
  105. const containerWidth = containerRef.value.clientWidth * (document.body.clientWidth / 8960);
  106. const containerHeight = containerRef.value.clientHeight * (document.body.clientHeight / 2520);
  107. width.value = containerWidth + 'px';
  108. height.value = containerHeight + 'px';
  109. yztMap.updateSize();
  110. };
  111. // 加载事件
  112. onMounted(() => {
  113. init();
  114. window.addEventListener('resize', handleResize);
  115. });
  116. // 卸载事件
  117. onUnmounted(() => {
  118. window.removeEventListener('resize', handleResize);
  119. });
  120. </script>
  121. <style scoped>
  122. .map-container {
  123. width: 100%;
  124. height: 100%;
  125. position: relative;
  126. .zoom-text {
  127. position: absolute;
  128. bottom: 8px;
  129. right: 75px;
  130. font-size: 14px;
  131. }
  132. .right-tool {
  133. position: absolute;
  134. bottom: 50px;
  135. right: 5px;
  136. }
  137. .model-btn {
  138. background-color: #022577;
  139. font-size: 14px;
  140. color: #889ee9;
  141. cursor: pointer;
  142. padding: 3px 3px;
  143. border-radius: 5px;
  144. }
  145. :deep(.amap-scalecontrol) {
  146. left: unset !important;
  147. background-color: unset !important;
  148. right: 74px;
  149. }
  150. :deep(.amap-scale-text) {
  151. width: 230px !important;
  152. text-align: left !important;
  153. font-size: 14px;
  154. padding-left: 20px;
  155. }
  156. :deep(.amap-scale-middle) {
  157. width: 228px !important;
  158. }
  159. :deep(.amap-scale-edgeright) {
  160. left: 228px !important;
  161. }
  162. :deep(.amap-logo),
  163. :deep(.amap-copyright) {
  164. display: none !important;
  165. }
  166. /* 自定义测距工具样式 */
  167. :deep(.amap-ranger) {
  168. background-color: #ffffff;
  169. border: 1px solid #888888;
  170. border-radius: 5px;
  171. padding: 5px;
  172. }
  173. :deep(.amap-ranger .amap-toolbar) {
  174. color: #333;
  175. font-size: 12px;
  176. padding: 5px;
  177. }
  178. :deep(.amap-ranger .amap-toolbar button) {
  179. background-color: #022577;
  180. color: #fff;
  181. border: none;
  182. border-radius: 4px;
  183. padding: 5px;
  184. margin-right: 5px;
  185. cursor: pointer;
  186. }
  187. :deep(.amap-ranger .amap-toolbar button:hover) {
  188. background-color: #1a3c75;
  189. }
  190. :deep(.amap-ranger .amap-toolbar .amap-toolbar-text) {
  191. color: #444;
  192. font-size: 14px;
  193. }
  194. }
  195. </style>