百度地图在vue中的使用

  1. 可以使用vue-baidu-map这个插件,专门针对vue的百度地图插件
    官网地址
    百度地图实例demo
  2. 在vue项目中使用
    在main.js 中
import Vue from 'vue'
import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
 // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
 ak: 'YOUR_APP_KEY'
})

在vue文件中

<template>
    <baidu-map
      :min-zoom="11"
      @ready="handler"
      @zoomend="handleZoom"
      class="map"
      :center="'北京'"
      :zoom="zoom"
      scroll-wheel-zoom
    >
    </baidu-map>
</template>
  1. 在style中给map设置高度,否则地图不会显示
<style lang="less" scoped>
.map {
  width: 100%;
  height: 300px;
}
</style>

说明

baidu-map的常用属性
  • min-zoom:最小层级
  • center:地图的中心定位 可以是文字 也可以是对象,如{lng: 116.404, lat: 39.915}
  • zoom: 缩放的等级
  • scroll-wheel-zoom:是否允许鼠标滚轮缩放 默认是false
事件
  • ready:地图加载开始执行的函数 他有两个参数一个是BMap一个是map 如果想在组件中使用他们,可以通过赋值
handler ({ BMap, map }) {
  this.map = map
  this.BMap = BMap
}
  • zoomend:地图更改缩放级别结束时触发触发此事件
handleZoom (v) {
  this.zoom = v.target.getZoom() // 可以通过这个方法获取到当前地图的层级
}
百度地图常用的用过的方法记录
  • 设置地图的中心点以及缩放层级 第一个参数可以是文字也是是坐标
this.map.centerAndZoom('北京', 11) 
  • 仅仅只设置地图的中心点 要注意地图的中心点不能仅仅是一个有lng和lat的对象要通过BMap.Point 生成的坐标才有效,以上centerAndZoom方法也是这样
this.map.setCenter(
  new this.BMap.Point(111, 222)
)

其余可查看地图文档

vue-baidu-map常用的组件记录:

海量点组件

<!-- 海量图 所有的定位 -->
<bm-point-collection
    @click="handlePointCollection"
    :points="allMarkers"
    shape="BMAP_POINT_SHAPE_STAR"
    size="BMAP_POINT_SIZE_HUGE"
    color="red"
/>

label组件

<bm-label
    :label-style="labelStyle"
    @click="clickLabel(marker)"
    :content="marker | filtersContent(zoom)"
    :position="{ lng: marker.longitude, lat: marker.latitude }"
    :offset="{ width: -40, height: 8 }"
/>
data () {
  return {
    labelStyle: {
       opacity: 0.9,
       border: 'none',
       color: 'white',
       'background-color': 'transparent',
       'font-size': '16px',
       'text-align': 'center'
     }
  }
}

其余组件看vue-baidu-map 官网
做过的项目例子:
效果: 模仿58实现点击label放大层级 显示大层级下的小层级效果 https://ditu.58.com/bj/sydc?catename=shangpu&zstype=2&PGTID=0d306b36-0000-1409-889e-2e4815472f32&ClickID=11
全部代码:

<template>
  <div>
    <!-- 百度地图 -->
    <baidu-map
      :v-loading="loading"
      :min-zoom="11"
      @ready="handler"
      @zoomend="handleZoom"
      class="map"
      :center="'北京'"
      :zoom="zoom"
      scroll-wheel-zoom
    >
      <!-- 大区域 and 小区域 -->
      <div v-show="zoom >= 11 && zoom <= 16">
        <div v-for="marker in markers" :key="marker.longitude + Math.random()">
          <bm-label
            :label-style="labelStyle"
            @click="clickLabel(marker)"
            :content="marker | filtersContent(zoom)"
            :position="{ lng: marker.longitude, lat: marker.latitude }"
            :offset="{ width: -40, height: 8 }"
          />
        </div>
      </div>

      <!-- 海量图 所有的定位 -->
      <bm-point-collection
        v-if="zoom >= 17"
        @click="handlePointCollection"
        :points="allMarkers"
        shape="BMAP_POINT_SHAPE_STAR"
        size="BMAP_POINT_SIZE_HUGE"
        color="red"
      />
    </baidu-map>
  </div>
</template>

<script>

export default {
  data () {
    return {
      labelStyle: {
        opacity: 0.9,
        border: 'none',
        color: 'white',
        'background-color': 'transparent',
        'font-size': '16px',
        'text-align': 'center'
      },
      active: false,
      markers: [],
      bigMarkers: [], // 大区域的数据
      smallMarkers: [], // 小区域的数据
      allMarkers: [], // 所有店铺区域的精细数据
      zoom: 14,
      map: {},
      BMap: {}
    }
  },
  
  filters: {
    filtersContent (marker, zoom) {
      let str = ''
      if (zoom >= 11 && zoom <= 13) {
        str = `<div class="BMapLabel_big"><div style="margin: 0 0 10px">${marker.area_name}</div>${marker.total}套</div>`
      } else if (zoom >= 14 && zoom <= 16) {
        str = `<div class="BMapLabel_small">${marker.area_name}</div>`
      }
      return str
    }
  },
  
  watch: {
    zoom: {
      handler (value) {
        if (value >= 11 && value <= 13) {
          this.markers = this.bigMarkers
        } else if (value >= 14 && value <= 16) {
          this.markers = this.smallMarkers
        }
      },
      immediate: true
    }
  },

  methods: {
   
    /**
     * 关于地图的所有方法
     */
    handler ({ BMap, map }) {
      this.map = map
      this.BMap = BMap
      const city = this.$options.filters.city_name(this.current_store.city_id)
      map.centerAndZoom(city, 11) // 根据城市名设置中心位置
      this.getPointCollectionData(11) // 大区域
      this.getPointCollectionData(14) // 小区域
      this.getPointCollectionData(17) // 所有
    },
    handleZoom (v) {
      this.zoom = v.target.getZoom()
    },
    async getPointCollectionData (value) {
      const params = {
        big_class_value: this.params.big_class_value,
        small_class_value: this.params.small_class_value,
        acreage_value: this.params.acreage_value,
        city_id: this.current_store.city_id,
        stage_status: 3,
        mapBigArea: value === 11 ? 1 : undefined, // 大区域统计
        mapSmallArea: value === 14 ? 1 : undefined, // 小区域统计
        mapAll: value === 17 ? 1 : undefined, // 所有的地图定位
        big_area_value: value === 17 ? undefined : this.params.big_area_value,
        small_area_value: value === 17 ? undefined : this.params.small_area_value
      }
      await getTransferStore(params).then(res => {
        if (value === 11) {
          this.markers = this.bigMarkers = res.data.total
          if (this.markers.length === 1 && !this.params.small_area_value) {
            this.map.setCenter(
              new this.BMap.Point(this.markers[0].longitude, this.markers[0].latitude)
            )
          }
        } else if (value === 14) {
          this.smallMarkers = res.data.total
          if (this.params.small_area_value) {
            const arr = this.bigMarkers.filter(item => item.id === this.params.big_area_value)
            if (!arr[0]) return
            this.clickLabel(arr[0])
          }
        } else if (value === 17) {
          this.allMarkers = []
          res.data.datas && res.data.datas.map(item => {
            this.allMarkers.push({
              lng: item.longitude,
              lat: item.latitude,
              area_name: item.area_name,
              id: item.id
            })
          })
        }
      })
    },
    // 点击label放大层级 并根据父级区域设置中心点
    clickLabel (marker) {
      if (this.zoom >= 11 && this.zoom <= 13) {
        this.zoom = 14
        this.formValidate.store_address.big_area_value = marker.id
        this.params.big_area_value = marker.id
        this.page = 1
        this.show()
      } else if (this.zoom >= 14 && this.zoom <= 16) {
        this.zoom = 17
        this.formValidate.store_address.small_area_value = marker.id
        this.params.small_area_value = marker.id
        this.page = 1
        this.show()
      }
      if (this.zoom >= 14 && this.zoom <= 16) {
        const arr = this.smallMarkers.filter(item => item.p_area_name === marker.area_name)
        if (arr.length === 0) return
        this.map.setCenter(new this.BMap.Point(arr[0].longitude, arr[0].latitude))
      } else if (this.zoom >= 17) {
        const arr = this.allMarkers.filter(item => item.area_name === marker.area_name)
        if (arr.length === 0) return
        this.map.setCenter(new this.BMap.Point(arr[0].lng, arr[0].lat))
      }
    },
    async handlePointCollection ({ currentTarget, point }) {
      const arr = this.allMarkers.filter(item => Number(item.lat) === point.lat && Number(item.lng) === point.lng)
      if (arr.length === 0) return
      const res = await getTransferStore({ id: arr[0].id, sign_detail: 1 })
      this.dataList = res.data.datas.data
      this.total = res.data.datas.total
    }
  }
}
</script>

<style lang="less" scoped>
.map {
  width: 100%;
  height: 300px;
}
</style>
  • 0
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
Vue 使用 echarts 百度地图需要先引入 echarts 和百度地图的 JavaScript API。 1. 引入 echarts 在 Vue 可以通过 npm 安装 echarts,然后在需要使用的组件引入: ``` npm install echarts --save import echarts from 'echarts' ``` 2. 引入百度地图 JavaScript API 在 Vue 可以在 index.html 引入百度地图 JavaScript API: ``` <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=百度地图密钥"></script> ``` 注意:需要替换 ak 参数为你的百度地图密钥。 3. 在组件使用 echarts 百度地图Vue 可以通过在组件的 mounted 钩子函数初始化 echarts 实例和百度地图实例,并将百度地图实例作为参数传递给 echarts 的 setOption 方法。 ``` <template> <div id="chart"></div> </template> <script> import echarts from 'echarts' export default { mounted() { // 初始化 echarts 实例 const chart = echarts.init(document.getElementById('chart')) // 初始化百度地图实例 const bmap = new BMap.Map("chart"); bmap.centerAndZoom(new BMap.Point(116.404, 39.915), 11); bmap.addControl(new BMap.NavigationControl()); // echarts 百度地图 option 配置 const option = { bmap: { center: [116.404, 39.915], zoom: 11, roam: true, }, series: [{ type: 'scatter', coordinateSystem: 'bmap', data: [[116.404, 39.915]], symbolSize: 20, }] } // 将百度地图实例传递给 echarts 的 setOption 方法 chart.setOption(option, true, { bmap: bmap }); } } </script> ``` 上面的代码使用了一个 scatter 类型的系列,它的 coordinateSystem 设置为 bmap,这样就可以在百度地图上绘制散点图了。 注意:需要在组件销毁时调用 echarts 的 dispose 方法销毁实例,以避免内存泄漏。 ``` export default { mounted() { const chart = echarts.init(document.getElementById('chart')) // ... }, beforeDestroy() { echarts.dispose(document.getElementById('chart')) } } ```

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值