vue中使用高德地图 vue-amap 常见配置

35 篇文章 1 订阅

效果:

使用:

先下载依赖包  npm install vue-amap --save

安装、引入依赖

import VueAMap from 'vue-amap'
VueAMap.initAMapApiLoader({
  key: '自己高德地图的key',
  plugin: [
    // 定位空间,用来获取和展示用户主机所在的经纬度位置
    'AMap.Geolocation',
    // 输入提示插件
    'AMap.Autocomplete',
    // POI搜索插件
    'AMap.PlaceSearch',
    // 右下角缩略图插件,比例尺
    'AMap.Scale',
    // 地图鹰眼插件
    'AMap.OverView',
    // 地图工具条
    'AMap.ToolBar',
    // 类别切换空间,实现默认图层与卫星图,实施交通层之间切换的控制
    'AMap.MapType',
    // 编辑 折线多边形
    'AMap.PolyEditor',
    'AMap.CircleEditor',
    // 地图编码
    'AMap.Geocoder'
  ],
  // 默认高德 sdk 版本为 1.4.4
  v: '1.4.4'
})
Vue.use(VueAMap)

代码:

HTML:

<div class="amap-page-container">
    <el-amap-search-box
      class="search-box"
      :search-option="searchOption"
      :on-search-result="onSearchResult"
    ></el-amap-search-box>
    <!-- 搜索框 -->
    <!-- <el-amap-search-box
      class="search-boxMap"
      :search-option="searchOption"
      :on-search-result="onSearchResult"
    >
    </el-amap-search-box> -->
    <el-amap
      ref="map"
      vid="amapDemo"
      :amap-manager="amapManager"
      :center="center"
      :zoom="zoom"
      :plugin="plugin"
      :events="events"
      :rightclick="rightclick"
      class="amap-demo"
    >
      <!-- 在地图上标记点 -->
      <el-amap-marker
        v-for="(marker,index) in markers"
        :key="index"
        :position="marker"
      >
      </el-amap-marker>
      <!-- 坐标点 -->
      <el-amap-marker
        vid="amapDemo"
        :position="center"
      ></el-amap-marker>

      <!-- <el-amap-info-window
        :position="currentWindow.position"
        :content="currentWindow.content"
        :visible="currentWindow.visible"
        :events="currentWindow.events"
      >
      </el-amap-info-window> -->

      <!-- 放置图片 -->
      <!-- <el-amap-ground-image
        v-for="(groundimage,index) in groundimages"
        :url="groundimage.url"
        :key="index+'1'"
        :bounds="groundimage.bounds"
        :events="groundimage.events"
      >
      </el-amap-ground-image> -->

      <!-- 文字 -->
      <el-amap-text
        v-for="(text,index) in texts"
        :text="text.text"
        :key="index+'index'"
        :offset="text.offset"
        :position="text.position"
        :events="text.events"
      >
      </el-amap-text>
    </el-amap>
    <!-- 点击地图的按钮 -->
    <div class="toolbar">
      <button @click="switchWindow(0)">展示第一个位置</button>
      <button @click="switchWindow(1)">展示第二个位置</button>
    </div>
  </div>

js片段:

import { AMapManager } from 'vue-amap'
const amapManager = new AMapManager()
let Geocoder
export default {
  data () {
    const self = this
    return {
      loaded: false,
      amapManager,
      zoom: 20,
      input: '',
      searchOption: {
        city: '',
        citylimit: false
      },
      center: [113.728679, 34.723295],
      events: {
        // init: (o) => {
        //   o.getCity((result) => {
        //     console.log(result)
        //   })
        // },
        moveend: () => { },
        zoomchange: () => { },
        click: (e) => {
          self.center = [e.lnglat.lng, e.lnglat.lat]
          Geocoder.getAddress(self.center, function (status, result) {
            if (status === 'complete' && result.info === 'OK') {
              self.input = result.regeocode.formattedAddress
              document.querySelector('.search-box-wrapper input').value =
                self.input
            }
          })
        },
        // 右键点击事件
        rightclick (e) {
          self.rightclick(e)
        }
      },
      // ToolBar工具条插件、MapType插件、Scale比例尺插件、OverView鹰眼插件
      plugin: [
        // 定位插件  右下
        {
          pName: 'Geolocation',
          // 是否使用高精度定位,默认true
          enableHighAccuracy: true,
          // 超过10秒后停止定位,默认:无穷大
          timeout: 100,
          // 自动偏移后的坐标为高德坐标,默认:true
          convert: true,
          // 显示定位按钮,默认:true
          showButton: true,
          // 定位按钮停靠位置,默认'LB',左下角
          buttonPosition: 'RB',
          // 定位成功后在定位到的位置显示标记,默认:true
          showMarker: true,
          // 定位成功后用圆圈表示定位精度范围,默认:true
          showCircle: true,
          // 定位成功后将定位到的位置作为地图中心点,默认true
          panToLocation: true,
          // 定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
          zoomToAccuracy: true,
          extensions: 'all',
          events: {
            init (o) {
              o.getCurrentPosition((status, result) => {
                if (status === 'error') return alert('定位失败')
                if (result && result.position) {
                  const lng = result.position.lng
                  const lat = result.position.lat
                  self.center = [lng, lat]
                  self.loaded = true
                  self.zoom = 10
                  self.$nextTick()
                }
              })
            }
          }
        },
        {
          pName: 'Geocoder',
          events: {
            init (o) {
              Geocoder = o
              //   o.getAddress(self.center, function (status, result) {
              //     if (status === 'complete' && result.info === 'OK') {
              //       self.input = result.regeocode.formattedAddress
              //       document.querySelector('.search-box-wrapper input').value =
              //         self.input
              //     }
              //   })
            }
          }
        },
        // 地图工具条插件  左上
        {
          pName: 'ToolBar',
          events: {
            // init (o) {
            //   console.log(o)
            // }
          }
        },

        {
          pName: 'MapType',
          defaultType: 0
          // events: {
          //   init (o) {
          //     console.log(o)
          //   }
          // }
        },
        // 比例尺插件  左下
        {
          pName: 'Scale'
          // events: {
          //   init (instance) {
          //     console.log(instance)
          //   }
          // }
        }, {
          pName: 'OverView'
          // events: {
          //   init (instance) {
          //     console.log(instance)
          //   }
          // }
        }],
      markers: [
        [121.49996, 31.297646],
        [121.40018, 31.197622],
        [121.49991, 31.207649]
      ],
      windows: [ // 窗口信息框
        {
          position: [121.69996, 31.237646],
          content: '<div>我在这里呢 !</div>',
          visible: true,
          events: {
            close () {
              console.log('关闭窗口一!')
            }
          }
        }, {
          position: [121.5875285, 31.21515044],
          content: '<div>我在国际博览中心!</div>',
          visible: true,
          events: {
            close () {
              console.log('关闭窗口二!')
            }
          }
        }
      ],
      // 放置图片
      groundimages: [
        {
          url: 'https://aos-cdn-image.amap.com/pp/avatar/a12/ce/fe/57710054.jpg?ver=1531037095&imgoss=1',
          bounds: [[121.5273285, 31.21515044], [122.9273285, 32.31515044]],
          events: {
            click () {
              alert('click groundimage')
            }
          }
        }
      ],
      // 放置文字
      texts: [
        {
          position: [121.49996, 31.297646],
          text: '<div>第一个</div>',
          offset: [0, -50],
          events: {
            click: () => {
              console.log('1')
            }
          }
        },
        {
          position: [121.40018, 31.197622],
          text: '<div>第二个</div>',
          offset: [0, -50],
          events: {
            click: () => {
              console.log('2')
            }
          }
        }
      ],
      // 当前窗口
      currentWindow: {
        position: [0, 0],
        content: '',
        events: {},
        visible: false
      }
    }
  },
  methods: {
    // 切换信息窗口
    switchWindow (tab) {
      this.currentWindow.visible = false
      this.$nextTick(() => {
        this.currentWindow = this.windows[tab]
        this.currentWindow.visible = true
      })
    },
    // 搜索事件
    onSearchResult (pois) {
      // this.input = document.querySelector('.search-box-wrapper input').value
      // this.center = [pois[0].lng, pois[0].lat]
      if (pois.length > 0) {
        this.zoom = 16
        var poi = pois[0]
        const lng = poi.lng
        const lat = poi.lat
        this.center = [lng, lat]
      }
      // console.log(this.center)
    },
    rightclick (e) {
      console.log(e)
    }
  }
}

css样式

<style lang='less' scoped>
// .search-boxMap {
//   position: absolute;
//   top: 25px;
//   left: 70px;
// }
// .amap-page-container {
//   position: relative;
// }
.amap-page-container {
  width: 100%;
  height: 400px;
  margin-bottom: 20px;
}
.toolbar button {
  background: #42b983;
  border: 0;
  color: white;
  padding: 8px;
  margin: 0 5px;
  border-radius: 3px;
  cursor: pointer;
  margin-top: 10px;
}
</style>
<style>
.el-vue-search-box-container {
  width: 100% !important;
  margin-bottom: 10px;
}
.search-box-wrapper {
  background-color: #eee;
}
</style>

或者可以使用封装的高德地图  效果不是太好看

地址:AMap-Vue

  • 2
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 2
    评论
Vue使用高德SDK可以通过vue-amap插件来实现。以下是一些基本步骤: 1. 首先,通过npm或yarn安装vue-amap插件。可以使用以下命令进行安装: ```bash npm install vue-amap ``` 2. 在main.js文件引入vue-amap插件: ```javascript import VueAMap from 'vue-amap'; Vue.use(VueAMap); ``` 3. 在Vue组件使用vue-amap插件。可以在组件的mounted钩子函数进行初始化和配置。 ```javascript mounted() { // 初始化vue-amap this.initAMapAPI(); }, methods: { initAMapAPI() { // 配置vue-amap VueAMap.initAMapApiLoader({ key: 'your-amap-api-key', plugin: ['AMap.Geolocation'] }); // 加载插件模块 VueAMap .then(() => { // 创建地图对象 const map = new VueAMap.Map('map-container'); // 添加地图控件等操作 // ... // 获取当前位置 this.getCurrentLocation(map); }) .catch(error => { console.log('地图加载失败:', error); }); }, getCurrentLocation(map) { // 使用高德SDK定位功能获取当前位置 AMap.plugin('AMap.Geolocation', () => { const geolocation = new VueAMap.Geolocation(); map.addControl(geolocation); geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 获取到当前位置信息 console.log('当前位置:', result); } else { // 获取失败 console.log('获取位置失败'); } }); }); } } ``` 通过以上步骤,你就可以在Vue使用高德SDK了。需要注意的是,在使用过程要替换'your-amap-api-key'为你自己的高德API密钥。并且根据需求,你还可以添加更多高德SDK的功能和组件。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值