vue2项目详细使用高德地图教程,超详细!!!

高德官网教程地址:https://lbs.amap.com/api/javascript-api/guide/abc/prepare

官网其实很详细啦,但这是比官网还详细的保姆级教程!

入门准备:

成功注册账号后,申请key:
1.
在这里插入图片描述
2.
在这里插入图片描述

3.在这里插入图片描述
在项目中,assets文件中新建一个js文件,用于引入高德api,代码:

export default function MapLoader () {
  return new Promise((resolve, reject) => {
    if (window.AMap) {
      resolve(window.AMap)
    } else {
      // 动态创造DOM,引入api
      let script = document.createElement('script')
      script.type = 'text/javascript'
      script.async = true
      script.src = 'http://webapi.amap.com/maps?v=1.3&callback=initAMap&key=注册的key值'
      script.onerror = reject
      document.body.appendChild(script)
    }
    window.initAMap = () => {
      resolve(window.AMap)
    }
  })
}

接下来在webpack.base.conf.js文件里添加:
与entry同级

externals: {
    'AMap': 'AMap'
  },

在components中新建Amap.vue,代码如下:

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

<script>
  import MapLoader from '../assets/amap'

  export default {
      name: 'Amap',
      data () {
        return {
          map: '',
        }
      },
      mounted() {
        this.initAMap()
      },
      methods: {
          initAMap () {
            let that = this
            MapLoader().then(AMap => {
              that.map = new AMap.Map('container', {
                zoom: 11, // 级别
                center: [116.397428, 39.90923], // 中心点坐标
                viewMode:'3D' // 使用3D视图
              })
              // 异步加载插件
              // AMap.ToolBar: 滑动工具条, AMap.Scale: 比例尺
              // 一次加载多个
              AMap.plugin(['AMap.ToolBar', 'AMap.Scale'], function () {
                that.map.addControl(new AMap.ToolBar())
                that.map.addControl(new AMap.Scale())
              })
              // AMap.Geolocation: 定位
              AMap.plugin('AMap.Geolocation', function () {
               let geolocation = new AMap.Geolocation({
                 enableHighAccuracy: true, //是否使用高精度定位,默认:true
                 timeout: 10000,           //超过10秒后停止定位,默认:无穷大
                 maximumAge: 0,            //定位结果缓存0毫秒,默认:0
                 convert: true,            //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
                 showButton: true,         //显示定位按钮,默认:true
                 buttonPosition: 'LB',     //定位按钮停靠位置,默认:'LB',左下角
                 buttonOffset: new AMap.Pixel(10, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
                 showMarker: true,         //定位成功后在定位到的位置显示点标记,默认:true
                 showCircle: true,         //定位成功后用圆圈表示定位精度范围,默认:true
                 panToLocation: true,      //定位成功后将定位到的位置作为地图中心点,默认:true
                 zoomToAccuracy:true       //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
                })
                that.map.addControl(geolocation)
                
                // 获取当前定位的信息
                geolocation.getCurrentPosition((status,result) => {
                  console.log(result.addressComponent)
                })
                // AMap.event.addListener(geolocation, 'complete', onComplete);//返回定位信息
                // AMap.event.addListener(geolocation, 'error', onError);      //返回定位出错信息
              })
            })
          }
      }
  }
</script>

<style scoped>
  /*隐藏高德地图logo,版本号*/
  /deep/ .amap-logo{
    visibility: hidden;
  }
  /deep/ .amap-copyright {
    visibility: hidden;
  }
</style>

如果注释还有不明白的地方,可以看官网教程哦~

  • 1
    点赞
  • 36
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
回答: 要在vue2项目使用高德地图,首先需要进入高德地图的官方网站申请一个key和密钥,创建一个项目。然后,安装高德地图的包,可以使用npm i @amap/amap-jsapi-loader --save命令进行安装。接下来,新建一个放地图的vue组件,并在mounted生命周期中调用创建地图的函数。在这个函数中,可以设置地图的缩放级别、中心点坐标、地图样式等参数。具体的代码可以参考官方文档。\[1\]\[2\]\[3\] #### 引用[.reference_title] - *1* [vue2项目中引入高德地图--1 简单应用](https://blog.csdn.net/Zhang_zhangxka/article/details/128655881)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *2* [Vue2项目使用高德地图](https://blog.csdn.net/GP2719434241/article/details/130521598)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] - *3* [前端系列——vue2+高德地图web端开发(使用和引入)](https://blog.csdn.net/weixin_55683934/article/details/130237445)[target="_blank" data-report-click={"spm":"1018.2226.3001.9630","extra":{"utm_source":"vip_chatgpt_common_search_pc_result","utm_medium":"distribute.pc_search_result.none-task-cask-2~all~insert_cask~default-1-null.142^v91^control_2,239^v3^insert_chatgpt"}} ] [.reference_item] [ .reference_list ]
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值