tiandituMap.vue 10 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423
  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. const left = -(uni.upx2px(150));
  107. export default {
  108. data() {
  109. return {
  110. options: {},
  111. markerList: [],
  112. labelLsit: [],
  113. MyLngLat: {
  114. lng: '',
  115. lat: ''
  116. }
  117. }
  118. },
  119. mounted() {
  120. },
  121. beforeDestroy() {
  122. this.removeMapEvent()
  123. },
  124. methods: {
  125. onPosition() {
  126. // const { longitude, latitude } = { longitude: 114.414431, latitude: 30.482926 };
  127. // this.SelectedDot({ longitude, latitude })
  128. // this.MyLngLat = {
  129. // lng: longitude,
  130. // lat: latitude
  131. // }
  132. // console.log("this.MyLngLat = " , this.MyLngLat)
  133. // this.setMyIcon()
  134. EventBus.$emit('TianDiTuSearch', (res) => {
  135. const { longitude, latitude } = res;
  136. this.SelectedDot({ longitude, latitude })
  137. this.MyLngLat = {
  138. lng: longitude,
  139. lat: latitude
  140. }
  141. this.setMyIcon()
  142. })
  143. },
  144. addMapEvent() {
  145. //移除地图的移动停止事件
  146. this.getLocation()
  147. this.removeMapEvent()
  148. if (Tmap) {
  149. Tmap.addEventListener("moveend", this.MapMoveend);
  150. Tmap.addEventListener('zoomend', this.handleZoomEvent);
  151. }
  152. },
  153. removeMapEvent() {
  154. //移除地图的移动停止事件
  155. try {
  156. if (Tmap) {
  157. Tmap.removeEventListener("moveend", this.MapMoveend);
  158. Tmap.removeEventListener("zoomend", this.handleZoomEvent);
  159. }
  160. } catch (error) {
  161. //TODO handle the exception
  162. }
  163. },
  164. // 移动地图,获取中心点
  165. MapMoveend(e) {
  166. try {
  167. const v = e.target.getCenter()
  168. const Lng = v.getLng();
  169. const Lat = v.getLat();
  170. this.$emit("moveMap", { Lng, Lat })
  171. } catch (error) {
  172. //TODO handle the exception
  173. }
  174. },
  175. // 点击地图标注的点位
  176. clickMapSite({ type, target, lnglat, containerPoint }) {
  177. const Lng = lnglat.getLng();
  178. const Lat = lnglat.getLat();
  179. this.$emit('handleMapSite', { Lng, Lat })
  180. },
  181. // 天地图缩放事件
  182. handleZoomEvent(event) {
  183. if (!this.labelLsit || this.labelLsit.length === 0) return;
  184. this.labelLsit.forEach(el => {
  185. this.labelShowOrHide(el)
  186. })
  187. },
  188. // 标签的显示与隐藏
  189. labelShowOrHide(label) {
  190. if (!Tmap) return
  191. let currentZoom = Tmap.getZoom();
  192. if (currentZoom >= 16) {
  193. label.show()
  194. } else {
  195. label.hide()
  196. }
  197. },
  198. initTmap() {
  199. try {
  200. Tmap = null;
  201. Tmap = new T.Map('mapDiv', {
  202. projection: 'EPSG:4326',
  203. });
  204. } catch (error) {
  205. //TODO handle the exception
  206. }
  207. },
  208. initTMap(newValue, oldValue, ownerInstance, instance) {
  209. this.options = newValue
  210. if (newValue.type === 'open' && newValue.apikey) {
  211. if (!window.T) {
  212. const script = document.createElement('script')
  213. script.src = 'https://api.tianditu.gov.cn/api?v=4.0&tk=' + this.options.apikey
  214. script.onload = this.initChartsRender.bind(this)
  215. document.head.appendChild(script)
  216. } else {
  217. const {
  218. lng,
  219. lat
  220. } = this.options
  221. this.initTmap()
  222. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  223. this.$ownerInstance.callMethod('nextPoint', {
  224. lng,
  225. lat
  226. })
  227. Tmap.addEventListener('click', (e) => {
  228. console.log("nextPoint", e.lnglat)
  229. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  230. });
  231. }
  232. } else {
  233. // 选点,更新
  234. // const {
  235. // lng,
  236. // lat
  237. // } = newValue
  238. // this.upDataChartsRender(lng, lat)
  239. this.$emit("clickMap")
  240. }
  241. },
  242. initChartsRender() {
  243. this.$ownerInstance.callMethod('compliteonLoadTianDiTu')
  244. const {
  245. lng,
  246. lat
  247. } = this.options
  248. var that = this;
  249. this.initTmap()
  250. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  251. this.setIcon(lng, lat, true)
  252. this.$ownerInstance.callMethod('nextPoint', {
  253. lng,
  254. lat
  255. })
  256. Tmap.addEventListener('click', (e) => {
  257. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  258. });
  259. // Tmap.addEventListener("moveend", (e) => {
  260. // console.log('addEventListener = ' , e)
  261. // });
  262. this.addMapEvent()
  263. this.$emit("handleSearch")
  264. },
  265. upDataChartsRender(lng, lat) {
  266. if (!Tmap) return
  267. this.setIcon(lng, lat, true)
  268. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  269. },
  270. setIcon(lng, lat, isClear, iconU, info) {
  271. if (isClear) {
  272. this.clearIcon()
  273. }
  274. const icon = new T.Icon({
  275. iconUrl: iconU || this.options.png,
  276. iconSize: new T.Point(45, 45),
  277. iconAnchor: new T.Point(15, 30)
  278. });
  279. const marker = new T.Marker(new T.LngLat(lng, lat), {
  280. icon
  281. });
  282. // 点位注册点击事件
  283. try {
  284. marker.removeEventListener("click", this.clickMapSite);
  285. } catch (error) {}
  286. marker.addEventListener("click", this.clickMapSite);
  287. // 缓存注册点击事件的点位
  288. this.markerList.push(marker)
  289. Tmap.addOverLay(marker);
  290. if (info) {
  291. try {
  292. var label = new T.Label({
  293. // text: this.setLable(info.locationName),
  294. text: info.locationName,
  295. position: marker.getLngLat(),
  296. offset: new T.Point(left, 30)
  297. });
  298. Tmap.addOverLay(label);
  299. label.setLngLat(marker.getLngLat());
  300. label.setBorderLine(0);
  301. // label.setBackgroundColor ('transparent');
  302. label.setFontSize(10);
  303. this.labelLsit.push(label)
  304. // 判断当前是否显示
  305. this.labelShowOrHide(label)
  306. } catch (error) {
  307. //TODO handle the exception
  308. console.log("info = error ", error)
  309. }
  310. }
  311. },
  312. // 移除点位,并注销点位绑定的点击事件
  313. clearIcon() {
  314. return new Promise((resolve, reject) => {
  315. try {
  316. (this.markerList || []).forEach(el => {
  317. try {
  318. el.removeEventListener("click", this.clickMapSite);
  319. } catch (error) {}
  320. });
  321. this.markerList = [];
  322. this.labelLsit = [];
  323. Tmap.clearOverLays();
  324. this.setMyIcon()
  325. } catch (error) {
  326. //TODO handle the exception
  327. } finally {
  328. resolve()
  329. }
  330. })
  331. },
  332. getLocation() {
  333. var lo = new T.Geolocation();
  334. console.log("天地图获取定位 = ", lo, lo.getStatus())
  335. try {
  336. lo.getCurrentPosition((res) => {
  337. console.log('获取定位', res)
  338. });
  339. } catch (error) {
  340. //TODO handle the exception
  341. console.log("天地图获取定位 = error ", error)
  342. }
  343. },
  344. SelectedDot(res) {
  345. const { longitude, latitude } = res;
  346. Tmap.panTo(new T.LngLat(longitude, latitude), 16); // 移动到选中的点位
  347. },
  348. setMyIcon() {
  349. const { lng, lat } = this.MyLngLat || {}
  350. if (!Tmap || !lng || !lat) return;
  351. const icon = new T.Icon({
  352. iconUrl: iconPath,
  353. iconSize: new T.Point(45, 45),
  354. iconAnchor: new T.Point(15, 30)
  355. });
  356. const marker = new T.Marker(new T.LngLat(lng, lat), {
  357. icon
  358. });
  359. Tmap.addOverLay(marker);
  360. try {
  361. marker.removeEventListener("click", () => {});
  362. } catch (error) {}
  363. marker.addEventListener("click", () => {});
  364. },
  365. setLable(locationName) {
  366. let el = '';
  367. for (let i = 0; i < locationName.length; i++) {
  368. if (!i || i % 10) {
  369. el += `${locationName[i]}`
  370. } else {
  371. el += `<br>${locationName[i]}`
  372. }
  373. };
  374. return `<p class='map-label'>${el}<p>`
  375. }
  376. },
  377. }
  378. </script>
  379. <style lang="scss">
  380. .mapDiv {
  381. width: 100%;
  382. height: 100%;
  383. }
  384. .position {
  385. position: fixed;
  386. right: 30rpx;
  387. bottom: 40vh;
  388. z-index: 1000;
  389. width: 80rpx;
  390. height: 80rpx;
  391. background-color: #fff;
  392. padding: 10rpx;
  393. .position-icon {
  394. width: 100%;
  395. height: 100%;
  396. path {
  397. fill: #3291F8;
  398. }
  399. }
  400. }
  401. </style>