tiandituMap.vue 9.5 KB

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