tiandituMap.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461
  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. checkedDot: false,
  30. Tmap: null,
  31. option: {
  32. type: '',
  33. apikey: '',
  34. lng: '',
  35. lat: '',
  36. png: iconPath
  37. }
  38. };
  39. },
  40. created() {
  41. },
  42. mounted() {
  43. },
  44. beforeDestroy() {
  45. // EventBus.$off('someEvent'); // 确保在组件销毁前移除事件监听
  46. },
  47. methods: {
  48. compliteonLoadTianDiTu() {
  49. this.$emit('onLoadTianDiTu')
  50. },
  51. initCharts(lng, lat, iconPng) {
  52. this.option = {
  53. apikey: this.apiKey,
  54. lng,
  55. lat,
  56. png: iconPng || this.option.png,
  57. type: 'open'
  58. }
  59. // setTimeout(() => {
  60. // this.Trenderjs.getLocation()
  61. // }, 5000)
  62. },
  63. upDataCharts(lng, lat) {
  64. this.option = {
  65. ...this.option,
  66. type: 'Icon',
  67. lng,
  68. lat,
  69. png: this.customIcon || this.option.png,
  70. type: 'update'
  71. }
  72. },
  73. async nextPoint(lnglat) {
  74. var that = this;
  75. let params = {
  76. postStr: JSON.stringify({
  77. lon: lnglat.lng,
  78. lat: lnglat.lat,
  79. ver: 1,
  80. }),
  81. type: 'geocode',
  82. tk: that.apiKey
  83. }
  84. let resData = await tools.createRequest('https://api.tianditu.gov.cn/geocoder', params, true)
  85. if (resData.status === '0') {
  86. const info = tools.formatterAdressLocation(resData.result, 1)
  87. this.option = {
  88. ...this.option,
  89. apikey: this.apiKey,
  90. lng: lnglat.lng,
  91. lat: lnglat.lat,
  92. png: this.customIcon || this.option.png,
  93. type: 'update'
  94. }
  95. this.$emit('onSelect', info)
  96. } else {
  97. tools.createMessage('数据异常', 1000, false, 'error')
  98. }
  99. },
  100. }
  101. }
  102. </script>
  103. <script module="Trenderjs" lang="renderjs">
  104. import { EventBus } from "@/utils/vueBus.js"
  105. import iconPath from '@/static/images/point.png'
  106. var Tmap = null;
  107. const left = -(uni.upx2px(150));
  108. export default {
  109. data() {
  110. return {
  111. options: {},
  112. markerList: [],
  113. labelLsit: [],
  114. MyLngLat: {
  115. lng: '',
  116. lat: ''
  117. }
  118. }
  119. },
  120. mounted() {
  121. },
  122. beforeDestroy() {
  123. this.removeMapEvent()
  124. },
  125. methods: {
  126. onPosition() {
  127. // const { longitude, latitude } = { longitude: 114.414431, latitude: 30.482926 };
  128. // this.SelectedDot({ longitude, latitude })
  129. // this.MyLngLat = {
  130. // lng: longitude,
  131. // lat: latitude
  132. // }
  133. // console.log("this.MyLngLat = " , this.MyLngLat)
  134. // this.setMyIcon()
  135. EventBus.$emit('TianDiTuSearch', (res) => {
  136. const { longitude, latitude } = res;
  137. this.SelectedDot({ longitude, latitude })
  138. this.MyLngLat = {
  139. lng: longitude,
  140. lat: latitude
  141. }
  142. this.clearIcon()
  143. })
  144. },
  145. addMapEvent() {
  146. //移除地图的移动停止事件
  147. this.getLocation()
  148. this.removeMapEvent()
  149. if (Tmap) {
  150. Tmap.addEventListener("moveend", this.MapMoveend);
  151. Tmap.addEventListener('zoomend', this.handleZoomEvent);
  152. }
  153. },
  154. removeMapEvent() {
  155. //移除地图的移动停止事件
  156. try {
  157. if (Tmap) {
  158. Tmap.removeEventListener("moveend", this.MapMoveend);
  159. Tmap.removeEventListener("zoomend", this.handleZoomEvent);
  160. }
  161. } catch (error) {
  162. //TODO handle the exception
  163. }
  164. },
  165. // 移动地图,获取中心点
  166. MapMoveend(e) {
  167. try {
  168. console.log('------------------')
  169. // 地图移动,判断是否缩放/移动地图,还是通过点位定位
  170. if (!this.checkedDot) {
  171. // 自然缩放移动,刷新接口
  172. const v = e.target.getCenter()
  173. const Lng = v.getLng();
  174. const Lat = v.getLat();
  175. this.$emit("moveMap", { Lng, Lat })
  176. } else {
  177. // 点位移动,做处理
  178. this.$nextTick(() => {
  179. this.checkedDot = false;
  180. })
  181. }
  182. } catch (error) {
  183. //TODO handle the exception
  184. }
  185. },
  186. // 点击地图标注的点位
  187. clickMapSite({ type, target, lnglat, containerPoint }) {
  188. console.log('clickMapSite === ', 1)
  189. const Lng = lnglat.getLng();
  190. const Lat = lnglat.getLat();
  191. this.$emit('handleMapSite', { Lng, Lat })
  192. },
  193. // 天地图缩放事件
  194. handleZoomEvent(event) {
  195. if (!this.labelLsit || this.labelLsit.length === 0) return;
  196. this.labelLsit.forEach(el => {
  197. this.labelShowOrHide(el)
  198. })
  199. },
  200. // 标签的显示与隐藏
  201. labelShowOrHide(label) {
  202. if (!Tmap) return
  203. let currentZoom = Tmap.getZoom();
  204. if (currentZoom >= 16) {
  205. label.show()
  206. } else {
  207. label.hide()
  208. }
  209. },
  210. initTmap() {
  211. try {
  212. Tmap = null;
  213. Tmap = new T.Map('mapDiv', {
  214. projection: 'EPSG:4326',
  215. });
  216. } catch (error) {
  217. console.log('Tmap error= ', error)
  218. //TODO handle the exception
  219. }
  220. },
  221. setViewport() {
  222. try {
  223. console.log('window.devicePixelRatio = ', window.devicePixelRatio)
  224. console.log('window.getResolution 1 = 1 ', Tmap, window.screen.width * window.devicePixelRatio)
  225. const w = window.screen.width * window.devicePixelRatio
  226. const meta = document.createElement('meta')
  227. meta.name = 'viewport'
  228. meta.content =
  229. `width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no`
  230. // meta.content = `initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no`
  231. // meta.media = `(device-height: 568px)`
  232. document.head.appendChild(meta)
  233. } catch (err) {
  234. console.log('---------- ', err)
  235. }
  236. },
  237. initTMap(newValue, oldValue, ownerInstance, instance) {
  238. this.options = newValue
  239. if (newValue.type === 'open' && newValue.apikey) {
  240. if (!window.T) {
  241. // this.setViewport()
  242. const script = document.createElement('script')
  243. script.src = 'https://api.tianditu.gov.cn/api?v=4.0&tk=' + this.options.apikey
  244. script.onload = this.initChartsRender.bind(this)
  245. document.head.appendChild(script)
  246. } else {
  247. const {
  248. lng,
  249. lat
  250. } = this.options
  251. this.initTmap()
  252. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  253. this.$ownerInstance.callMethod('nextPoint', {
  254. lng,
  255. lat
  256. })
  257. Tmap.addEventListener('click', (e) => {
  258. console.log("nextPoint", e.lnglat)
  259. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  260. });
  261. }
  262. } else {
  263. // 选点,更新
  264. // const {
  265. // lng,
  266. // lat
  267. // } = newValue
  268. // this.upDataChartsRender(lng, lat)
  269. this.$emit("clickMap")
  270. }
  271. },
  272. initChartsRender() {
  273. this.$ownerInstance.callMethod('compliteonLoadTianDiTu')
  274. const {
  275. lng,
  276. lat
  277. } = this.options
  278. var that = this;
  279. this.initTmap()
  280. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  281. this.setIcon(lng, lat, true)
  282. this.$ownerInstance.callMethod('nextPoint', {
  283. lng,
  284. lat
  285. })
  286. Tmap.addEventListener('click', (e) => {
  287. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  288. });
  289. // Tmap.addEventListener("moveend", (e) => {
  290. // console.log('addEventListener = ' , e)
  291. // });
  292. this.addMapEvent()
  293. this.$emit("handleSearch")
  294. },
  295. upDataChartsRender(lng, lat) {
  296. if (!Tmap) return
  297. this.setIcon(lng, lat, true)
  298. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  299. },
  300. setIcon(lng, lat, isClear, iconU, info, max = false) {
  301. if (isClear) {
  302. this.clearIcon()
  303. }
  304. const icon = new T.Icon({
  305. iconUrl: iconU || this.options.png,
  306. iconSize: max ? new T.Point(60, 60) : new T.Point(45, 45),
  307. iconAnchor: new T.Point(15, 45)
  308. });
  309. const marker = new T.Marker(new T.LngLat(lng, lat), {
  310. icon
  311. });
  312. // 点位注册点击事件
  313. try {
  314. marker.removeEventListener("click", this.clickMapSite);
  315. } catch (error) {}
  316. marker.addEventListener("click", this.clickMapSite);
  317. // 缓存注册点击事件的点位
  318. this.markerList.push(marker)
  319. Tmap.addOverLay(marker);
  320. if (info) {
  321. try {
  322. var label = new T.Label({
  323. // text: this.setLable(info.locationName),
  324. text: info.locationName,
  325. position: marker.getLngLat(),
  326. offset: new T.Point(left, 30)
  327. });
  328. Tmap.addOverLay(label);
  329. label.setLngLat(marker.getLngLat());
  330. label.setBorderLine(0);
  331. // label.setBackgroundColor ('transparent');
  332. label.setFontSize(10);
  333. this.labelLsit.push(label)
  334. // 判断当前是否显示
  335. this.labelShowOrHide(label)
  336. } catch (error) {
  337. //TODO handle the exception
  338. console.log("info = error ", error)
  339. }
  340. }
  341. },
  342. // 移除点位,并注销点位绑定的点击事件
  343. clearIcon() {
  344. return new Promise((resolve, reject) => {
  345. try {
  346. (this.markerList || []).forEach(el => {
  347. try {
  348. el.removeEventListener("click", this.clickMapSite);
  349. } catch (error) {}
  350. });
  351. this.markerList = [];
  352. this.labelLsit = [];
  353. Tmap.clearOverLays();
  354. this.setMyIcon()
  355. } catch (error) {
  356. //TODO handle the exception
  357. } finally {
  358. resolve()
  359. }
  360. })
  361. },
  362. getLocation() {
  363. var lo = new T.Geolocation();
  364. console.log("天地图获取定位 = ", lo, lo.getStatus())
  365. try {
  366. lo.getCurrentPosition((res) => {
  367. console.log('获取定位', res)
  368. });
  369. } catch (error) {
  370. //TODO handle the exception
  371. console.log("天地图获取定位 = error ", error)
  372. }
  373. },
  374. SelectedDot(res, dot = false, level = 16) {
  375. this.checkedDot = dot
  376. const { longitude, latitude } = res;
  377. Tmap.panTo(new T.LngLat(longitude, latitude), level); // 移动到选中的点位
  378. },
  379. setMaxIcon() {
  380. },
  381. setMyIcon() {
  382. const { lng, lat } = this.MyLngLat || {}
  383. if (!Tmap || !lng || !lat) return;
  384. const icon = new T.Icon({
  385. iconUrl: iconPath,
  386. iconSize: new T.Point(45, 45),
  387. iconAnchor: new T.Point(15, 30)
  388. });
  389. const marker = new T.Marker(new T.LngLat(lng, lat), {
  390. icon
  391. });
  392. // Tmap.addOverLay(marker);
  393. try {
  394. marker.removeEventListener("click", () => {});
  395. } catch (error) {}
  396. marker.addEventListener("click", () => {});
  397. },
  398. setLable(locationName) {
  399. let el = '';
  400. for (let i = 0; i < locationName.length; i++) {
  401. if (!i || i % 10) {
  402. el += `${locationName[i]}`
  403. } else {
  404. el += `<br>${locationName[i]}`
  405. }
  406. };
  407. return `<p class='map-label'>${el}<p>`
  408. }
  409. },
  410. }
  411. </script>
  412. <style lang="scss">
  413. .mapDiv {
  414. width: 100%;
  415. height: 100%;
  416. }
  417. .position {
  418. position: fixed;
  419. right: 30rpx;
  420. bottom: 40vh;
  421. z-index: 1000;
  422. width: 80rpx;
  423. height: 80rpx;
  424. background-color: #fff;
  425. padding: 10rpx;
  426. .position-icon {
  427. width: 100%;
  428. height: 100%;
  429. path {
  430. fill: #3291F8;
  431. }
  432. }
  433. }
  434. </style>