map.vue 6.8 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308
  1. <template>
  2. <div class="map-layout" @click="activeId = null">
  3. <div
  4. id="map"
  5. class="map-container"
  6. ref="container"
  7. :class="{ active: !!activeId }"
  8. @click.stop
  9. @mousedown="activeId = null"
  10. @wheel="activeId = null"
  11. >
  12. <div class="map-component">
  13. <el-tooltip
  14. class="tooltip"
  15. :visible="!!activeId"
  16. :content="active?.name"
  17. effect="light"
  18. placement="top"
  19. virtual-triggering
  20. :virtual-ref="triggerRef"
  21. />
  22. <el-select
  23. v-model="tileType"
  24. placeholder="选择底图"
  25. style="width: 120px"
  26. class="tile-type-select"
  27. >
  28. <el-option
  29. v-for="item in tileOptions"
  30. :key="item"
  31. :label="item"
  32. :value="item"
  33. />
  34. </el-select>
  35. </div>
  36. </div>
  37. <div class="right-control">
  38. <MapRight
  39. @fly-point="flyScenePoint"
  40. @fly-scene="flyScene"
  41. @goto-point="gotoPoint"
  42. />
  43. </div>
  44. </div>
  45. </template>
  46. <script setup lang="ts">
  47. import MapRight from "./map-right.vue";
  48. import { router, setDocTitle } from "@/router";
  49. import { TileType, createMap } from "./";
  50. import { ScenePoint, Scene, scenePoints, scenes } from "@/store/scene";
  51. import { initRelics, initSelfRelics, relics } from "@/store/relics";
  52. import { computed, onMounted, ref, watchEffect, watch } from "vue";
  53. import { Manage } from "./manage";
  54. import ScaleLine from "ol/control/ScaleLine";
  55. const activeId = ref<ScenePoint["id"] | null>();
  56. const active = computed(() =>
  57. scenePoints.value.find((point) => point.id === activeId.value)
  58. );
  59. const activePixel = ref<number[] | null>();
  60. const triggerRef = ref({
  61. getBoundingClientRect() {
  62. return DOMRect.fromRect({
  63. x: activePixel.value![0],
  64. y: activePixel.value![1],
  65. width: 0,
  66. height: 0,
  67. });
  68. },
  69. });
  70. const tileOptions: TileType[] = ["影像底图", "矢量底图"];
  71. const tileType = ref<TileType>(tileOptions[0]);
  72. const points = computed(() =>
  73. scenePoints.value
  74. .filter((point) => point.pos)
  75. .map((point) => ({
  76. data: point.pos,
  77. id: point.id,
  78. label: point.name,
  79. }))
  80. );
  81. const gotoPoint = (point: ScenePoint) => {
  82. router.push({
  83. name: router.currentRoute.value.name === "map" ? "pano" : "query-pano",
  84. params: { pid: point.id },
  85. });
  86. };
  87. const flyUserCenter = () => {
  88. navigator.geolocation.getCurrentPosition(
  89. (pos) => {
  90. mapManage.setCenter([pos.coords.longitude, pos.coords.latitude]);
  91. },
  92. () => console.error("获取中心位置失败"),
  93. {
  94. enableHighAccuracy: true,
  95. timeout: 5000,
  96. maximumAge: 0,
  97. }
  98. );
  99. };
  100. const center = [109.47293862712675, 30.26530938156551];
  101. const container = ref<HTMLDivElement>();
  102. let mapManage: Manage;
  103. onMounted(async () => {
  104. mapManage = createMap(container.value!);
  105. mapManage.setCenter(center);
  106. mapManage.hotsBus.on("active", (id) => {
  107. if (id) {
  108. activeId.value = id;
  109. active.value && activeScenePoint(active.value!);
  110. } else {
  111. activeId.value = null;
  112. activePixel.value = null;
  113. }
  114. });
  115. mapManage.hotsBus.on("click", (id) => {
  116. const point = id && scenePoints.value.find((point) => point.id === id);
  117. point && gotoPoint(point);
  118. });
  119. refreshHots();
  120. refreshTileType();
  121. const scaleLine = new ScaleLine({
  122. className: "scale-view",
  123. maxWidth: 150,
  124. minWidth: 100,
  125. units: "metric",
  126. });
  127. // 加载比例尺
  128. mapManage.map.addControl(scaleLine);
  129. watch(
  130. tileType,
  131. (type) => {
  132. const el = (scaleLine as any).element as HTMLDivElement;
  133. el.classList.add(type === "影像底图" ? "light" : "dark");
  134. el.classList.remove(type === "影像底图" ? "dark" : "light");
  135. console.log(el, type);
  136. },
  137. { flush: "post", immediate: true }
  138. );
  139. });
  140. const activeScenePoint = (point: ScenePoint) => {
  141. activePixel.value = mapManage.map.getPixelFromCoordinate(point.pos);
  142. activeId.value = point.id;
  143. };
  144. const flyPos = (pos: number[]) => mapManage.map.getView().setCenter(pos);
  145. const flyScenePoint = (point: ScenePoint) => {
  146. flyPos(point.pos);
  147. setTimeout(() => {
  148. activeScenePoint(point);
  149. }, 16);
  150. };
  151. const flyScene = (scene: Scene) => {
  152. const totalPos = [0, 0];
  153. let numCalc = 0;
  154. for (let i = 0; i < scene.scenePos.length; i++) {
  155. totalPos[0] += scene.scenePos[i].pos[0];
  156. totalPos[1] += scene.scenePos[i].pos[1];
  157. numCalc++;
  158. }
  159. totalPos[0] /= numCalc;
  160. totalPos[1] /= numCalc;
  161. flyPos(totalPos);
  162. };
  163. const refreshHots = () => {
  164. if (!mapManage) return;
  165. mapManage.clearHots();
  166. mapManage.addHots(points.value);
  167. };
  168. const refreshTileType = () => {
  169. if (!mapManage) return;
  170. mapManage.setTileType(tileType.value);
  171. };
  172. watch(points, refreshHots, { immediate: true });
  173. watch(tileType, refreshTileType, { immediate: true });
  174. watch(
  175. () => [router.currentRoute.value.name, router.currentRoute.value.params?.relicsId],
  176. ([name, rid]) => {
  177. if (["map", "query-map"].includes(name as string)) {
  178. relics.value = undefined;
  179. const fn = name === "map" ? initSelfRelics : initRelics;
  180. fn(Number(rid)).finally(() => {
  181. if (!relics.value) {
  182. router.replace({ name: "relics" });
  183. }
  184. const scene = scenes.value.find(
  185. (scene) => !scene.scenePos.every((pos) => !pos.pos || pos.pos.length === 0)
  186. );
  187. if (scene) {
  188. flyScene(scene);
  189. } else {
  190. flyUserCenter();
  191. }
  192. });
  193. }
  194. },
  195. { immediate: true }
  196. );
  197. watchEffect(() => {
  198. if (
  199. ["map", "query-map"].includes(router.currentRoute.value.name as string) &&
  200. relics.value
  201. ) {
  202. setDocTitle(relics.value.name);
  203. }
  204. });
  205. </script>
  206. <style lang="scss">
  207. .tooltip {
  208. pointer-events: none;
  209. }
  210. .map-layout {
  211. display: flex;
  212. flex-direction: row;
  213. height: 100%;
  214. }
  215. .map-container {
  216. flex: 1;
  217. position: relative;
  218. }
  219. .right-control {
  220. flex: none;
  221. width: 300px;
  222. padding: 15px;
  223. border-left: 1px solid var(--border-color);
  224. }
  225. .map-component {
  226. width: 100%;
  227. height: 100%;
  228. position: relative;
  229. }
  230. .active {
  231. cursor: pointer;
  232. }
  233. .active-point {
  234. position: absolute;
  235. pointer-events: none;
  236. }
  237. .map-component {
  238. pointer-events: none;
  239. position: absolute;
  240. width: 100%;
  241. height: 100%;
  242. left: 0;
  243. top: 0;
  244. z-index: 9;
  245. }
  246. .env {
  247. width: 100%;
  248. height: 100%;
  249. }
  250. .tile-type-select {
  251. pointer-events: all;
  252. position: absolute;
  253. right: 10px;
  254. top: 10px;
  255. }
  256. .scale-view {
  257. --color: #fff;
  258. position: absolute;
  259. left: 20px;
  260. bottom: 20px;
  261. height: 8px;
  262. color: var(--color);
  263. text-align: center;
  264. border: 1px solid var(--color);
  265. border-top: none;
  266. z-index: 1;
  267. font-size: 14px;
  268. display: flex;
  269. align-items: end;
  270. &.light {
  271. --color: #fff;
  272. > div {
  273. text-shadow: 0 0 2px #000;
  274. }
  275. }
  276. &.dark {
  277. --color: #000;
  278. > div {
  279. text-shadow: 0 0 2px #fff;
  280. }
  281. }
  282. }
  283. </style>