index.vue 6.4 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280
  1. <template>
  2. <div class="map-layout">
  3. <div class="search">
  4. <el-input
  5. v-model="keyword"
  6. :placeholder="`请输入${searchName}`"
  7. class="input-with-select"
  8. @keydown.enter="searchHandler"
  9. >
  10. <template #prepend>
  11. <el-select v-model="searchType" style="width: 100px">
  12. <el-option
  13. :label="type.label"
  14. :value="type.value"
  15. v-for="type in searchTypes"
  16. />
  17. </el-select>
  18. </template>
  19. </el-input>
  20. <div
  21. class="latlng-result"
  22. v-if="searchType === 'latlng' && (mark || mark === null)"
  23. :class="{ success: mark }"
  24. >
  25. <template v-if="mark">
  26. <h3>经纬度定位成功</h3>
  27. <p>经度:{{ mark.lat }}</p>
  28. <p>纬度:{{ mark.lng }}</p>
  29. </template>
  30. <template v-else>
  31. <h3>经纬度定位失败</h3>
  32. <p>请输入正确的经纬度格式</p>
  33. <p>纬度,经度 (例如23.11766,113.28122)</p>
  34. <p>纬度范围:-90到90</p>
  35. <p>经度范围:-180到180</p>
  36. </template>
  37. </div>
  38. <div class="name-result" v-if="searchType === 'name' && options.length">
  39. <div
  40. class="address-item operate"
  41. v-for="option in options"
  42. :class="{ active: activeOption === option }"
  43. @click="clickOption(option)"
  44. >
  45. {{ option.address }}
  46. </div>
  47. </div>
  48. </div>
  49. <div class="map" ref="mapEle">
  50. <div class="tiles-select">
  51. <el-dropdown placement="bottom-end">
  52. <icon name="close" size="30px" color="#000" />
  53. <template #dropdown>
  54. <el-dropdown-menu>
  55. <el-dropdown-item
  56. v-for="(group, ndx) in tileGroups"
  57. @click="groupIndex = ndx"
  58. >
  59. {{ group.name }}
  60. </el-dropdown-item>
  61. </el-dropdown-menu>
  62. </template>
  63. </el-dropdown>
  64. </div>
  65. </div>
  66. </div>
  67. </template>
  68. <script lang="ts" setup>
  69. import { computed, ref, shallowRef, watch, watchEffect } from "vue";
  70. import {
  71. getCurrentLatlng,
  72. LatLng,
  73. latlngStrTransform,
  74. useLMap,
  75. useOnlyMarker,
  76. useSetLTileLayers,
  77. } from "./useLeaflet";
  78. import { BasemapInfo, SearchResultItem, SelectMapImageProps } from "../index";
  79. import html2canvas from "html2canvas";
  80. import {
  81. ElInput,
  82. ElSelect,
  83. ElOption,
  84. ElDropdown,
  85. ElDropdownMenu,
  86. ElDropdownItem,
  87. } from "element-plus";
  88. const props = defineProps<SelectMapImageProps>();
  89. const mapEle = shallowRef<HTMLDivElement>();
  90. const lMap = useLMap(mapEle);
  91. const setTileLayers = useSetLTileLayers(lMap);
  92. const groupIndex = ref(props.activeGroupIndex || 0);
  93. watchEffect(
  94. () => {
  95. if (groupIndex.value > props.tileGroups.length - 1) {
  96. groupIndex.value = 0;
  97. }
  98. },
  99. { flush: "sync" }
  100. );
  101. const tiles = computed(() => props.tileGroups[groupIndex.value].tiles);
  102. watchEffect(() => setTileLayers(tiles.value));
  103. const searchTypes = [
  104. { label: "经纬度", value: "latlng" },
  105. { label: "名称", value: "name" },
  106. ];
  107. const searchType = ref(searchTypes[0].value as "latlng" | "name");
  108. const searchName = computed(
  109. () => searchTypes.find((item) => item.value === searchType.value)!.label
  110. );
  111. const keyword = ref("");
  112. const mark = useOnlyMarker(lMap, ref<LatLng | null>());
  113. watch(searchType, () => {
  114. mark.value = undefined;
  115. keyword.value = "";
  116. });
  117. const options = ref<SearchResultItem[]>([]);
  118. const activeOption = ref<SearchResultItem>();
  119. const clickOption = (item: SearchResultItem) => {
  120. activeOption.value = item;
  121. mark.value = item.latlng;
  122. };
  123. let token = 0;
  124. const searchHandler = async () => {
  125. const currentToken = ++token;
  126. if (searchType.value === "latlng") {
  127. mark.value = latlngStrTransform(keyword.value);
  128. return;
  129. }
  130. options.value = [];
  131. activeOption.value = undefined;
  132. if (!keyword.value) return;
  133. const result = await props.search(keyword.value);
  134. if (currentToken === token) {
  135. options.value = result;
  136. }
  137. };
  138. getCurrentLatlng().then((latlng) => {
  139. if (!keyword.value && mark.value === undefined && searchType.value === "latlng") {
  140. keyword.value = `${latlng.lat},${latlng.lng}`;
  141. searchHandler();
  142. }
  143. });
  144. const submit = async (): Promise<BasemapInfo> => {
  145. if (!lMap.value) {
  146. throw "地图未初始化";
  147. }
  148. const bound = lMap.value.getBounds();
  149. const southWest = bound.getSouthWest(); // 西南角
  150. const northEast = bound.getNorthEast(); // 东北角
  151. const width = lMap.value.distance(
  152. [southWest.lng, northEast.lat],
  153. [northEast.lng, northEast.lat]
  154. );
  155. const height = lMap.value.distance(
  156. [northEast.lng, southWest.lat],
  157. [northEast.lng, northEast.lat]
  158. );
  159. let blob: Blob;
  160. try {
  161. const canvas = await html2canvas(mapEle.value!, {
  162. useCORS: true, // 允许跨域图片(瓦片需支持 CORS)
  163. allowTaint: true, // 允许污染 Canvas(慎用)
  164. scale: 2, // 提高分辨率
  165. logging: false, // 关闭日志
  166. });
  167. blob = await new Promise<Blob>((resolve, reject) => {
  168. canvas.toBlob((blob) => {
  169. blob ? resolve(blob) : reject("截图失败");
  170. });
  171. });
  172. } catch (e) {
  173. throw "截图失败";
  174. }
  175. return {
  176. blob,
  177. size: { width, height },
  178. };
  179. };
  180. defineExpose({ submit });
  181. </script>
  182. <style lang="scss" scoped>
  183. .map-layout {
  184. display: flex;
  185. }
  186. .search {
  187. margin-right: 24px;
  188. flex: 1;
  189. }
  190. .map {
  191. flex: 0 0 auto;
  192. height: 600px;
  193. width: 800px;
  194. position: relative;
  195. }
  196. .tiles-select {
  197. position: absolute;
  198. z-index: 9999;
  199. right: 0px;
  200. top: 0px;
  201. }
  202. .latlng-result {
  203. margin-top: 16px;
  204. border-radius: 2px;
  205. border: 1px solid #d9d9d9;
  206. display: flex;
  207. align-items: center;
  208. flex-direction: column;
  209. justify-content: center;
  210. padding: 16px;
  211. p {
  212. font-size: 14px;
  213. color: #a7a7a7;
  214. line-height: 22px;
  215. min-width: 144px;
  216. text-align: left;
  217. }
  218. h3 {
  219. color: #f56c6c;
  220. font-weight: bold;
  221. font-size: 14px;
  222. margin-bottom: 8px;
  223. }
  224. &.success h3 {
  225. color: #67c23a;
  226. }
  227. }
  228. .name-result {
  229. margin-top: 10px;
  230. border: 1px solid #d9d9d9;
  231. border-radius: 2px;
  232. .address-item {
  233. padding: 10px 6px;
  234. margin: 0 10px;
  235. cursor: pointer;
  236. &.active {
  237. background: var(--el-color-primary-light-9);
  238. }
  239. &:not(:last-child) {
  240. padding-bottom: 6px;
  241. border-bottom: 1px solid #d9d9d9;
  242. }
  243. }
  244. }
  245. </style>
  246. <style lang="scss">
  247. .leaflet-container {
  248. background: #fff;
  249. }
  250. </style>