tiandituMap.vue 12 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457
  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. const Lng = lnglat.getLng();
  189. const Lat = lnglat.getLat();
  190. this.$emit('handleMapSite', { Lng, Lat })
  191. },
  192. // 天地图缩放事件
  193. handleZoomEvent(event) {
  194. if (!this.labelLsit || this.labelLsit.length === 0) return;
  195. this.labelLsit.forEach(el => {
  196. this.labelShowOrHide(el)
  197. })
  198. },
  199. // 标签的显示与隐藏
  200. labelShowOrHide(label) {
  201. if (!Tmap) return
  202. let currentZoom = Tmap.getZoom();
  203. if (currentZoom >= 16) {
  204. label.show()
  205. } else {
  206. label.hide()
  207. }
  208. },
  209. initTmap() {
  210. try {
  211. Tmap = null;
  212. Tmap = new T.Map('mapDiv', {
  213. projection: 'EPSG:4326',
  214. });
  215. } catch (error) {
  216. console.log('Tmap error= ', error)
  217. //TODO handle the exception
  218. }
  219. },
  220. setViewport() {
  221. try {
  222. console.log('window.devicePixelRatio = ', window.devicePixelRatio)
  223. console.log('window.getResolution 1 = 1 ', Tmap , window.screen.width * window.devicePixelRatio)
  224. const w = window.screen.width * window.devicePixelRatio
  225. const meta = document.createElement('meta')
  226. meta.name = 'viewport'
  227. meta.content = `width=device-width,initial-scale=1.0, maximum-scale=1.0,minimum-scale=1.0, user-scalable=no`
  228. // meta.content = `initial-scale=1.0, maximum-scale=1.0, minimum-scale=1.0, user-scalable=no`
  229. // meta.media = `(device-height: 568px)`
  230. document.head.appendChild(meta)
  231. } catch (err) {
  232. console.log('---------- ', err)
  233. }
  234. },
  235. initTMap(newValue, oldValue, ownerInstance, instance) {
  236. this.options = newValue
  237. if (newValue.type === 'open' && newValue.apikey) {
  238. if (!window.T) {
  239. // this.setViewport()
  240. const script = document.createElement('script')
  241. script.src = 'https://api.tianditu.gov.cn/api?v=4.0&tk=' + this.options.apikey
  242. script.onload = this.initChartsRender.bind(this)
  243. document.head.appendChild(script)
  244. } else {
  245. const {
  246. lng,
  247. lat
  248. } = this.options
  249. this.initTmap()
  250. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  251. this.$ownerInstance.callMethod('nextPoint', {
  252. lng,
  253. lat
  254. })
  255. Tmap.addEventListener('click', (e) => {
  256. console.log("nextPoint", e.lnglat)
  257. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  258. });
  259. }
  260. } else {
  261. // 选点,更新
  262. // const {
  263. // lng,
  264. // lat
  265. // } = newValue
  266. // this.upDataChartsRender(lng, lat)
  267. this.$emit("clickMap")
  268. }
  269. },
  270. initChartsRender() {
  271. this.$ownerInstance.callMethod('compliteonLoadTianDiTu')
  272. const {
  273. lng,
  274. lat
  275. } = this.options
  276. var that = this;
  277. this.initTmap()
  278. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  279. this.setIcon(lng, lat, true)
  280. this.$ownerInstance.callMethod('nextPoint', {
  281. lng,
  282. lat
  283. })
  284. Tmap.addEventListener('click', (e) => {
  285. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  286. });
  287. // Tmap.addEventListener("moveend", (e) => {
  288. // console.log('addEventListener = ' , e)
  289. // });
  290. this.addMapEvent()
  291. this.$emit("handleSearch")
  292. },
  293. upDataChartsRender(lng, lat) {
  294. if (!Tmap) return
  295. this.setIcon(lng, lat, true)
  296. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  297. },
  298. setIcon(lng, lat, isClear, iconU, info) {
  299. if (isClear) {
  300. this.clearIcon()
  301. }
  302. const icon = new T.Icon({
  303. iconUrl: iconU || this.options.png,
  304. iconSize: new T.Point(45, 45),
  305. iconAnchor: new T.Point(15, 30)
  306. });
  307. const marker = new T.Marker(new T.LngLat(lng, lat), {
  308. icon
  309. });
  310. // 点位注册点击事件
  311. try {
  312. marker.removeEventListener("click", this.clickMapSite);
  313. } catch (error) {}
  314. marker.addEventListener("click", this.clickMapSite);
  315. // 缓存注册点击事件的点位
  316. this.markerList.push(marker)
  317. Tmap.addOverLay(marker);
  318. if (info) {
  319. try {
  320. var label = new T.Label({
  321. // text: this.setLable(info.locationName),
  322. text: info.locationName,
  323. position: marker.getLngLat(),
  324. offset: new T.Point(left, 30)
  325. });
  326. Tmap.addOverLay(label);
  327. label.setLngLat(marker.getLngLat());
  328. label.setBorderLine(0);
  329. // label.setBackgroundColor ('transparent');
  330. label.setFontSize(10);
  331. this.labelLsit.push(label)
  332. // 判断当前是否显示
  333. this.labelShowOrHide(label)
  334. } catch (error) {
  335. //TODO handle the exception
  336. console.log("info = error ", error)
  337. }
  338. }
  339. },
  340. // 移除点位,并注销点位绑定的点击事件
  341. clearIcon() {
  342. return new Promise((resolve, reject) => {
  343. try {
  344. (this.markerList || []).forEach(el => {
  345. try {
  346. el.removeEventListener("click", this.clickMapSite);
  347. } catch (error) {}
  348. });
  349. this.markerList = [];
  350. this.labelLsit = [];
  351. Tmap.clearOverLays();
  352. this.setMyIcon()
  353. } catch (error) {
  354. //TODO handle the exception
  355. } finally {
  356. resolve()
  357. }
  358. })
  359. },
  360. getLocation() {
  361. var lo = new T.Geolocation();
  362. console.log("天地图获取定位 = ", lo, lo.getStatus())
  363. try {
  364. lo.getCurrentPosition((res) => {
  365. console.log('获取定位', res)
  366. });
  367. } catch (error) {
  368. //TODO handle the exception
  369. console.log("天地图获取定位 = error ", error)
  370. }
  371. },
  372. SelectedDot(res , dot = false) {
  373. this.checkedDot = dot
  374. const { longitude, latitude } = res;
  375. Tmap.panTo(new T.LngLat(longitude, latitude), 16); // 移动到选中的点位
  376. },
  377. setMyIcon() {
  378. const { lng, lat } = this.MyLngLat || {}
  379. if (!Tmap || !lng || !lat) return;
  380. const icon = new T.Icon({
  381. iconUrl: iconPath,
  382. iconSize: new T.Point(45, 45),
  383. iconAnchor: new T.Point(15, 30)
  384. });
  385. const marker = new T.Marker(new T.LngLat(lng, lat), {
  386. icon
  387. });
  388. Tmap.addOverLay(marker);
  389. try {
  390. marker.removeEventListener("click", () => {});
  391. } catch (error) {}
  392. marker.addEventListener("click", () => {});
  393. },
  394. setLable(locationName) {
  395. let el = '';
  396. for (let i = 0; i < locationName.length; i++) {
  397. if (!i || i % 10) {
  398. el += `${locationName[i]}`
  399. } else {
  400. el += `<br>${locationName[i]}`
  401. }
  402. };
  403. return `<p class='map-label'>${el}<p>`
  404. }
  405. },
  406. }
  407. </script>
  408. <style lang="scss">
  409. .mapDiv {
  410. width: 100%;
  411. height: 100%;
  412. }
  413. .position {
  414. position: fixed;
  415. right: 30rpx;
  416. bottom: 40vh;
  417. z-index: 1000;
  418. width: 80rpx;
  419. height: 80rpx;
  420. background-color: #fff;
  421. padding: 10rpx;
  422. .position-icon {
  423. width: 100%;
  424. height: 100%;
  425. path {
  426. fill: #3291F8;
  427. }
  428. }
  429. }
  430. </style>