index.vue 4.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160
  1. <template>
  2. <div class="a-map" id="aMap"></div>
  3. </template>
  4. <script>
  5. import { geolocation, initGeocoder } from './common/utils'
  6. export default {
  7. name: 'AMap',
  8. props: {
  9. zoom: {
  10. type: Number,
  11. default: 17
  12. },
  13. markerIcon: {
  14. type: String,
  15. default: '/location.png'
  16. },
  17. center: {
  18. type: Boolean,
  19. default: false
  20. },
  21. address: {
  22. type: String,
  23. default: ''
  24. },
  25. lnglat: {
  26. type: Array,
  27. default: null
  28. }
  29. },
  30. data () {
  31. return {
  32. marker: null,
  33. map: null,
  34. timer: null,
  35. geocoder: null
  36. }
  37. },
  38. methods: {
  39. async init () {
  40. if (this.lnglat) {
  41. console.log('经纬度定位')
  42. this.initMap(this.lnglat)
  43. } else if (this.address) {
  44. console.log('地址定位')
  45. this.initGeocoder()
  46. } else {
  47. console.log('高精度自动定位')
  48. this.geolocation()
  49. }
  50. },
  51. // 高精度定位
  52. async geolocation () {
  53. try {
  54. const result = await geolocation()
  55. this.$emit('position', {
  56. lng: result.position.lng,
  57. lat: result.position.lat
  58. })
  59. this.initMap(result.position)
  60. } catch (error) {
  61. this.initMap()
  62. alert('获取当前位置信息失败,已为您定位到当前城市!')
  63. console.log('获取当前位置信息失败,已为您定位到当前城市!')
  64. }
  65. },
  66. // 根据地址信息定位
  67. async initGeocoder () {
  68. try {
  69. this.geocoder = await initGeocoder()
  70. console.log(this.geocoder)
  71. this.geocoder.getLocation(this.address, (status, result) => {
  72. if (status === 'complete' && result.info === 'OK') {
  73. const position = result.geocodes[0].location
  74. this.initMap(position)
  75. this.$emit('position', {
  76. lng: position.lng,
  77. lat: position.lat
  78. })
  79. }
  80. })
  81. } catch (error) {
  82. console.log(error)
  83. }
  84. },
  85. // 初始化地图
  86. initMap (position) {
  87. this.map = new window.AMap.Map('aMap', {
  88. viewMode: '2D', // 默认使用 2D 模式,如果希望使用带有俯仰角的 3D 模式,请设置 viewMode: '3D',
  89. zoom: this.zoom, // 初始化地图层级
  90. center: position // 初始化地图中心点
  91. })
  92. this.map.on('click', this.onMapClick)
  93. if (position) {
  94. this.map.add(this.initMarker(position))
  95. }
  96. this.initMapControl()
  97. },
  98. // 初始化标记点
  99. initMarker (position) {
  100. this.marker = new window.AMap.Marker({
  101. icon: this.markerIcon,
  102. position,
  103. anchor: 'bottom-center',
  104. draggable: true
  105. })
  106. this.marker.on('dragging', this.moveMarker)
  107. return this.marker
  108. },
  109. // 修改标记点
  110. onMapClick (map) {
  111. if (this.marker) this.map.remove(this.marker)
  112. if (this.center) this.map.setCenter(map.lnglat)
  113. this.map.add(this.initMarker(map.lnglat))
  114. this.$emit('position', {
  115. lng: map.lnglat.lng,
  116. lat: map.lnglat.lat
  117. })
  118. },
  119. // 标记点移动
  120. moveMarker (marker) {
  121. if (this.timer) clearTimeout(this.timer)
  122. this.timer = setTimeout(() => {
  123. if (this.center) this.map.setCenter(marker.lnglat)
  124. this.$emit('position', {
  125. lng: marker.lnglat.lng,
  126. lat: marker.lnglat.lat
  127. })
  128. }, 200)
  129. },
  130. // 地图控件
  131. initMapControl () {
  132. window.AMap.plugin(
  133. ['AMap.ToolBar', 'AMap.Scale', 'AMap.MapType', 'AMap.Geolocation'],
  134. () => {
  135. // 在图面添加工具条控件,工具条控件集成了缩放、平移、定位等功能按钮在内的组合控件
  136. this.map.addControl(new window.AMap.ToolBar())
  137. // 在图面添加比例尺控件,展示地图在当前层级和纬度下的比例尺
  138. this.map.addControl(new window.AMap.Scale())
  139. // 在图面添加类别切换控件,实现默认图层与卫星图、实施交通图层之间切换的控制
  140. this.map.addControl(new window.AMap.MapType())
  141. // 在图面添加定位控件,用来获取和展示用户主机所在的经纬度位置
  142. // this.map.addControl(new window.AMap.Geolocation())
  143. }
  144. )
  145. }
  146. }
  147. }
  148. </script>
  149. <style lang="scss" scoped>
  150. #aMap {
  151. height: 100%;
  152. min-height: 400px;
  153. }
  154. </style>