vue 百度地图 + 定位

本文介绍如何在Vue项目中集成百度地图API并实现基本的地图显示及用户位置定位功能。主要内容包括:在项目中引入百度地图JS库、配置Webpack加载地图资源、编写组件以展示地图并获取当前位置坐标。

摘要生成于 C知道 ,由 DeepSeek-R1 满血版支持, 前往体验 >

vue 百度地图 + 定位

> 前提需要自己有百度的密钥,如没有可以去百度地图申请

一、在主目录下的index.html引入js,例如:

这里写图片描述
这里写图片描述

二、在webpack.base.conf.js配置文件中配置BMap,在module.exports 中与entry平级,例如:

这里写图片描述

三、在项目中引入BMap:

这里写图片描述

四、代码:

<template>
  <div class="home">
    <div id="allmap" class="allmap"></div>
  </div>
</template>

<script>
  import {getStore, setStore, removeStore} from '@/config/Utils'
  import BMap from 'BMap'
  export default {
    data () {
      return {
        type: 'tab',
        address_detail: null,
        center: {lng: 116.40387397, lat: 39.91488908}
      }
    },
    mounted () {
      this.ready()
    },
    methods: {
      ready () {
        let map = new BMap.Map('allmap')
        let point = new BMap.Point(this.center.lng, this.center.lat)
        map.centerAndZoom(point, 10)
        map.enableScrollWheelZoom(true)
        map.enableDoubleClickZoom(true)
        var geolocation = new BMap.Geolocation()
        geolocation.getCurrentPosition((r) => {
          if (r.point) {
            this.center.lng = r.longitude
            this.center.lat = r.latitude
            let markers = new BMap.Marker(r.point)
            map.addOverlay(markers)
            map.panTo(r.point)
            map.centerAndZoom(r.point, 16)
          }
        }, { enableHighAccuracy: true })
      }
    }
  }
</script>

<style>
  #allmap{
    width: 100%;
    height: 15rem;
  }
</style>

这里写图片描述

五、效果:

这里写图片描述

如有错误地方,请留言指教,谢谢大家

评论 9
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值