• vue项目新增高德地图,poi查询,点标记


    一. vue项目里面加入高德地图

    JS API介绍
    开发文档

    二.成为开发者并创建 key

    申请流程

    三.项目准备

    1. 安装插件 npm i @amap/amap-jsapi-loader --save
    2. vue项目引入
    3. 初始化

    实现代码:

    <template>
      <div>
        <div class="top-part">
          <!-- 输入框 -->
          <el-input v-model="inputVal" placeholder="请输入内容" id="searchInput" style="width:200px"></el-input>
        </div>
        <!-- 容器 -->
        <div id="container" ></div>
      </div>
    </template>
    
    <script>
    import AMapLoader from '@amap/amap-jsapi-loader'; // 引入
    window._AMapSecurityConfig = {
      securityJsCode: '你的高德地图账户的安全密钥', // 安全密钥
    }
    export default {
      data() {
        return {
          map: null,
          inputVal: '',
          autoOptions: {
            input: 'searchInput' // searchInput输入框的id
          },
          placeSearch: null,
          auto: null,
        }
      },
      mounted() {
        this.initMap()
      },
      methods: {
        // 初始化地图
        initMap() {
          AMapLoader.load({
            key: "你高德地图账户里面的key",        // 申请好的Web端开发者Key,首次调用 load 时必填
            version: "2.0",      // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
            plugins: ['AMap.ToolBar', 'AMap.Scale', 'AMap.HawkEye', 'AMap.AutoComplete', 'AMap.PlaceSearch'],       // 需要使用的的插件列表,如比例尺'AMap.Scale'等
          }).then((AMap) => {
            this.map = new AMap.Map("container", {  //设置地图容器id
              viewMode: "3D",    //是否为3D地图模式
              zoom: 10,           //初始化地图级别
              center: [120.2, 30.3], //初始化地图中心点位置
            });
            this.map.addControl(new AMap.Scale())
            this.map.addControl(new AMap.ToolBar())
            this.map.addControl(new AMap.HawkEye())
            this.auto = new AMap.AutoComplete(this.autoOptions) // AutoComplete提示插件
            this.placeSearch = new AMap.PlaceSearch({ // 搜索插件
              map: this.map
            })
            // 绑定select事件
            this.auto.on('select', this.select)
            // 点标记
            let marker1 = new AMap.Marker({
              position: [120.2, 30.3],
              title: '杭州'
            })
            this.map.add(marker1)
            // 点标记加图标
            let icon = new AMap.Icon({
              size: new AMap.Size(40, 40),
              image: require('@/assets/imgs/user.png'),
              imageSize: new AMap.Size(40, 40),
            })
            let marker2 = new AMap.Marker({
              position: [120.2, 30.4],
              icon: icon,
              title: '杭州市区'
            })
            this.map.add(marker2)
          }).catch(e => {
            console.log(e);
          })
        },
        select(e) {
          console.log('ee', e);
          this.placeSearch.setCity(e.poi.adcode)
          this.placeSearch.search(e.poi.name)
        }
      }
    }
    </script>
    
    <style scoped>
    .top-part{
      width: 100%;
      display: flex;
      justify-content: center;
      padding: 20px 0;
      background: #F6F6F6;
    }
    #container{
      padding:0px;
      margin: 0px;
      width: 100%;
      height: 700px;
    }
    </style>
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99

    效果图:

    在这里插入图片描述

  • 相关阅读:
    使用Python调用Linux下的动态链接库
    Spring Boot 2.6.0正式发布:默认禁止循环依赖、增强Docker镜像构建...
    怎么将图片旋转45度?
    【JVM笔记】Java虚拟机栈与常见异常和如何设置栈内存大小
    .NET周报【10月第3期 2022-10-25】
    Security ❀ 安全设备学习规范(第二版)
    RedisTemplate map集合使用说明-opsForHash(三)
    PDF应该怎么转换成Excel文档呢?
    基于sklearn的集成学习实战
    switch case 语句(详细)
  • 原文地址:https://blog.csdn.net/qq_45331969/article/details/132764004