tiandituMap.vue 11 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433
  1. <template>
  2. <view id="mapDiv" class="mapDiv" :apikey="apiKey" :prop="option" :change:prop="Trenderjs.initTMap">
  3. <view class="position" @click.stop="Trenderjs.onPosition">
  4. <svg t="1734080022350" class="position-icon" viewBox="0 0 1024 1024" version="1.1"
  5. xmlns="http://www.w3.org/2000/svg" p-id="4287" xmlns:xlink="http://www.w3.org/1999/xlink">
  6. <path d="M512 512m-80 0a80 80 0 1 0 160 0 80 80 0 1 0-160 0Z" p-id="4288"></path>
  7. <path
  8. d="M960 480h-33.632C910.752 276.16 747.84 113.248 544 97.632V64a32 32 0 1 0-64 0v33.632C276.16 113.248 113.248 276.16 97.632 480H64a32 32 0 0 0 0 64h33.632C113.248 747.84 276.16 910.752 480 926.368V960a32 32 0 1 0 64 0v-33.632C747.84 910.752 910.752 747.84 926.368 544H960a32 32 0 1 0 0-64zM544 862.368V800a32 32 0 1 0-64 0v62.368C311.424 847.104 176.896 712.576 161.632 544H224a32 32 0 1 0 0-64H161.632C176.896 311.424 311.424 176.896 480 161.632V224a32 32 0 0 0 64 0V161.632c168.576 15.296 303.104 149.792 318.368 318.368H800a32 32 0 1 0 0 64h62.368c-15.264 168.576-149.792 303.104-318.368 318.368z"
  9. p-id="4289"></path>
  10. </svg>
  11. </view>
  12. </view>
  13. </template>
  14. <script>
  15. import tools from './tools.js'
  16. import iconPath from '@/static/images/point.png'
  17. import { EventBus } from "@/utils/vueBus.js"
  18. export default {
  19. name: "tianditu",
  20. props: {
  21. apiKey: {
  22. type: String,
  23. require: true,
  24. default: ''
  25. },
  26. },
  27. data() {
  28. return {
  29. Tmap: null,
  30. option: {
  31. type: '',
  32. apikey: '',
  33. lng: '',
  34. lat: '',
  35. png: iconPath
  36. }
  37. };
  38. },
  39. created() {
  40. },
  41. mounted() {
  42. },
  43. beforeDestroy() {
  44. // EventBus.$off('someEvent'); // 确保在组件销毁前移除事件监听
  45. },
  46. methods: {
  47. compliteonLoadTianDiTu() {
  48. this.$emit('onLoadTianDiTu')
  49. },
  50. initCharts(lng, lat, iconPng) {
  51. this.option = {
  52. apikey: this.apiKey,
  53. lng,
  54. lat,
  55. png: iconPng || this.option.png,
  56. type: 'open'
  57. }
  58. // setTimeout(() => {
  59. // this.Trenderjs.getLocation()
  60. // }, 5000)
  61. },
  62. upDataCharts(lng, lat) {
  63. this.option = {
  64. ...this.option,
  65. type: 'Icon',
  66. lng,
  67. lat,
  68. png: this.customIcon || this.option.png,
  69. type: 'update'
  70. }
  71. },
  72. async nextPoint(lnglat) {
  73. var that = this;
  74. let params = {
  75. postStr: JSON.stringify({
  76. lon: lnglat.lng,
  77. lat: lnglat.lat,
  78. ver: 1,
  79. }),
  80. type: 'geocode',
  81. tk: that.apiKey
  82. }
  83. let resData = await tools.createRequest('https://api.tianditu.gov.cn/geocoder', params, true)
  84. if (resData.status === '0') {
  85. const info = tools.formatterAdressLocation(resData.result, 1)
  86. this.option = {
  87. ...this.option,
  88. apikey: this.apiKey,
  89. lng: lnglat.lng,
  90. lat: lnglat.lat,
  91. png: this.customIcon || this.option.png,
  92. type: 'update'
  93. }
  94. this.$emit('onSelect', info)
  95. } else {
  96. tools.createMessage('数据异常', 1000, false, 'error')
  97. }
  98. },
  99. }
  100. }
  101. </script>
  102. <script module="Trenderjs" lang="renderjs">
  103. import { EventBus } from "@/utils/vueBus.js"
  104. import iconPath from '@/static/images/point.png'
  105. var Tmap = null;
  106. export default {
  107. data() {
  108. return {
  109. options: {},
  110. markerList: [],
  111. labelLsit: [],
  112. MyLngLat: {
  113. lng: '',
  114. lat: ''
  115. }
  116. }
  117. },
  118. mounted() {
  119. },
  120. beforeDestroy() {
  121. this.removeMapEvent()
  122. },
  123. methods: {
  124. onPosition() {
  125. // const { longitude, latitude } = { longitude: 114.414431, latitude: 30.482926 };
  126. // this.SelectedDot({ longitude, latitude })
  127. // this.MyLngLat = {
  128. // lng: longitude,
  129. // lat: latitude
  130. // }
  131. // console.log("this.MyLngLat = " , this.MyLngLat)
  132. // this.setMyIcon()
  133. EventBus.$emit('TianDiTuSearch', (res) => {
  134. const { longitude, latitude } = res;
  135. this.SelectedDot({ longitude, latitude })
  136. this.MyLngLat = {
  137. lng: longitude,
  138. lat: latitude
  139. }
  140. this.setMyIcon()
  141. })
  142. },
  143. addMapEvent() {
  144. //移除地图的移动停止事件
  145. this.getLocation()
  146. this.removeMapEvent()
  147. if (Tmap) {
  148. Tmap.addEventListener("moveend", this.MapMoveend);
  149. Tmap.addEventListener('zoomend', this.handleZoomEvent);
  150. }
  151. },
  152. removeMapEvent() {
  153. //移除地图的移动停止事件
  154. try {
  155. if (Tmap) {
  156. Tmap.removeEventListener("moveend", this.MapMoveend);
  157. Tmap.removeEventListener("zoomend", this.MapMoveend);
  158. }
  159. } catch (error) {
  160. //TODO handle the exception
  161. }
  162. },
  163. // 移动地图,获取中心点
  164. MapMoveend(e) {
  165. try {
  166. const v = e.target.getCenter()
  167. const Lng = v.getLng();
  168. const Lat = v.getLat();
  169. this.$emit("moveMap", { Lng, Lat })
  170. } catch (error) {
  171. //TODO handle the exception
  172. }
  173. },
  174. // // 移动地图,获取中心点
  175. // MapMoveend: throttle((e) => {
  176. // console.log("防抖防抖防抖1111", e.target.getCenter(), this)
  177. // const v = e.target.getCenter()
  178. // const Lng = v.getLng();
  179. // const Lat = v.getLat();
  180. // this.$emit("moveMap", { Lng, Lat })
  181. // }, 500),
  182. // 点击地图标注的点位
  183. clickMapSite({ type, target, lnglat, containerPoint }) {
  184. const Lng = lnglat.getLng();
  185. const Lat = lnglat.getLat();
  186. this.$emit('handleMapSite', { Lng, Lat })
  187. },
  188. // 天地图缩放事件
  189. handleZoomEvent(event) {
  190. if (!this.labelLsit || this.labelLsit.length === 0) return;
  191. // let currentZoom = event.target.getZoom();
  192. // console.log("handleZoomEvent = 天地图缩放事件 " , currentZoom)
  193. this.labelLsit.forEach(el => {
  194. this.labelShowOrHide(el)
  195. })
  196. },
  197. // 标签的显示与隐藏
  198. labelShowOrHide(label) {
  199. if (!Tmap) return
  200. let currentZoom = Tmap.getZoom();
  201. if (currentZoom >= 16) {
  202. label.show()
  203. } else {
  204. label.hide()
  205. }
  206. },
  207. initTmap() {
  208. try {
  209. Tmap = null;
  210. Tmap = new T.Map('mapDiv', {
  211. projection: 'EPSG:4326',
  212. });
  213. } catch (error) {
  214. //TODO handle the exception
  215. }
  216. },
  217. initTMap(newValue, oldValue, ownerInstance, instance) {
  218. this.options = newValue
  219. if (newValue.type === 'open' && newValue.apikey) {
  220. if (!window.T) {
  221. const script = document.createElement('script')
  222. script.src = 'https://api.tianditu.gov.cn/api?v=4.0&tk=' + this.options.apikey
  223. script.onload = this.initChartsRender.bind(this)
  224. document.head.appendChild(script)
  225. } else {
  226. const {
  227. lng,
  228. lat
  229. } = this.options
  230. this.initTmap()
  231. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  232. this.$ownerInstance.callMethod('nextPoint', {
  233. lng,
  234. lat
  235. })
  236. Tmap.addEventListener('click', (e) => {
  237. console.log("nextPoint", e.lnglat)
  238. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  239. });
  240. }
  241. } else {
  242. // 选点,更新
  243. // const {
  244. // lng,
  245. // lat
  246. // } = newValue
  247. // this.upDataChartsRender(lng, lat)
  248. this.$emit("clickMap")
  249. }
  250. },
  251. initChartsRender() {
  252. this.$ownerInstance.callMethod('compliteonLoadTianDiTu')
  253. const {
  254. lng,
  255. lat
  256. } = this.options
  257. var that = this;
  258. this.initTmap()
  259. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  260. this.setIcon(lng, lat, true)
  261. this.$ownerInstance.callMethod('nextPoint', {
  262. lng,
  263. lat
  264. })
  265. Tmap.addEventListener('click', (e) => {
  266. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  267. });
  268. // Tmap.addEventListener("moveend", (e) => {
  269. // console.log('addEventListener = ' , e)
  270. // });
  271. this.addMapEvent()
  272. this.$emit("handleSearch")
  273. },
  274. upDataChartsRender(lng, lat) {
  275. if (!Tmap) return
  276. this.setIcon(lng, lat, true)
  277. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  278. },
  279. setIcon(lng, lat, isClear, iconU, info) {
  280. if (isClear) {
  281. this.clearIcon()
  282. }
  283. const icon = new T.Icon({
  284. iconUrl: iconU || this.options.png,
  285. iconSize: new T.Point(45, 45),
  286. iconAnchor: new T.Point(15, 30)
  287. });
  288. const marker = new T.Marker(new T.LngLat(lng, lat), {
  289. icon
  290. });
  291. // 点位注册点击事件
  292. try {
  293. marker.removeEventListener("click", this.clickMapSite);
  294. } catch (error) {}
  295. marker.addEventListener("click", this.clickMapSite);
  296. // 缓存注册点击事件的点位
  297. this.markerList.push(marker)
  298. Tmap.addOverLay(marker);
  299. if (info) {
  300. try {
  301. var label = new T.Label({
  302. // text: this.setLable(info.locationName),
  303. text: info.locationName,
  304. position: marker.getLngLat(),
  305. offset: new T.Point(-50, 30)
  306. });
  307. Tmap.addOverLay(label);
  308. label.setLngLat(marker.getLngLat());
  309. label.setBorderLine(0);
  310. // label.setBackgroundColor ('transparent');
  311. label.setFontSize(10);
  312. this.labelLsit.push(label)
  313. // 判断当前是否显示
  314. this.labelShowOrHide(label)
  315. } catch (error) {
  316. //TODO handle the exception
  317. console.log("info = error ", error)
  318. }
  319. }
  320. },
  321. // 移除点位,并注销点位绑定的点击事件
  322. clearIcon() {
  323. return new Promise((resolve, reject) => {
  324. try {
  325. (this.markerList || []).forEach(el => {
  326. try {
  327. el.removeEventListener("click", this.clickMapSite);
  328. } catch (error) {}
  329. });
  330. this.markerList = [];
  331. this.labelLsit = [];
  332. Tmap.clearOverLays();
  333. this.setMyIcon()
  334. } catch (error) {
  335. //TODO handle the exception
  336. } finally {
  337. resolve()
  338. }
  339. })
  340. },
  341. getLocation() {
  342. var lo = new T.Geolocation();
  343. console.log("天地图获取定位 = ", lo, lo.getStatus())
  344. try {
  345. lo.getCurrentPosition((res) => {
  346. console.log('获取定位', res)
  347. });
  348. } catch (error) {
  349. //TODO handle the exception
  350. console.log("天地图获取定位 = error ", error)
  351. }
  352. },
  353. SelectedDot(res) {
  354. const { longitude, latitude } = res;
  355. Tmap.panTo(new T.LngLat(longitude, latitude), 16); // 移动到选中的点位
  356. },
  357. setMyIcon() {
  358. const { lng, lat } = this.MyLngLat || {}
  359. if (!Tmap || !lng || !lat) return;
  360. const icon = new T.Icon({
  361. iconUrl: iconPath,
  362. iconSize: new T.Point(45, 45),
  363. iconAnchor: new T.Point(15, 30)
  364. });
  365. const marker = new T.Marker(new T.LngLat(lng, lat), {
  366. icon
  367. });
  368. Tmap.addOverLay(marker);
  369. try {
  370. marker.removeEventListener("click", () => {});
  371. } catch (error) {}
  372. marker.addEventListener("click", () => {});
  373. },
  374. setLable(locationName) {
  375. let el = '';
  376. for (let i = 0; i < locationName.length; i++) {
  377. if (!i || i % 10) {
  378. el += `${locationName[i]}`
  379. } else {
  380. el += `<br>${locationName[i]}`
  381. }
  382. };
  383. return `<p class='map-label'>${el}<p>`
  384. }
  385. },
  386. }
  387. </script>
  388. <style lang="scss">
  389. .mapDiv {
  390. width: 100%;
  391. height: 100%;
  392. }
  393. .position {
  394. position: fixed;
  395. right: 30rpx;
  396. bottom: 40vh;
  397. z-index: 1000;
  398. width: 80rpx;
  399. height: 80rpx;
  400. background-color: #fff;
  401. padding: 10rpx;
  402. .position-icon {
  403. width: 100%;
  404. height: 100%;
  405. path {
  406. fill: #3291F8;
  407. }
  408. }
  409. }
  410. </style>