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>

效果图:

在这里插入图片描述

  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
1. 引入高德地图API 在vue3项目中,可以通过在index.html中引入高德地图API的方式来使用高德地图。在head标签中添加以下代码: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=你的高德地图key"></script> ``` 其中,key为你在高德开放平台申请的地图应用的key。 2. 在组件中添加地图容器 在组件模板中添加一个div作为地图容器: ```html <template> <div id="map"></div> </template> ``` 3. 初始化地图 在组件中添加一个mounted生命周期钩子,在其中初始化地图: ```javascript mounted() { const map = new AMap.Map("map", { zoom: 13, //地图级别 center: [116.397428, 39.90923], //中心坐标 }); } ``` 其中,zoom为地图级别,center为地图中心的经纬度坐标。 4. 添加标记 在地图上添加标记,可以通过AMap.Marker类来实现。在mounted生命周期钩子中添加以下代码: ```javascript const marker = new AMap.Marker({ position: [116.397428, 39.90923], //标记位置 title: "这是一个标记", //鼠标悬停时的提示信息 }); marker.setMap(map); //将标记添加到地图中 ``` 其中,position为标记的经纬度坐标,title为鼠标悬停时显示的提示信息。 完整代码如下: ```html <template> <div id="map"></div> </template> <script> export default { mounted() { const map = new AMap.Map("map", { zoom: 13, center: [116.397428, 39.90923], }); const marker = new AMap.Marker({ position: [116.397428, 39.90923], title: "这是一个标记", }); marker.setMap(map); }, }; </script> ``` 运行项目后即可在地图上看到一个标记

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值