当前位置:首页 > 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 实现截图的方法 使用 html2canvas 库 安装 html2canvas 库: npm install html2canvas 在 Vue 组件中使用: import html2ca…

vue实现fadein

vue实现fadein

Vue 实现 FadeIn 效果 在 Vue 中实现淡入(FadeIn)效果可以通过 CSS 过渡、动画或第三方库实现。以下是几种常见方法: 使用 CSS 过渡 通过 Vue 的过渡系统结合 CS…

vue拖拽实现

vue拖拽实现

Vue 拖拽实现方法 使用 HTML5 原生拖拽 API HTML5 提供了原生拖拽 API,可以通过 draggable 属性、dragstart、dragend、dragover 和 drop 事…

vue实现海报

vue实现海报

Vue 实现海报生成方案 使用 html2canvas 库 html2canvas 是一个将 HTML 元素转换为 Canvas 的库,适合将 Vue 组件渲染为海报图片 安装依赖: npm…

vue 实现注册

vue 实现注册

Vue 实现注册功能 注册功能通常包括表单验证、数据提交和用户反馈。以下是实现注册功能的步骤: 1. 创建注册表单组件 在 Vue 项目中创建一个注册表单组件,包含用户名、邮箱、密码等输入字段。…

vue实现频道

vue实现频道

Vue实现频道功能的方法 使用Vue Router动态路由 通过Vue Router的动态路由功能可以轻松实现频道切换。在路由配置中定义动态参数,组件内通过this.$route.params获取当前…