当前位置:首页 > VUE

vue引入地图实现定位

2026-01-22 08:18:07VUE

使用高德地图实现定位

安装高德地图的 Vue 插件

npm install @amap/amap-jsapi-loader --save

在 Vue 组件中引入并初始化地图

import AMapLoader from '@amap/amap-jsapi-loader';

export default {
  data() {
    return {
      map: null,
      position: null
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: '您的高德地图Key',  // 申请的高德地图开发者Key
        version: '2.0',
        plugins: ['AMap.Geolocation']
      }).then((AMap) => {
        this.map = new AMap.Map('map-container', {
          viewMode: '2D',
          zoom: 15
        });

        const geolocation = new AMap.Geolocation({
          enableHighAccuracy: true,
          timeout: 10000,
          buttonPosition: 'RB',
          showMarker: true,
          showCircle: true
        });

        this.map.addControl(geolocation);
        geolocation.getCurrentPosition();

        AMap.event.addListener(geolocation, 'complete', (data) => {
          this.position = data.position;
        });

        AMap.event.addListener(geolocation, 'error', (error) => {
          console.error('定位失败', error);
        });
      }).catch(e => {
        console.error('地图加载失败', e);
      });
    }
  }
}

在模板中添加地图容器

<template>
  <div id="map-container" style="width: 100%; height: 400px;"></div>
  <div v-if="position">
    当前位置: 经度 {{ position.lng }}, 纬度 {{ position.lat }}
  </div>
</template>

使用百度地图实现定位

安装百度地图 JavaScript API

npm install vue-baidu-map --save

在 main.js 中全局引入

vue引入地图实现定位

import BaiduMap from 'vue-baidu-map'

Vue.use(BaiduMap, {
  ak: '您的百度地图AK'  // 申请的百度地图开发者AK
})

在组件中使用定位功能

<template>
  <baidu-map class="map" :center="center" :zoom="zoom" @ready="handler">
    <bm-geolocation
      anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
      :showAddressBar="true"
      :autoLocation="true"
      @locationSuccess="locationSuccess"
      @locationError="locationError"
    ></bm-geolocation>
  </baidu-map>
</template>

<script>
export default {
  data() {
    return {
      center: {lng: 116.404, lat: 39.915},
      zoom: 15,
      currentPosition: null
    }
  },
  methods: {
    handler({BMap, map}) {
      this.BMap = BMap;
      this.map = map;
    },
    locationSuccess(point) {
      this.currentPosition = point.point;
      this.center.lng = point.point.lng;
      this.center.lat = point.point.lat;
    },
    locationError(error) {
      console.error('定位失败', error);
    }
  }
}
</script>

<style>
.map {
  width: 100%;
  height: 400px;
}
</style>

使用腾讯地图实现定位

安装腾讯地图 JavaScript API

npm install qqmap --save

在组件中使用定位功能

vue引入地图实现定位

import qqmap from 'qqmap';

export default {
  data() {
    return {
      center: {
        lat: 39.908823,
        lng: 116.397470
      },
      position: null
    };
  },
  mounted() {
    this.initMap();
  },
  methods: {
    initMap() {
      qqmap({
        key: '您的腾讯地图Key'
      }).then(qq => {
        const map = new qq.maps.Map(document.getElementById('map-container'), {
          center: new qq.maps.LatLng(this.center.lat, this.center.lng),
          zoom: 13
        });

        const geolocation = new qq.maps.Geolocation();
        geolocation.getLocation(position => {
          this.position = position;
          map.setCenter(new qq.maps.LatLng(position.lat, position.lng));
        }, error => {
          console.error('定位失败', error);
        }, {
          timeout: 10000
        });
      });
    }
  }
}

模板部分

<template>
  <div id="map-container" style="width: 100%; height: 400px;"></div>
  <div v-if="position">
    当前位置: 经度 {{ position.lng }}, 纬度 {{ position.lat }}
  </div>
</template>

注意事项

申请地图开发者Key时,需要根据所选地图平台(高德/百度/腾讯)到对应开发者平台注册账号

定位功能需要浏览器授权地理位置权限,在HTTPS环境下更可靠

移动端使用时,建议添加手势控制参数,提升用户体验

不同地图API的坐标系可能不同,如需转换可使用相应API提供的坐标转换方法

标签: 地图vue
分享给朋友:

相关文章

vue手动实现弹窗

vue手动实现弹窗

实现弹窗组件的基本结构 在Vue中手动实现弹窗组件,需要创建一个独立的弹窗组件,并通过props控制其显示与隐藏。弹窗组件通常包含遮罩层、内容区域和关闭按钮。 <template>…

vue查看更多怎么实现

vue查看更多怎么实现

实现 Vue 的“查看更多”功能 数据截取与显示控制 通过 v-if 或 v-show 控制内容的显示与隐藏。初始状态下只显示部分内容,点击“查看更多”后展开完整内容。 <template&g…

vue实现防抖节流

vue实现防抖节流

Vue 中实现防抖(Debounce) 防抖的核心是延迟执行函数,在连续触发时取消前一次的调用,只执行最后一次。适用于输入框搜索、窗口大小调整等场景。 方法1:使用 Lodash 的 _.debou…

vue实现下拉层级

vue实现下拉层级

Vue实现下拉层级菜单 使用Vue实现下拉层级菜单可以通过递归组件或动态渲染的方式完成。以下是两种常见实现方法: 递归组件实现 创建一个递归组件处理多级菜单结构: <template…

vue实现双折线图

vue实现双折线图

实现双折线图的步骤 安装必要的依赖库(如 ECharts 或 Chart.js),这里以 ECharts 为例: npm install echarts --save 在 Vue 组件中引入 ECh…

vue实现一个标签选择

vue实现一个标签选择

Vue 标签选择组件实现 基础实现 使用 Vue 的 v-for 和 v-model 指令可以快速实现标签选择功能: <template> <div class="tag-se…