tianditu-map.vue 6.6 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198
  1. <template>
  2. <view style="width: 100%;height: 100%;">
  3. <view id="mapDiv" class="mapDiv" :apikey="apiKey" :prop="option" :change:prop="Trenderjs.initTMap"></view>
  4. </view>
  5. </template>
  6. <script>
  7. import tools from '../../tools.js'
  8. import iconPath from '../../static/point.png'
  9. export default {
  10. name: 'TianDiTu-Map',
  11. data() {
  12. return {
  13. Tmap: null,
  14. option: {
  15. type: '',
  16. apikey: '',
  17. lng: '',
  18. lat: '',
  19. png: iconPath
  20. }
  21. }
  22. },
  23. props: {
  24. apiKey: {
  25. type: String,
  26. require: true,
  27. default: ''
  28. },
  29. customIcon: {
  30. type: String,
  31. default: ''
  32. }
  33. },
  34. methods: {
  35. compliteonLoadTianDiTu() {
  36. this.$emit('onLoadTianDiTu')
  37. },
  38. initCharts(lng, lat) {
  39. this.option = {
  40. apikey: this.apiKey,
  41. lng,
  42. lat,
  43. png: this.customIcon || this.option.png,
  44. type: 'open'
  45. }
  46. },
  47. upDataCharts(lng, lat) {
  48. this.option = {
  49. ...this.option,
  50. type: 'Icon',
  51. lng,
  52. lat,
  53. png: this.customIcon || this.option.png,
  54. type: 'update'
  55. }
  56. },
  57. async nextPoint(lnglat) {
  58. var that = this;
  59. let params = {
  60. postStr: JSON.stringify({
  61. lon: lnglat.lng,
  62. lat: lnglat.lat,
  63. ver: 1,
  64. }),
  65. type: 'geocode',
  66. tk: that.apiKey
  67. }
  68. let resData = await tools.createRequest('https://api.tianditu.gov.cn/geocoder', params, true)
  69. if (resData.status === '0') {
  70. const info = tools.formatterAdressLocation(resData.result, 1)
  71. this.option = {
  72. ...this.option,
  73. apikey: this.apiKey,
  74. lng: lnglat.lng,
  75. lat: lnglat.lat,
  76. png: this.customIcon || this.option.png,
  77. type: 'update'
  78. }
  79. this.$emit('onSelect', info)
  80. } else {
  81. tools.createMessage('数据异常', 1000, false, 'error')
  82. }
  83. },
  84. }
  85. }
  86. </script>
  87. <script module="Trenderjs" lang="renderjs">
  88. var Tmap = null;
  89. export default {
  90. data() {
  91. return {
  92. options: {},
  93. }
  94. },
  95. mounted() {
  96. // if (typeof window.T === 'object') {
  97. // console.warn('--------天地图已加载--------');
  98. // } else {
  99. // if (this.apiKey) {
  100. // const script = document.createElement('script')
  101. // script.src = 'https://api.tianditu.gov.cn/api?v=4.0&tk=' + this.apiKey
  102. // script.onload = this.initChartsRender.bind(this)
  103. // document.head.appendChild(script)
  104. // }
  105. // }
  106. },
  107. methods: {
  108. initTMap(newValue, oldValue, ownerInstance, instance) {
  109. this.options = newValue
  110. if (newValue.type === 'open' && newValue.apikey) {
  111. if (!window.T) {
  112. const script = document.createElement('script')
  113. script.src = 'https://api.tianditu.gov.cn/api?v=4.0&tk=' + this.options.apikey
  114. script.onload = this.initChartsRender.bind(this)
  115. document.head.appendChild(script)
  116. } else {
  117. const {
  118. lng,
  119. lat
  120. } = this.options
  121. Tmap = null;
  122. Tmap = new T.Map('mapDiv', {
  123. projection: 'EPSG:4326',
  124. });
  125. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  126. this.setIcon(lng, lat, true)
  127. this.$ownerInstance.callMethod('nextPoint', {
  128. lng,
  129. lat
  130. })
  131. Tmap.addEventListener('click', (e) => {
  132. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  133. });
  134. }
  135. } else {
  136. const {
  137. lng,
  138. lat
  139. } = newValue
  140. this.upDataChartsRender(lng, lat)
  141. }
  142. },
  143. initChartsRender() {
  144. this.$ownerInstance.callMethod('compliteonLoadTianDiTu')
  145. const {
  146. lng,
  147. lat
  148. } = this.options
  149. var that = this;
  150. Tmap = new T.Map('mapDiv', {
  151. projection: 'EPSG:4326',
  152. });
  153. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  154. this.setIcon(lng, lat, true)
  155. this.$ownerInstance.callMethod('nextPoint', {
  156. lng,
  157. lat
  158. })
  159. Tmap.addEventListener('click', (e) => {
  160. this.$ownerInstance.callMethod('nextPoint', e.lnglat)
  161. });
  162. },
  163. upDataChartsRender(lng, lat) {
  164. if (!Tmap) return
  165. this.setIcon(lng, lat, true)
  166. Tmap.centerAndZoom(new T.LngLat(lng, lat), 15);
  167. },
  168. setIcon(lng, lat, isClear) {
  169. if (isClear) {
  170. Tmap.clearOverLays()
  171. }
  172. const icon = new T.Icon({
  173. iconUrl: this.options.png,
  174. iconSize: new T.Point(30, 30),
  175. iconAnchor: new T.Point(15, 30)
  176. });
  177. const marker = new T.Marker(new T.LngLat(lng, lat), {
  178. icon
  179. });
  180. Tmap.addOverLay(marker);
  181. },
  182. },
  183. }
  184. </script>
  185. <style scoped>
  186. #mapDiv {
  187. width: 100%;
  188. height: 100%;
  189. }
  190. /deep/ .tdt-control-copyright {
  191. display: none;
  192. }
  193. </style>