当前位置:   article > 正文

vue-baidu-map 百度地图检索、获取坐标_vue baidu map 检索

vue baidu map 检索

最终效果

完整代码

子组件 -- 百度地图  src/projects/dic/VUE/Cases/getCoordinate.vue

  1. <template>
  2. <baidu-map @ready="loadedMap" @click="clickMap" style="height: 400px;width: 100%"
  3. :zoom="zoom" :center="mapCenter" :scroll-wheel-zoom="true">
  4. <!--拖拽标注点 @dragend="dragPointMarker" -->
  5. <bm-marker :position="pointMarker"
  6. @mouseover="overPointMarker"
  7. animation="BMAP_ANIMATION_BOUNCE">
  8. </bm-marker>
  9. <bm-info-window :closeOnClick="false" :position="pointMarker"
  10. @close="hideInfo" :show="showInfo"
  11. >
  12. <div style="min-width: 100px" class="info">
  13. <p v-if="pointInfo.name">地点名称:{{pointInfo.name}}</p>
  14. <p v-if="pointInfo.address">详细地址:{{pointInfo.address}}</p>
  15. <p v-if="pointInfo.location">
  16. <span style="margin-right: 10px">
  17. 经度:{{pointInfo.location.lat}}</span>
  18. <span>
  19. 纬度:{{pointInfo.location.lng}}
  20. </span>
  21. </p>
  22. </div>
  23. </bm-info-window>
  24. <bm-control
  25. anchor="BMAP_ANCHOR_TOP_RIGHT"
  26. :offset="{width: '10', height: '10'}"
  27. >
  28. <el-autocomplete
  29. prefix-icon="el-icon-search"
  30. clearable
  31. size="mini"
  32. v-model="place"
  33. :fetch-suggestions="searchPlace"
  34. placeholder="请输入关键字"
  35. @select="selectPlace"
  36. ></el-autocomplete>
  37. </bm-control>
  38. <bm-city-list @changeAfter="updateRegion" anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
  39. </baidu-map>
  40. </template>
  41. <script>
  42. export default {
  43. data() {
  44. return {
  45. // 地图
  46. map: '',
  47. // 地图中心点
  48. mapCenter: {},
  49. // 地图缩放
  50. zoom: 15,
  51. // 点
  52. point: {},
  53. // 点信息
  54. pointInfo: {},
  55. // 是否展示点信息
  56. showInfo: false,
  57. // 标注点的坐标信息
  58. pointMarker: {},
  59. // 地图坐标解析器
  60. gc: '',
  61. // 检索地址
  62. place: '',
  63. // 当前行政区划
  64. region: '北京',
  65. }
  66. },
  67. methods: {
  68. // 初始化地图
  69. loadedMap({BMap, map}) {
  70. //创建地址解析器实例
  71. this.gc = new BMap.Geocoder();
  72. this.map = map
  73. if (this.point.lat && this.point.lng) {
  74. this.mapCenter = this.point
  75. this.addMarker(this.point)
  76. this.pointInfo.location=this.point
  77. } else {
  78. this.mapCenter = "北京"
  79. }
  80. },
  81. // 点击地图--更新点坐标,获取点信息,添加标注
  82. clickMap(event) {
  83. this.updatePoint(event.point)
  84. this.getPointInfo(event.point)
  85. this.addMarker(event.point)
  86. },
  87. // 搜索地点
  88. searchPlace(queryString, cb) {
  89. this.$http.get("/baiduMapAPI/place/v2/search", {
  90. params: {
  91. query: queryString,
  92. region: this.region,
  93. city_limit: true,
  94. output: 'json',
  95. ak: this.GLOBAL.baiduMapAK
  96. }
  97. })
  98. .then(res => {
  99. let results = res.data.results.map(item => {
  100. return {
  101. value: item.name,
  102. ...item
  103. }
  104. });
  105. cb(results);
  106. })
  107. },
  108. // 选择地点 -- 更新点,添加标注,调整地图视图,更新点信息,展示信息窗
  109. selectPlace(pointInfo) {
  110. this.updatePoint(pointInfo.location)
  111. this.addMarker(pointInfo.location)
  112. this.moveMap(pointInfo.location)
  113. this.pointInfo = pointInfo
  114. this.showInfo = true
  115. },
  116. // 更新点坐标
  117. updatePoint(point) {
  118. this.point = point
  119. },
  120. // 获取点信息
  121. getPointInfo(point) {
  122. this.pointInfo.name = ''
  123. this.pointInfo.address = ''
  124. let that = this
  125. // 解析坐标点--获取坐标点所在的区域名称(城市)
  126. that.gc.getLocation(point, function (res) {
  127. let detailAddress = res.addressComponents
  128. // 省-- detailAddress.province
  129. that.region = detailAddress.city
  130. that.place = detailAddress.district + detailAddress.street + detailAddress.streetNumber
  131. that.pointInfo.location = point
  132. that.pointInfo.address = that.place
  133. that.showInfo = true
  134. })
  135. },
  136. // 移动地图
  137. moveMap(point) {
  138. this.mapCenter = point
  139. },
  140. // 更新区划
  141. updateRegion() {
  142. // 获取地图的中心点
  143. let point = this.map.getCenter()
  144. let that = this
  145. // 解析坐标点--获取坐标点所在的区域名称(城市)
  146. that.gc.getLocation(point, function (rs) {
  147. that.region = rs.addressComponents.city
  148. })
  149. },
  150. // 设置地图缩放级别
  151. setZoom(zoom) {
  152. this.zoom = zoom
  153. },
  154. // 添加标注——跳动的点
  155. addMarker(point) {
  156. this.pointMarker = point
  157. },
  158. // 点信息浮窗——鼠标悬浮在标注点上时,显示点信息
  159. overPointMarker() {
  160. this.showInfo = true
  161. },
  162. // 点信息浮窗——点击点信息浮窗上的关闭按钮,关闭点信息浮窗
  163. hideInfo() {
  164. this.showInfo = false
  165. },
  166. }
  167. }
  168. </script>
  169. <style scoped>
  170. .info {
  171. color: grey;
  172. font-size: 12px;
  173. }
  174. </style>

父组件  src/page/test.vue

  1. <template>
  2. <div style="padding: 30px">
  3. <el-form :model="formData" size="mini">
  4. <el-form-item label="地图坐标:">
  5. <div style="display: flex">
  6. <el-input readonly placeholder="经度" style="width: 120px" v-model="formData.jd"></el-input>
  7. <el-input readonly placeholder="纬度" style="width: 120px" v-model="formData.wd"></el-input>
  8. <el-button type="text" @click="showMap" style="margin-left: 10px">获取</el-button>
  9. </div>
  10. </el-form-item>
  11. </el-form>
  12. <el-dialog title="获取地图坐标" :visible.sync="mapDialog" v-if="mapDialog" width="60%" append-to-body>
  13. <GetCoordinate ref="map"></GetCoordinate>
  14. <span slot="footer">
  15. <el-button @click="mapDialog = false" size="mini">取 消</el-button>
  16. <el-button @click="getCoordinate" size="mini" type="primary">确 定</el-button>
  17. </span>
  18. </el-dialog>
  19. </div>
  20. </template>
  21. <script>
  22. import GetCoordinate from '@/projects/dic/VUE/Cases/getCoordinate.vue'
  23. export default {
  24. components: {GetCoordinate},
  25. data() {
  26. return {
  27. formData: {},
  28. mapDialog: false,
  29. }
  30. },
  31. methods: {
  32. showMap() {
  33. this.mapDialog = true
  34. this.$nextTick(
  35. () => {
  36. if (this.formData.jd && this.formData.wd) {
  37. this.$refs.map.updatePoint(
  38. {
  39. lat: this.formData.jd,
  40. lng: this.formData.wd
  41. }
  42. )
  43. }
  44. }
  45. )
  46. },
  47. getCoordinate() {
  48. if (this.$refs.map.pointInfo.location) {
  49. this.formData = {
  50. jd: this.$refs.map.pointInfo.location.lat,
  51. wd: this.$refs.map.pointInfo.location.lng,
  52. }
  53. this.mapDialog = false
  54. } else {
  55. this.$myMessage("请点击地图选择一个坐标点!")
  56. }
  57. }
  58. }
  59. }
  60. </script>
  61. <style scoped>
  62. </style>

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/羊村懒王/article/detail/325764
推荐阅读