tiandituMap.vue 6.2 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244
  1. <template>
  2. <view id="mapDiv" class="mapDiv" :apikey="apiKey" :prop="option" :change:prop="Trenderjs.initTMap">
  3. <!-- :change:prop="Trenderjs.initTMap" -->
  4. </view>
  5. </template>
  6. <script>
  7. import tools from './tools.js'
  8. import iconPath from '@/static/images/point.png'
  9. export default {
  10. name: "tianditu",
  11. props: {
  12. apiKey: {
  13. type: String,
  14. require: true,
  15. default: ''
  16. },
  17. },
  18. data() {
  19. return {
  20. Tmap: null,
  21. option: {
  22. type: '',
  23. apikey: '',
  24. lng: '',
  25. lat: '',
  26. png: iconPath
  27. }
  28. };
  29. },
  30. methods: {
  31. compliteonLoadTianDiTu() {
  32. this.$emit('onLoadTianDiTu')
  33. },
  34. initCharts(lng, lat , iconPng) {
  35. this.option = {
  36. apikey: this.apiKey,
  37. lng,
  38. lat,
  39. png: iconPng || this.option.png,
  40. type: 'open'
  41. }
  42. // console.log()
  43. // this.Trenderjs.createMap(this.apiKey)
  44. setTimeout(() => {
  45. this.Trenderjs.getLocation()
  46. },5000)
  47. },
  48. upDataCharts(lng, lat) {
  49. this.option = {
  50. ...this.option,
  51. type: 'Icon',
  52. lng,
  53. lat,
  54. png: this.customIcon || this.option.png,
  55. type: 'update'
  56. }
  57. },
  58. async nextPoint(lnglat) {
  59. var that = this;
  60. let params = {
  61. postStr: JSON.stringify({
  62. lon: lnglat.lng,
  63. lat: lnglat.lat,
  64. ver: 1,
  65. }),
  66. type: 'geocode',
  67. tk: that.apiKey
  68. }
  69. let resData = await tools.createRequest('https://api.tianditu.gov.cn/geocoder', params, true)
  70. if (resData.status === '0') {
  71. const info = tools.formatterAdressLocation(resData.result, 1)
  72. this.option = {
  73. ...this.option,
  74. apikey: this.apiKey,
  75. lng: lnglat.lng,
  76. lat: lnglat.lat,
  77. png: this.customIcon || this.option.png,
  78. type: 'update'
  79. }
  80. this.$emit('onSelect', info)
  81. } else {
  82. tools.createMessage('数据异常', 1000, false, 'error')
  83. }
  84. },
  85. }
  86. }
  87. </script>
  88. <script module="Trenderjs" lang="renderjs">
  89. var Tmap = null;
  90. export default {
  91. data() {
  92. return {
  93. options: {},
  94. }
  95. },
  96. mounted() {
  97. // if (typeof window.T === 'object') {
  98. // console.warn('--------天地图已加载--------');
  99. // } else {
  100. // if (this.apiKey) {
  101. // const script = document.createElement('script')
  102. // script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + this.apiKey
  103. // script.onload = this.initChartsRender.bind(this)
  104. // document.head.appendChild(script)
  105. // }
  106. // }
  107. },
  108. methods: {
  109. createMap(apikey) {
  110. console.log("window.T = ", window.T)
  111. const script = document.createElement('script')
  112. script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + apikey
  113. script.onload = this.initChartsRender.bind(this)
  114. document.head.appendChild(script)
  115. },
  116. initTMap(newValue, oldValue, ownerInstance, instance) {
  117. console.log("v initTMap = ", newValue, oldValue, ownerInstance, instance)
  118. this.options = newValue
  119. if (newValue.type === 'open' && newValue.apikey) {
  120. if (!window.T) {
  121. const script = document.createElement('script')
  122. script.src = 'http://api.tianditu.gov.cn/api?v=4.0&tk=' + this.options.apikey
  123. script.onload = this.initChartsRender.bind(this)
  124. document.head.appendChild(script)
  125. const script_gd = document.createElement('script')
  126. script.src = "https://webapi.amap.com/maps?v=2.0&key=2ba8ac1dbcb55c167aee2e0bbbca8a96"
  127. document.head.appendChild(script_gd)
  128. } else {
  129. const {
  130. lng,
  131. lat
  132. } = this.options
  133. Tmap = null;
  134. Tmap = new T.Map('mapDiv', {
  135. projection: 'EPSG:4326',
  136. });
  137. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  138. // this.setIcon(lng, lat, true)
  139. this.$ownerInstance.callMethod('nextPoint', {
  140. lng,
  141. lat
  142. })
  143. Tmap.addEventListener('click', (e) => {
  144. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  145. });
  146. }
  147. } else {
  148. // 选点,更新
  149. // const {
  150. // lng,
  151. // lat
  152. // } = newValue
  153. // this.upDataChartsRender(lng, lat)
  154. }
  155. },
  156. initChartsRender() {
  157. this.$ownerInstance.callMethod('compliteonLoadTianDiTu')
  158. const {
  159. lng,
  160. lat
  161. } = this.options
  162. var that = this;
  163. Tmap = new T.Map('mapDiv', {
  164. projection: 'EPSG:4326',
  165. });
  166. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  167. this.setIcon(lng, lat, true)
  168. this.$ownerInstance.callMethod('nextPoint', {
  169. lng,
  170. lat
  171. })
  172. Tmap.addEventListener('click', (e) => {
  173. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  174. });
  175. },
  176. upDataChartsRender(lng, lat) {
  177. if (!Tmap) return
  178. this.setIcon(lng, lat, true)
  179. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  180. },
  181. setIcon(lng, lat, isClear, iconU) {
  182. if (isClear) {
  183. this.clearIcon()
  184. }
  185. const icon = new T.Icon({
  186. iconUrl: iconU || this.options.png,
  187. iconSize: new T.Point(30, 30),
  188. iconAnchor: new T.Point(15, 30)
  189. });
  190. const marker = new T.Marker(new T.LngLat(lng, lat), {
  191. icon
  192. });
  193. Tmap.addOverLay(marker);
  194. },
  195. clearIcon() {
  196. Tmap.clearOverLays()
  197. },
  198. getLocation() {
  199. var lo = new T.Geolocation();
  200. console.log('获取定位' , lo )
  201. const fn = (e) => {
  202. console.log('eeeeeeeeeeeeeeeeeeee' , e)
  203. // if (this.getStatus() == 0) {
  204. // map.centerAndZoom(e.lnglat, 15)
  205. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  206. // var marker = new T.Marker(e.lnglat);
  207. // map.addOverLay(marker);
  208. // }
  209. // if (this.getStatus() == 1) {
  210. // map.centerAndZoom(e.lnglat, e.level)
  211. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  212. // var marker = new T.Marker(e.lnglat);
  213. // map.addOverLay(marker);
  214. // }
  215. }
  216. lo.getCurrentPosition(fn);
  217. // if (this.getStatus() == 0) {
  218. // map.centerAndZoom(e.lnglat, 15)
  219. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  220. // var marker = new T.Marker(e.lnglat);
  221. // map.addOverLay(marker);
  222. // }
  223. // if (this.getStatus() == 1) {
  224. // map.centerAndZoom(e.lnglat, e.level)
  225. // alert("获取定位坐标:" + e.lnglat.lat + "," + e.lnglat.lng)
  226. // var marker = new T.Marker(e.lnglat);
  227. // map.addOverLay(marker);
  228. // }
  229. }
  230. },
  231. }
  232. </script>
  233. <style lang="scss">
  234. .mapDiv {
  235. width: 100%;
  236. height: 100%;
  237. }
  238. </style>