tiandituMap.vue 5.8 KB

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