首页 > 其他分享 >在vue中使用高德地图

在vue中使用高德地图

时间:2023-01-17 13:34:18浏览次数:49  
标签:map vue 地图 AMap marker new 高德 amap

文章地址:https://blog.csdn.net/weixin_43550562/article/details/127753291
文章地址:https://www.likecs.com/show-204471276.html
npm i @amap/amap-jsapi-loader --save

<template>
  <div class="drawStation">
    <div id="draw-station-container">
      <div class="input">
        <el-input v-model="location.lng" size="small" placeholder="经度" />
        <el-input v-model="location.lat" size="small" placeholder="纬度" />
        <el-input v-model="location.address" size="small" placeholder="搜索地址" />
      </div>
    </div>
  </div>
</template>
 
<script>
import AMapLoader from '@amap/amap-jsapi-loader'
// 设置安全密钥
// window._AMapSecurityConfig = {
//   securityJsCode: '安全密钥'
// }
export default {
  name: 'DrawStation',
  data() {
    return {
      // 地图对象
      AMap: null,
      // 地图实例对象
      map: null,
      marker: null,
      // 经纬度
      location: {
        lat: '',
        lng: '',
        address: ''
      }
    }
  },
  mounted() {
    // DOM初始化完成进行地图初始化
    this.initMap()
  },
  methods: {
    /**
     * 创建地图
     */
    initMap() {
      AMapLoader.load({
        key: 'afae044d3e5f5e49b0e06421d2666b7c', // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: ['AMap.Scale', 'AMap.PlaceSearch', 'AMap.AutoComplete'] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.AMap = AMap
          this.map = new AMap.Map('draw-station-container', {
            // 设置地图容器id
            viewMode: '3D', // 是否为3D地图模式
            zoom: 15, // 初始化地图级别
            center: [114.25335, 22.72724], // 初始化地图中心点位置
            mapStyle: 'amap://styles/normal' // 设置地图的显示样式
          })
 
          this.marker = new AMap.Marker({
            position: new AMap.LngLat(114.25335, 22.72724),
            icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', // 添加 Icon 图标 URL
            title: '深圳'
          })
          this.map.add(this.marker)
          // 移除已创建的 marker
          //   this.map.remove(marker)
 
          // 地图点击事件
          this.map.on('click', this.clickMapHandler)
        })
        .catch((e) => {
          console.log(e)
        })
    },
    // 点击地图事件
    clickMapHandler(e) {
      this.location.lng = e.lnglat.getLng()
      this.location.lat = e.lnglat.getLat()
      this.map.remove(this.marker)
      AMapLoader.load({
        key: 'afae044d3e5f5e49b0e06421d2666b7c', // 申请好的Web端开发者Key,首次调用 load 时必填
        version: '2.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        plugins: ['AMap.Scale', 'AMap.PlaceSearch', 'AMap.AutoComplete'] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        this.AMap = AMap
        this.marker = new AMap.Marker({
          position: new AMap.LngLat(e.lnglat.getLng(), e.lnglat.getLat()),
          icon: 'https://webapi.amap.com/theme/v1.3/markers/n/mark_b.png', // 添加 Icon 图标 URL
          title: '深圳'
        })
        this.map.add(this.marker)
        // 移除已创建的 marker
        // this.map.remove(marker)
      })
    }
  }
}
</script>
 
<style scoped>
.drawStation #draw-station-container {
    padding: 0px;
    /* margin: 20px 0 20px 0; */
    margin: 20px auto;
    width: 100%;
    height: 800px;
}
 
.el-input {
    width: 150px;
    margin: 10px 0 0 10px;
    z-index: 5;
}
</style>

标签:map,vue,地图,AMap,marker,new,高德,amap
From: https://www.cnblogs.com/hxy--Tina/p/17057599.html

相关文章

  • vue项目导出功能_前端导出Excel_Export2Excel.js
    VUE项目导出功能_前端导出Excel_Export2Excel.jsvue项目利用插件直接进行excel生成。1.安装依赖[email protected]......
  • VUE3企业级项目基础框架搭建流程(2)
    typescript安装这里使用的vue项目语言为:TypeScript,不了解的可以先去学习一下。TypeScript中文网正常情况下安装typescript的命令为://全局安装npminstall-gtypescri......
  • Vue 使用localStorage报错:_LocalStorage2.default.getItem is not a function
    问题在mounted中使用localStorage获取数据,没想到报错如下:打断点看过localStorage中存在getItem()方法。这个问题类似之前遇到的canvas2image的那个问题(canvasToImage报......
  • vue H5样式适配PC端分辨率
    VUEH5样式适配PC端分辨率VUE2vue-element-admin脚手架,版本4.4.0,使用笔记本开发125%,在其它电脑端显示时分辨率不是125%的情况下,样式会发生变化,看到的与自己本地开发环......
  • Vue简介
             ......
  • vue select 组件封装及调用
    子组件 mySelect.vue<template><divclass="mySelect"><el-selectv-model="value1"placeholder="请选择"@change="handleSelect"><el-option......
  • VUE3企业级项目基础框架搭建流程(1)
    开发环境和技术栈操作系统windows11开发工具vscode、phpstudy(小皮):nginx1.15.11,mysql5.7.26,php7.4,NavicatforMySQL前端VUE3、TypeScript、SCSS、ElementUi、R......
  • VUEX 使用学习三 : mutations
    转载请注明出处:在Vuex中store数据改变的唯一方法就是提交 mutations。mutations里面装着一些改变数据方法的集合,这是Vuex设计很重要的一点,就是把处理数据逻辑方......
  • VUEX state 的使用学习二
    转载请注明出处:state提供唯一的数据资源,所有的共享的数据都要统一放到store中的state中进行存储;状态state用于存储所有组件的数据。管理数据//初始化vue......
  • VUE项目创建&安装vue等
    npminstall--savevue-hot-reload-api升级或安装cnpmnpminstallcnpm-g升级npmcnpminstallnpm-g最新稳定版$cnpminstallvue@next全局安装vue-cliya......