tiandituMap.vue 8.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319
  1. <template>
  2. <view id="mapDiv" class="mapDiv" :apikey="apiKey" :prop="option" :change:prop="Trenderjs.initTMap">
  3. <!-- :change:prop="Trenderjs.initTMap" -->
  4. <!-- <view class="position" @click.stop="Trenderjs.onPosition">
  5. <svg t="1734080022350" class="position-icon" viewBox="0 0 1024 1024" version="1.1" xmlns="http://www.w3.org/2000/svg"
  6. p-id="4287" xmlns:xlink="http://www.w3.org/1999/xlink">
  7. <path d="M512 512m-80 0a80 80 0 1 0 160 0 80 80 0 1 0-160 0Z" p-id="4288"></path>
  8. <path
  9. 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"
  10. p-id="4289"></path>
  11. </svg>
  12. </view> -->
  13. </view>
  14. </template>
  15. <script>
  16. import tools from './tools.js'
  17. import iconPath from '@/static/images/point.png'
  18. import { EventBus } from "@/utils/vueBus.js"
  19. export default {
  20. name: "tianditu",
  21. props: {
  22. apiKey: {
  23. type: String,
  24. require: true,
  25. default: ''
  26. },
  27. },
  28. data() {
  29. return {
  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. EventBus.$on('TianDiTuSite', (res) => {
  44. const { longitude, latitude } = res;
  45. this.Trenderjs.SelectedDot(longitude, latitude)
  46. console.log('Event received with payload:', res);
  47. });
  48. },
  49. beforeDestroy() {
  50. EventBus.$off('someEvent'); // 确保在组件销毁前移除事件监听
  51. },
  52. methods: {
  53. compliteonLoadTianDiTu() {
  54. this.$emit('onLoadTianDiTu')
  55. },
  56. initCharts(lng, lat, iconPng) {
  57. this.option = {
  58. apikey: this.apiKey,
  59. lng,
  60. lat,
  61. png: iconPng || this.option.png,
  62. type: 'open'
  63. }
  64. // setTimeout(() => {
  65. // this.Trenderjs.getLocation()
  66. // }, 5000)
  67. },
  68. upDataCharts(lng, lat) {
  69. this.option = {
  70. ...this.option,
  71. type: 'Icon',
  72. lng,
  73. lat,
  74. png: this.customIcon || this.option.png,
  75. type: 'update'
  76. }
  77. },
  78. async nextPoint(lnglat) {
  79. var that = this;
  80. let params = {
  81. postStr: JSON.stringify({
  82. lon: lnglat.lng,
  83. lat: lnglat.lat,
  84. ver: 1,
  85. }),
  86. type: 'geocode',
  87. tk: that.apiKey
  88. }
  89. let resData = await tools.createRequest('https://api.tianditu.gov.cn/geocoder', params, true)
  90. if (resData.status === '0') {
  91. const info = tools.formatterAdressLocation(resData.result, 1)
  92. this.option = {
  93. ...this.option,
  94. apikey: this.apiKey,
  95. lng: lnglat.lng,
  96. lat: lnglat.lat,
  97. png: this.customIcon || this.option.png,
  98. type: 'update'
  99. }
  100. this.$emit('onSelect', info)
  101. } else {
  102. tools.createMessage('数据异常', 1000, false, 'error')
  103. }
  104. },
  105. }
  106. }
  107. </script>
  108. <script module="Trenderjs" lang="renderjs">
  109. var Tmap = null;
  110. export default {
  111. data() {
  112. return {
  113. options: {},
  114. }
  115. },
  116. mounted() {
  117. },
  118. beforeDestroy() {
  119. this.removeMapMoveend()
  120. },
  121. methods: {
  122. onPosition(e) {
  123. e.
  124. console.log("---------------------")
  125. },
  126. addMapMoveend() {
  127. //移除地图的移动停止事件
  128. console.log("Tmap = 11", Tmap)
  129. this.removeMapMoveend()
  130. if (Tmap) Tmap.addEventListener("moveend", this.MapMoveend);
  131. },
  132. removeMapMoveend() {
  133. //移除地图的移动停止事件
  134. try {
  135. if (Tmap) Tmap.removeEventListener("moveend", this.MapMoveend);
  136. } catch (error) {
  137. //TODO handle the exception
  138. }
  139. },
  140. MapMoveend(e) {
  141. try {
  142. const v = e.target.getCenter()
  143. const Lng = v.getLng();
  144. const Lat = v.getLat()
  145. console.log("MapMoveend 99= ", Lng, Lat)
  146. this.$emit("moveMap", { Lng, Lat })
  147. } catch (error) {
  148. //TODO handle the exception
  149. }
  150. },
  151. initTmap() {
  152. try {
  153. Tmap = null;
  154. Tmap = new T.Map('mapDiv', {
  155. projection: 'EPSG:4326',
  156. });
  157. } catch (error) {
  158. //TODO handle the exception
  159. }
  160. },
  161. initTMap(newValue, oldValue, ownerInstance, instance) {
  162. this.options = newValue
  163. if (newValue.type === 'open' && newValue.apikey) {
  164. if (!window.T) {
  165. const script = document.createElement('script')
  166. script.src = 'https://api.tianditu.gov.cn/api?v=4.0&tk=' + this.options.apikey
  167. script.onload = this.initChartsRender.bind(this)
  168. document.head.appendChild(script)
  169. setTimeout(() => {
  170. this.$emit("handleSearch")
  171. }, 500);
  172. } else {
  173. const {
  174. lng,
  175. lat
  176. } = this.options
  177. this.initTmap()
  178. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  179. this.$ownerInstance.callMethod('nextPoint', {
  180. lng,
  181. lat
  182. })
  183. Tmap.addEventListener('click', (e) => {
  184. console.log("nextPoint", e.lnglat)
  185. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  186. });
  187. }
  188. } else {
  189. // 选点,更新
  190. // const {
  191. // lng,
  192. // lat
  193. // } = newValue
  194. // this.upDataChartsRender(lng, lat)
  195. this.$emit("clickMap")
  196. }
  197. },
  198. initChartsRender() {
  199. this.$ownerInstance.callMethod('compliteonLoadTianDiTu')
  200. const {
  201. lng,
  202. lat
  203. } = this.options
  204. var that = this;
  205. this.initTmap()
  206. console.log("77777777777", Tmap)
  207. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  208. this.setIcon(lng, lat, true)
  209. this.$ownerInstance.callMethod('nextPoint', {
  210. lng,
  211. lat
  212. })
  213. Tmap.addEventListener('click', (e) => {
  214. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  215. });
  216. // Tmap.addEventListener("moveend", (e) => {
  217. // console.log('addEventListener = ' , e)
  218. // });
  219. this.addMapMoveend()
  220. },
  221. upDataChartsRender(lng, lat) {
  222. if (!Tmap) return
  223. this.setIcon(lng, lat, true)
  224. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  225. },
  226. setIcon(lng, lat, isClear, iconU) {
  227. if (isClear) {
  228. this.clearIcon()
  229. }
  230. const icon = new T.Icon({
  231. iconUrl: iconU || this.options.png,
  232. iconSize: new T.Point(30, 30),
  233. iconAnchor: new T.Point(15, 30)
  234. });
  235. const marker = new T.Marker(new T.LngLat(lng, lat), {
  236. icon
  237. });
  238. Tmap.addOverLay(marker);
  239. },
  240. clearIcon() {
  241. Tmap.clearOverLays()
  242. },
  243. getLocation() {
  244. var lo = new T.Geolocation();
  245. console.log('获取定位', lo)
  246. const fn = (e) => {
  247. console.log('eeeeeeeeeeeeeeeeeeee', e)
  248. // if (this.getStatus() == 0) {
  249. // map.centerAndZoom(e.lnglat, 15)
  250. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  251. // var marker = new T.Marker(e.lnglat);
  252. // map.addOverLay(marker);
  253. // }
  254. // if (this.getStatus() == 1) {
  255. // map.centerAndZoom(e.lnglat, e.level)
  256. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  257. // var marker = new T.Marker(e.lnglat);
  258. // map.addOverLay(marker);
  259. // }
  260. }
  261. lo.getCurrentPosition(fn);
  262. // if (this.getStatus() == 0) {
  263. // map.centerAndZoom(e.lnglat, 15)
  264. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  265. // var marker = new T.Marker(e.lnglat);
  266. // map.addOverLay(marker);
  267. // }
  268. // if (this.getStatus() == 1) {
  269. // map.centerAndZoom(e.lnglat, e.level)
  270. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  271. // var marker = new T.Marker(e.lnglat);
  272. // map.addOverLay(marker);
  273. // }
  274. },
  275. SelectedDot(lng, lat) {
  276. Tmap.panTo(new T.LngLat(lng, lat), 16); // 移动到选中的点位
  277. }
  278. },
  279. }
  280. </script>
  281. <style lang="scss">
  282. .mapDiv {
  283. width: 100%;
  284. height: 100%;
  285. }
  286. .position {
  287. position: fixed;
  288. right: 30rpx;
  289. bottom: 50vh;
  290. z-index: 1000;
  291. width: 80rpx;
  292. height: 80rpx;
  293. background-color: #fff;
  294. padding: 10rpx;
  295. .position-icon {
  296. width: 100%;
  297. height: 100%;
  298. path {
  299. fill: #3291F8;
  300. }
  301. }
  302. }
  303. </style>