tiandituMap - 副本.vue 12 KB

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