vue百度地图获取当前定位并显示mark,坐标地址解析

本文介绍如何在Vue项目中集成百度地图API,实现实时获取用户定位,并在地图上显示标记。同时,讲解了坐标地址的解析过程。
摘要由CSDN通过智能技术生成

1.先安装好vue百度地图

这是一个组件
<template>
  <div>
    <baidu-map :center="center" :scroll-wheel-zoom="true" :zoom="zoom" :style="{ height: height, width: width }" @ready="initMap" @click="handler">
      <bm-marker :position="point" :dragging="true">
        <bm-label :label-style="{ color: 'red', fontSize: '14px' }" :offset="{ width: 20, height: -15 }" content="位置" />
      </bm-marker>
    </baidu-map>
  </div>
</template>
<script>
export default {
  name: 'BaiduMapPoint',
  props: {
    placeholder: {
      type: String,
      default: ''
    },
    // eslint-disable-next-line vue/require-default-prop
    zoom: {
      type: Number,
      default: 18
    },
    width: {
      type: String,
      default: '100%'
    },
    height: {
      
  • 2
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
首先需要在Vue项目中引入百度地图JavaScript API。 1. 在`index.html`文件中添加百度地图JavaScript API的引用: ```html <!-- 引入百度地图 JavaScript API --> <script type="text/javascript" src="https://api.map.baidu.com/api?v=3.0&ak=您的AK"></script> ``` 其中,`AK`是你在百度地图开发者中心中申请的密钥。 2. 在Vue组件中添加地图容器和定位按钮: ```html <template> <div> <!-- 地图容器 --> <div id="map"></div> <!-- 定位按钮 --> <button @click="getLocation">定位</button> </div> </template> ``` 3. 在Vue组件的`<script>`标签中编写JavaScript代码: ```js export default { data() { return { // 地图对象 map: null, // 定位标记 marker: null, // 定位信息 location: null }; }, methods: { // 初始化地图 initMap() { const map = new BMap.Map('map'); map.centerAndZoom(new BMap.Point(116.404, 39.915), 18); this.map = map; }, // 获取当前位置信息 getLocation() { const geolocation = new BMap.Geolocation(); geolocation.getCurrentPosition( position => { this.location = position; this.showLocation(); }, { enableHighAccuracy: true } ); }, // 在地图显示位置信息 showLocation() { const { point, address } = this.location; if (!this.marker) { const marker = new BMap.Marker(point); this.map.addOverlay(marker); this.marker = marker; } else { this.marker.setPosition(point); } this.map.panTo(point); alert(`当前位置:${address}`); } }, mounted() { this.initMap(); } }; ``` 在上述代码中,我们首先定义了`map`、`marker`和`location`三个变量,分别表示地图对象、定位标记和位置信息。在`initMap`方法中,我们创建了一个地图对象并将其初始化。在`getLocation`方法中,我们通过`BMap.Geolocation`获取当前位置信息,并将其保存在`location`变量中。然后我们调用`showLocation`方法,在地图显示定位标记,并弹窗显示当前位置信息。
评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值