当前位置:首页 > 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 的 @ 功能实现 在 Vue 中实现类似社交平台的 @ 功能,通常涉及输入框的监听、用户匹配和选择插入。以下是具体实现方法: 监听输入框内容 使用 v-model 绑定输入框内容,并通过 @…

vue 实现关闭

vue 实现关闭

Vue 实现关闭功能的方法 在Vue中实现关闭功能通常涉及组件销毁、模态框隐藏或路由跳转等场景。以下是几种常见实现方式: 销毁当前组件 使用v-if或v-show控制组件显示/隐藏,适用于局部组件:…

vue实现input

vue实现input

Vue 实现 Input 输入框的方法 基本双向绑定 使用 v-model 指令实现双向数据绑定,适用于大多数表单场景: <template> <input v-model="…

vue 实现单选

vue 实现单选

使用 v-model 绑定单选值 在 Vue 中,可以通过 v-model 绑定单选按钮的值。定义一个响应式数据(如 selectedOption),并将其与单选按钮的 value 属性关联。…

vue 菜单实现

vue 菜单实现

Vue 菜单实现方法 在Vue中实现菜单功能可以通过多种方式完成,以下是几种常见的实现方法: 使用v-for动态生成菜单 通过数据驱动的方式动态渲染菜单项,适合菜单内容可能变化的场景: <t…

vue实现系统

vue实现系统

Vue 实现系统的核心方法 使用 Vue.js 构建系统时,需结合其响应式特性与组件化开发模式。以下为关键实现路径: 项目初始化与工程配置 通过 Vue CLI 或 Vite 创建项目骨架,推荐选择…