当前位置:   article > 正文

vue百度地图实现下拉匹配地址并给选中项添加标注、定位

vue百度地图实现下拉匹配地址并给选中项添加标注、定位

效果图

安装npm依赖

npm install vue-baidu-map

页面代码

新建页面test.vue

  1. <template>
  2. <baidu-map class="bm-view" scroll-wheel-zoom :center="location" :zoom="zoom" ak="xx" @ready="mapReady">
  3. <bm-view class="map"></bm-view>
  4. <bm-control :offset="{width: '50px', height: '10px'}">
  5. <el-input v-model="inputValue" id="suggestId"></el-input>
  6. </bm-control>
  7. <bm-navigation anchor="BMAP_ANCHOR_BOTTOM_RIGHT"></bm-navigation>
  8. </baidu-map>
  9. </template>
  10. <script>
  11. import BaiduMap from 'vue-baidu-map/components/map/Map.vue'
  12. import BmView from 'vue-baidu-map/components/map/MapView.vue'
  13. import BmLocalSearch from 'vue-baidu-map/components/search/LocalSearch.vue'
  14. import BmNavigation from 'vue-baidu-map/components/controls/Navigation'
  15. import BmMarkerClusterer from 'vue-baidu-map/components/extra/MarkerClusterer'
  16. import BmMarker from 'vue-baidu-map/components/overlays/Marker'
  17. import BmInfoWindow from 'vue-baidu-map/components/overlays/InfoWindow'
  18. import BmAutoComplete from 'vue-baidu-map/components/others/AutoComplete'
  19. import BmControl from 'vue-baidu-map/components/controls/Control'
  20. export default {
  21. components: {
  22. BaiduMap, BmView, BmLocalSearch, BmNavigation, BmMarkerClusterer, BmMarker, BmInfoWindow, BmAutoComplete, BmControl
  23. },
  24. data() {
  25. return {
  26. geocoder: null,
  27. model: {},
  28. BMap: {},
  29. map: {},
  30. location: {
  31. lng: 113.93588,
  32. lat: 22.74894
  33. },
  34. zoom: 16,
  35. inputValue: "",
  36. keyword: ''
  37. }
  38. },
  39. methods: {
  40. mapReady ({BMap, map}) {
  41. this.BMap = BMap;
  42. this.map = map;
  43. this.geocoder = new BMap.Geocoder(); //创建地址解析器的实例
  44. this.map.centerAndZoom(new BMap.Point(113.93588, 22.74894), 14);
  45. const that = this
  46. //建立一个自动完成的对象
  47. var ac = new BMap.Autocomplete({
  48. "input" : "suggestId",
  49. "location" : this.map
  50. });
  51. // 鼠标点击下拉列表
  52. ac.addEventListener("onconfirm", function(e) {
  53. var v = e.item.value;
  54. var keyword = v.province + v.city + v.district + v.street + v.business;
  55. //清除地图上所有覆盖物
  56. that.map.clearOverlays();
  57. //智能搜索
  58. var local = new BMap.LocalSearch(that.map, {
  59. onSearchComplete: function (){
  60. var p = local.getResults().getPoi(0).point; //获取第一个智能搜索的结果
  61. that.map.centerAndZoom(p, 16);
  62. that.map.addOverlay(new BMap.Marker(p)); //添加标注
  63. }
  64. });
  65. local.search(keyword);
  66. });
  67. // 点击百度地图上的搜索出来的红色标记点
  68. map.addEventListener("click", ({ point }) => {
  69. this.location.lng = point.lng;
  70. this.location.lat = point.lat;
  71. this.geocoder.getLocation(point, res => {
  72. this.inputValue = res.address;
  73. this.model.address = res.address;
  74. this.model.storeLongitude = point.lng
  75. this.model.storeLatitude = point.lat
  76. this.$forceUpdate();
  77. });
  78. });
  79. }
  80. }
  81. }
  82. </script>
  83. <style lang="scss">
  84. .map {
  85. width: 100%;
  86. height: 100%;
  87. flex: 1;
  88. }
  89. .address_wrap{
  90. width: 100%;
  91. height: 100%;
  92. ::v-deep{
  93. .bm-view {
  94. display: block;
  95. width: 100%;
  96. height: 100%;
  97. .BMap_noprint.anchorTL{
  98. width: 22%;
  99. top: 10px !important;
  100. left: 10px !important;
  101. }
  102. }
  103. }
  104. }
  105. .bm-view {
  106. display: block;
  107. width: 100%;
  108. height: 100%;
  109. .BMap_noprint.anchorTL{
  110. width: 22%;
  111. top: 10px !important;
  112. left: 10px !important;
  113. }
  114. }
  115. </style>

其中baidu-map组件中的ak参数需要在百度地图开放平台新建应用获得。

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

闽ICP备14008679号