vue-baidu-map使用

项目里用到一个地图初始定位,项目q前端框架是vue,所以选择使用vue-bai-map,记录一波

1.注册百度Ak

地址 https://lbsyun.baidu.com/apiconsole/key#/home,控制台=》运用管理=》我的运用=》创建运用生成一个AK

2.安装插件

npm install vue-biadu-map --save 在展示的页面写入,局部注册组件,需要的功能组件找到对应的模块下引入即可

<template>

    <div>

         <BaiduMap class="bm-view" ak="你的AK" :center="center" :zoom="zoom" @ready="handler"

         :scroll-wheel-zoom="true">

         <bm-navigation anchor="BMAP_ANCHOR_TOP_RIGHT"></bm-navigation>

         <bm-maker :position="center"></bm-maker>

         </BaiduMap>

    </div>

</template>

<script>

import BaiduMap from 'vue-baidu-map/components/map/Map'

import BmNavigation from 'vue-baidu-map/components/controls/Navigation'

import BmMaker from 'vue-baidu-map/components/overlays/Marker'

export default {

    components: {

        BaiduMap,

        BmNavigation,

        BmMaker

    },

    data() {

        return {

            center: {lng: 0, lat: 0},

            zoom: 3,

            }

    },

    methods: {

        handler ({BMap, map}) {

           const that = this;

      var geolocation = new BMap.Geolocation();

      geolocation.getCurrentPosition(function(r) {

        if (this.getStatus() == BMAP_STATUS_SUCCESS) {

          that.center.lng = r.longitude;

          that.center.lat = r.latitude;

          console.log(that.center);

        } else {

          alert("failed" + this.getStatus());

        }

      })

    }

}

}

</script>

<style lang="scss" scoped>

.bm-view {

  width: 100%;

  height: 300px;

}

</style>

 

  • 0
    点赞
  • 1
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值