当前位置:首页 > 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 router 实现

vue router 实现

Vue Router 的实现 Vue Router 是 Vue.js 的官方路由管理器,用于构建单页面应用(SPA)。以下是 Vue Router 的核心实现方式。 安装 Vue Router 通过…

vue能实现

vue能实现

Vue.js 是一个流行的前端框架,能够实现多种功能。以下是 Vue 能够实现的主要功能和特性: 响应式数据绑定 Vue 的核心特性之一是响应式数据绑定。通过数据驱动视图,当数据发生变化时,视图会自…

vue 实现报价

vue 实现报价

Vue 实现报价功能 在 Vue 中实现报价功能通常涉及前端表单交互、数据计算和与后端 API 的通信。以下是实现报价功能的几种常见方法: 表单设计与数据绑定 使用 Vue 的 v-model 指…

vue 实现$(id)

vue 实现$(id)

在 Vue 中实现类似 jQuery 的 $(id) 功能,可以通过以下几种方式操作 DOM 元素: 使用 ref 属性 Vue 推荐使用 ref 属性来直接访问 DOM 元素或组件实例。在模板中为…

vue实现书库

vue实现书库

Vue 实现书库功能 使用 Vue 构建书库功能需要结合前端框架和后端数据管理。以下是实现步骤和关键点: 项目初始化 使用 Vue CLI 或 Vite 初始化项目,安装 Vue Router 和状…

vue实现openoffice

vue实现openoffice

Vue 中集成 OpenOffice 的实现方法 在 Vue 项目中集成 OpenOffice 通常需要通过后端服务或现有库实现文档的预览和编辑功能。以下是几种常见的实现方式: 使用 OnlyOff…