Vue2使用百度地图展示或搜索地点(vue-baidu-map)

①安装插件:yarn add vue-baidu-map

②在main.js中全局注册引入:

import BaiduMap from 'vue-baidu-map'
Vue.use(BaiduMap, {
  ak: 'your_app_key' // 百度地图秘钥
})

④使用一:在地图上展示标注地点

查询并获取某个地点的坐标工具拾取坐标系统

<div class="m-map-container">
    <div class="m-map">
        <baidu-map
            class="bmView"
            :scroll-wheel-zoom="false" // 禁止鼠标滚轮缩放
			:dragging="false" // 禁止拖拽
			:double-click-zoom="false" // 禁止鼠标双击缩放
            :map-click="false" // 禁用鼠标点击查看详情
            :center="location"
            :zoom="zoom">
            <bm-marker :position="location" animation="BMAP_ANIMATION_BOUNCE"></bm-marker>
            <bm-view style="width: 100%; height: 480px; flex: 1;border-radius: 5px;"></bm-view>
        </baidu-map>
    </div>
</div>
data () {
    return {
        location: {
            lng: 102.68906,
            lat: 25.049784
        }, // 指定地点的经纬度
        zoom: 20 // 地图缩放比例
    }
}
.m-map-container {
  width: 1200px;
  text-align: center;
  margin: 0 auto;
  background: #fff;
  .m-map {
    margin: 0 auto;
    width: 800px;
    padding-top: 60px;
  }
}
// 移除左下角百度地图版权
/deep/ div.BMap_cpyCtrl.BMap_noprint.anchorBL {
  display: none;
}
// 移除左下角百度地图logo
/deep/ div.anchorBL {
  display: none;
}
// 悬浮地图时不展示小手
/deep/ .BMap_mask {
  cursor: default;
}

⑤使用二:在地图上搜索地点,并可通过鼠标点击获取其经纬度及详细地址信息

<template>
  <a-modal
    title="地址坐标"
    :width="980"
    :visible="visible"
    :confirmLoading="confirmLoading"
    @cancel="handleCancel"
    :footer="null"
  >
    <a-spin :spinning="confirmLoading">
      <a-form :form="form">
        <a-row :gutter="16">
          <a-col :lg="12" :md="12">
            <a-form-item>
              <a-input-search
                placeholder="请输入地址"
                enter-button="查询"
                @search="handleSearch"
                v-decorator="['address',{initialValue: defaultAddress}]"
              />
            </a-form-item>
          </a-col>
          <a-col :lg="12" :md="12">
            <a-form-item label="坐标" :labelCol="labelCol" :wrapper-col="wrapperCol">
              <a-input
                v-decorator="['address_coordinates']"/>
            </a-form-item>
          </a-col>
        </a-row>
        <div class="map">
          <!-- 地图点击事件getLocationPoint,获取地点详细信息、经纬度 -->
          <!-- scroll-wheel-zoom:是否可以用鼠标滚轮控制地图缩放,zoom:视图比例 -->
          <!-- dragging:是否可以用鼠标进行拖拽 -->
          <!-- double-click-zoom:是否可以用鼠标双击控制地图缩放 -->
          <!-- map-click:是否可以用鼠标点击查看详情 -->
          <baidu-map
            class="bmView"
            :scroll-wheel-zoom="true"
            :zoom="zoom"
            @click="getLocationPoint"
          >
            <!-- 右上角比例尺控件 -->
            <bm-scale anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-scale>
            <!-- 右上角缩放控件 -->
            <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>
            <!-- 左上角地图类型控件 -->
            <bm-map-type :map-types="['BMAP_NORMAL_MAP', 'BMAP_HYBRID_MAP']" anchor="BMAP_ANCHOR_TOP_LEFT"></bm-map-type>
            <!-- 右下角缩略图 -->
            <bm-overview-map anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :isOpen="true"></bm-overview-map>
            <!-- 右下角加入定位控件 -->
            <bm-geolocation anchor="BMAP_ANCHOR_BOTTOM_RIGHT" :showAddressBar="true" :autoLocation="true"></bm-geolocation>
            <!-- 左上角加入城市列表 -->
            <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
            <!-- 右上角加入全景控件 -->
            <!-- <bm-panorama anchor="BMAP_ANCHOR_BOTTOM_LEFT"></bm-panorama> -->
            <bm-view class="u-bm-view"></bm-view>
            <bm-local-search :keyword="addressKeyword" :auto-viewport="true" class="u-search"></bm-local-search>
          </baidu-map>
        </div>
        <span class="m-span">
          <a-form-item style="margin-top: 20px;margin-bottom: 10px;">
            <a-button class="u-submit" type="primary" htmlType="submit" @click="handleSubmit">确定</a-button>
            <a-button class="u-cancal" @click="handleCancel">取消</a-button>
          </a-form-item>
        </span>
      </a-form>
    </a-spin>
  </a-modal>
</template>
<script>
export default {
  data () {
    return {
      labelCol: {
        lg: { span: 4 },
        md: { span: 4 }
      },
      wrapperCol: {
        lg: { span: 20 },
        md: { span: 20 }
      },
      form: this.$form.createForm(this),
      addParams: {
        mod: 'add'
      },
      defaultAddress: '',
      visible: false,
      confirmLoading: false,
      zoom: 12,
      addressKeyword: ''
    }
  },
  methods: {
    getLocationPoint (e) {
      console.log('e:', e)
      console.log(e.point.lng + ',' + e.point.lat)
      /*
	    可以通过在.eslintrc.js中加入:
	    globals: {
	        BMap: true
	    }, // 防止eslint报错:'BMap' is not defined.
	    或者直接使用:// eslint-disable-next-line no-undef
      */
      const geoCoder = new BMap.Geocoder() // 创建地址解析器的实例
      /* 利用坐标获取地址的详细信息 */
      geoCoder.getLocation(e.point, res => {
        console.log('res:', res)
        this.form.setFieldsValue({
          // 地址和经纬度
          address: res.address,
          address_coordinates: res.point.lng + ',' + res.point.lat
        })
      })
    },
    handleSearch (value) {
      console.log(value)
      this.addressKeyword = value
    },
    check (value) {
      if (!this.defaultAddress) {
        this.defaultAddress = value
      } else {
        this.form.setFieldsValue({
          address: value
        })
      }
      this.addressKeyword = value
      this.visible = true
    },
    handleSubmit (e) {
      e.preventDefault()
      const { form: { validateFields } } = this
      this.confirmLoading = true
      validateFields((errors, values) => {
        if (!errors) {
          this.visible = false
          this.confirmLoading = false
          console.log('values:', values)
          this.$emit('ok', values)
        } else {
          this.confirmLoading = false
        }
      })
    },
    handleCancel () {
      this.visible = false
    }
  }
}
</script>
<style lang="less" scoped>
// 移除左下角百度地图版权
/deep/ div.BMap_cpyCtrl.BMap_noprint.anchorBL{
  display: none;
}
// 移除左下角百度地图logo
/deep/ div.anchorBL{
  display: none;
}
.u-bm-view{
   width: 100%;
   height: 500px;
   flex: 1;
}
.m-span{
  text-align: center;
}
.u-submit{
  margin-right: 30px;
  width: 100px;
}
.u-cancal{
  margin-left: 30px;
  width: 100px;
}
/deep/ .BMap_CityListCtrl{
  margin-top: 30px;
  div div {
    .city_content_top{
      height: 36px;
      .cur_city_info{
        position: absolute;
        bottom: 0px;
        height: 24px;
      }
      #city_ctrl_form{
        .sel_city_input{
          height: 24px;
        }
      }
    }
  }
}
.u-search{
  display:none;
}
</style>
 

 

好的,以下是完整的代码: ``` <template> <div> <button @click="toggleHeatmap">{{ buttonText }}</button> <div v-if="showHeatmap"> <div ref="map" style="width: 100%; height: 500px;"></div> <BmlHeatmap :data="heatmapData"></BmlHeatmap> </div> </div> </template> <script> import BMap from 'BMap'; import BmlHeatmap from 'vue-baidu-map/src/components/BmlHeatmap'; export default { components: { BmlHeatmap }, data() { return { showHeatmap: false, heatmapData: null, buttonText: '显示热力图', map: null } }, methods: { toggleHeatmap() { if (!this.showHeatmap) { // 如果heatmapData还没有获取到,则发起异步请求获取数据 axios.get('/api/heatmap-data').then(response => { this.heatmapData = response.data; this.showHeatmap = true; this.buttonText = '隐藏热力图'; this.initMap(); }); } else { // 如果heatmapData已经存在,则直接切换显示状态 this.showHeatmap = !this.showHeatmap; this.buttonText = this.showHeatmap ? '隐藏热力图' : '显示热力图'; } }, initMap() { this.map = new BMap.Map(this.$refs.map); // 添加地图控件 this.map.addControl(new BMap.NavigationControl()); this.map.addControl(new BMap.ScaleControl()); this.map.addControl(new BMap.OverviewMapControl()); // 设置地图中心点和缩放级别 this.map.centerAndZoom(new BMap.Point(116.404, 39.915), 11); } } } </script> ``` 这里我们首先引入了BMap和BmlHeatmap组件,然后在toggleHeatmap方法中初始化地图,并且在showHeatmap为true时渲染地图和热力图组件。 需要注意的是,在使用百度地图之前,您需要先在index.html中引入百度地图的JS API,例如: ``` <!DOCTYPE html> <html> <head> <meta charset="utf-8"> <title>Vue Baidu Map Demo</title> <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=YOUR_APP_KEY"></script> </head> <body> <div id="app"></div> <script src="/dist/build.js"></script> </body> </html> ``` 这里需要将YOUR_APP_KEY替换成您在百度地图开发者平台获取到的应用密钥。希望这个完整的代码能够对您有所帮助!
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值