index.vue 4.0 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159
  1. <template>
  2. <div class="map">
  3. <div id="allMap" />
  4. <div class="point">
  5. <span>您当前位置:</span>
  6. <span>{{ pointText }}</span>
  7. </div>
  8. </div>
  9. </template>
  10. <script>
  11. export default {
  12. props: {
  13. // 城市名称
  14. city: {
  15. type: String,
  16. default: ''
  17. },
  18. initPoint: {
  19. type: String,
  20. default: ''
  21. }
  22. },
  23. data() {
  24. return {
  25. map: null,
  26. latlng: null,
  27. marker: null
  28. }
  29. },
  30. computed: {
  31. pointText() {
  32. if (!this.latlng) {
  33. return '--,--'
  34. } else {
  35. return `${this.latlng.lat},${this.latlng.lng}`
  36. }
  37. }
  38. },
  39. watch: {
  40. latlng(nVal, oVal) {
  41. this.$emit('point', nVal)
  42. }
  43. },
  44. created() {
  45. this.$nextTick(() => {
  46. this.makeBiduMap()
  47. })
  48. },
  49. methods: {
  50. // 根据坐标初始化地图
  51. pointInit() {
  52. const pointArr = this.initPoint.split(',')
  53. const lat = parseFloat(pointArr[0])
  54. const lng = parseFloat(pointArr[1])
  55. // 初始化地图坐标
  56. const point = new window.BMapGL.Point(lng, lat)
  57. console.log(point)
  58. // 创建标记点对象模型
  59. this.marker = new window.BMapGL.Marker(point, { enableDragging: true })
  60. this.map.addOverlay(this.marker)
  61. this.map.centerAndZoom(point, 16)
  62. },
  63. // 默认初始化地图
  64. defInit() {
  65. // 初始化地图坐标 22.542802586057,114.10197962299
  66. const point = new window.BMapGL.Point(114.10237688976471, 22.542245357146264)
  67. // 创建标记点对象模型
  68. this.marker = new window.BMapGL.Marker(point, { enableDragging: true })
  69. this.map.addOverlay(this.marker)
  70. this.map.centerAndZoom(point, 16)
  71. // 如果有城市信息 就定位到城市
  72. if (this.city) return this.locationToCity()
  73. // 定位到ip
  74. this.locationToSelf()
  75. },
  76. // 对地图坐标的处理
  77. makeBiduMap() {
  78. // 创建地图模型
  79. this.map = new window.BMapGL.Map('allMap')
  80. // 初始化定位坐标
  81. if (this.initPoint) {
  82. this.pointInit()
  83. } else {
  84. this.defInit()
  85. }
  86. // 添加控件
  87. this.addControl()
  88. // 绑定事件
  89. this.addMapEvent()
  90. },
  91. // 添加控件
  92. addControl() {
  93. // 添加比例尺控件
  94. var scaleCtrl = new window.BMapGL.ScaleControl()
  95. this.map.addControl(scaleCtrl)
  96. // 添加缩放控件
  97. var zoomCtrl = new window.BMapGL.ZoomControl()
  98. this.map.addControl(zoomCtrl)
  99. // 允许鼠标滚轮缩放
  100. this.map.enableScrollWheelZoom(true)
  101. // 添加城市列表控件
  102. var cityCtrl = new window.BMapGL.CityListControl()
  103. this.map.addControl(cityCtrl)
  104. },
  105. // 定位到当前位置
  106. locationToSelf() {
  107. const that = this
  108. var geolocation = new window.BMapGL.Geolocation()
  109. geolocation.getCurrentPosition(function(r) {
  110. if (this.getStatus() === window.BMAP_STATUS_SUCCESS) {
  111. that.marker.setPosition(r.point)
  112. that.map.centerAndZoom(r.point, 16)
  113. that.latlng = r.point
  114. // console.log('您的位置:' + r.point.lng + ',' + r.point.lat)
  115. } else {
  116. console.log('failed' + this.getStatus())
  117. }
  118. })
  119. },
  120. // 地址解析(正)
  121. locationToCity() {
  122. var geo = new window.BMapGL.Geocoder()
  123. geo.getPoint(this.city, point => {
  124. if (point) {
  125. this.marker.setPosition(point)
  126. this.map.centerAndZoom(point, 16)
  127. this.latLng = point
  128. } else {
  129. console.log('您选择的地址没有解析到结果!')
  130. }
  131. }, this.city)
  132. },
  133. // 地图事件绑定
  134. addMapEvent() {
  135. this.marker.addEventListener('mouseup', e => {
  136. this.latlng = e.latLng
  137. // console.log('您的位置:' + e.latLng.lng + ',' + e.latLng.lat)
  138. })
  139. }
  140. }
  141. }
  142. </script>
  143. <style lang="scss" scoped>
  144. #allMap{
  145. width: 100%;
  146. height: 500px;
  147. }
  148. .point{
  149. margin-top: 16px;
  150. }
  151. </style>