当前位置:首页 > VUE

vue实现地图导航

2026-01-23 08:29:17VUE

使用高德地图API实现Vue地图导航

安装高德地图JavaScript API的Vue封装库

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

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

vue实现地图导航

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

export default {
  data() {
    return {
      map: null,
      AMap: null
    }
  },
  mounted() {
    this.initMap()
  },
  methods: {
    initMap() {
      AMapLoader.load({
        key: '您的高德地图Key',  // 替换为实际key
        version: '2.0',
        plugins: ['AMap.Driving']
      }).then((AMap) => {
        this.AMap = AMap
        this.map = new AMap.Map('map-container', {
          viewMode: '2D',
          zoom: 13,
          center: [116.397428, 39.90923]  // 默认北京中心点
        })
      })
    }
  }
}

添加路线规划功能

在组件中添加导航方法

methods: {
  drawRoute(start, end) {
    const driving = new this.AMap.Driving({
      map: this.map,
      policy: this.AMap.DrivingPolicy.LEAST_TIME
    })

    driving.search([
      { keyword: start, city: '城市名' },
      { keyword: end, city: '城市名' }
    ], (status, result) => {
      if (status === 'complete') {
        console.log('路线规划完成')
      } else {
        console.error('路线规划失败', result)
      }
    })
  }
}

添加地图标记和信息窗口

实现地点标记和信息展示

vue实现地图导航

addMarker(position, title) {
  const marker = new this.AMap.Marker({
    position: position,
    title: title,
    map: this.map
  })

  const infoWindow = new this.AMap.InfoWindow({
    content: `<div>${title}</div>`
  })

  marker.on('click', () => {
    infoWindow.open(this.map, marker.getPosition())
  })
}

集成位置搜索功能

添加地点搜索方法

searchPlace(keyword) {
  const placeSearch = new this.AMap.PlaceSearch({
    map: this.map,
    city: '城市名'
  })

  placeSearch.search(keyword, (status, result) => {
    if (status === 'complete') {
      result.poiList.pois.forEach(poi => {
        this.addMarker([poi.location.lng, poi.location.lat], poi.name)
      })
    }
  })
}

响应式地图容器样式

确保地图容器正确显示

<template>
  <div id="map-container" style="width: 100%; height: 500px;"></div>
</template>

注意事项

  1. 需要在项目中引入高德地图的安全密钥
  2. 使用前需在高德开放平台申请Web端Key
  3. 实际部署时需要考虑跨域问题和https协议要求
  4. 移动端需额外处理触摸事件和响应式布局

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

相关文章

vue实现扫描二维码

vue实现扫描二维码

实现 Vue 扫描二维码功能 在 Vue 中实现扫描二维码功能,可以通过第三方库或原生 API 完成。以下是两种常见方法: 使用 vue-qrcode-reader 库 vue-qrcode-rea…

vue实现菜单

vue实现菜单

Vue 实现菜单的方法 在 Vue 中实现菜单功能可以通过多种方式完成,以下是几种常见的方法: 使用 v-for 动态生成菜单项 通过数据驱动的方式,利用 v-for 指令动态渲染菜单项。定义一个…

vue实现菜单调用

vue实现菜单调用

Vue 实现动态菜单的常见方法 基于路由配置生成菜单 Vue Router 的路由配置可以天然转化为菜单结构,适合权限与路由绑定的系统: // router/index.js const rout…

vue实现压缩上传文件

vue实现压缩上传文件

压缩上传文件的实现方法 在Vue中实现文件压缩和上传功能,可以通过以下步骤完成。该方法结合了前端文件压缩库和HTTP请求,确保文件在上传前被有效压缩。 安装必要的依赖 需要使用compressorj…

在线脑图 vue 实现

在线脑图 vue 实现

在线脑图 Vue 实现方案 使用开源库实现 推荐使用 mind-elixir 或 jsmind 这类开源库,它们专为脑图设计且支持 Vue 集成。 安装依赖: npm install mi…

vue实现签约合作弹窗

vue实现签约合作弹窗

实现签约合作弹窗的步骤 使用Vue实现签约合作弹窗可以通过组件化方式完成,结合模态框和表单交互逻辑。以下是具体实现方法: 1. 创建弹窗组件 新建一个Vue组件文件(如SignContractDia…