前端VUE使用百度地图 、vue-baidu-map

前端VUE使用百度地图

捣鼓了好几天的地图,小白入手,记录一下下!具体的可以查看https://dafrok.github.io/vue-baidu-map

一、安装依赖

npm install vue-baidu-map --save

二、引入

在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'
})

在index.html文件中添加

<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=你的ak"></script>

在这里插入图片描述

三、简单的使用


<template>
  <div class="map">
    <baidu-map
      class="mapId"
      :center="center"
      :zoom="zoom"
      :scroll-wheel-zoom="true"
      @moving="syncCenterAndZoom"
      @moveend="syncCenterAndZoom"
      @zoomend="syncCenterAndZoom"
      @ready="readyMap"
    >
 <!--      //页面的打点显示-->   
      <bm-point-collection
        :points="points"
        shape="BMAP_POINT_SHAPE_STAR"
        color="red"
        size="BMAP_POINT_SIZE_SMALL"
        @click="clickHandler">
      </bm-point-collection>
      
<!--      //地理位置的搜索功能-->
<!--      <bm-local-search-->
<!--        :keyword="keyword"-->
<!--        :auto-viewport="true"-->
<!--        :location="location"-->
<!--      ></bm-local-search>-->

<!--      //缩放比例尺的展示-->
<!--      <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>-->

<!--      //定位-->
<!--      <bm-geolocation-->
<!--        anchor="BMAP_ANCHOR_BOTTOM_RIGHT"-->
<!--        :showAddressBar="true"-->
<!--        :autoLocation="true"-->
<!--      ></bm-geolocation>-->

<!--      //添加一个小红点的,并将当前的经纬度写入数据中-->
<!--      <bm-marker :position="{lng:center.lng, lat: center.lat}"></bm-marker>-->
    </baidu-map>
  </div>
</template>
<script>
  export default {
    data () {
      return {
        center: {
          lng: 116.404,
          lat: 39.915
        },//初始的中心位置
        zoom: 4,//缩放级别
        points: [],//打点的地方
        //百度地图初始化数据
        baidumapSwitch:false,
        location: "",
        keyword: ""
      }
    },
    mounted(){
    this.addPoints ()
    },
    methods: {
      //地图实例:初始化加载地图
      readyMap({ BMap, map }) {
        // console.log(BMap, "BMap");
        // console.log(map, "map");
      },
      clickHandler(e){
        console.log(e.point.lng);
        console.log(e.point.lat);
        this.center.lng = e.point.lng;
        this.center.lat = e.point.lat;
      },
      syncCenterAndZoom (e) {
        const {lng, lat} = e.target.getCenter()
        this.center.lng = lng
        this.center.lat = lat
        this.zoom = e.target.getZoom()
        console.log(this.zoom);
      }
 },
      //添加海量点
      addPoints () {
        const points = [];
        for (var i = 0; i < n; i++) {
          const position = {lng: Math.random() * 40 + 85, lat: Math.random() * 30 + 21}
          points.push(position)
        }
         this.points = points
      }
    }
  }
</script>
<style lang="scss" scoped>
  .map {
    background-color: #32d0ab;
   .mapId{
     height:600px;
     width:100%;
   }
  }
</style>
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Smile_zxx

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值