vue+高德地图/ el-amap

一在高德官网申请账号,key和样式地图

1、控制台访问地址,在控制台申请key高德控制台 (amap.com)icon-default.png?t=N7T8https://console.amap.com/dev/index

2、 应用管理->我的应用->添加key

二vue项目中使用

1、下载依赖

npm install --save vue-amap

2、main.js中引入

import VueAMap from '@vuemap/vue-amap';

Vue.use(VueAMap)


VueAMap.initAMapApiLoader({
  key: '你的key值',
   plugin: [            //引入地图功能
     'AMap.PlaceSearch', //POI搜索插件
    'AMap.Autocomplete', //输入提示插件
    'AMap.Scale', //右下角缩略图插件 比例尺
    'AMap.PolyEditor', //编辑 折线多,边形
   
  ]
});

3、在页面中使用

template

  <el-amap vid="amap" :zoom="zoom" :center="center" ref="map" class="amap-demo">
          <el-amap-rectangle :bounds="path" :visible="visible" :editable="editRan" 
            draggable @click="click" @move="move" @adjust="adjust"></el-amap-rectangle>
           <el-amap-search-box :visible="visibleSearch" @select="selectPoi" 
            @choose="selectPoi"/>
  </el-amap>

全部代码:

<template>
      <el-amap vid="amap" :zoom="zoom" :center="center" ref="map" class="amap-demo">
                <el-amap-rectangle :bounds="path" :visible="visible" :editable="editRan" draggable @click="click"
                                   @move="move" @adjust="adjust"></el-amap-rectangle>
                <el-amap-search-box :visible="visibleSearch" @select="selectPoi" @choose="selectPoi"/>
       </el-amap>
             
</template>

<script>



  export default {
    name: 'ReportAreaForm',
    data() {
      return {
       
        zoom: 12,
        center: [117.970675, 37.38268],
        draggable: false,
        visible: true,
        editRan: true,
        //117.976759
        path: [[117.976759, 37.387188], [117.964630, 37.378860]],
        visibleSearch: true
      }
    },
    computed: {
     
    },
    created() {
     
    },
    methods: {
      switchVisible() {
        this.visible = !this.visible
      },
      selectPoi(e) {
        this.visible = false
        let lat = e.poi.location.lat
        let lng = e.poi.location.lng
        console.log('selectPoi: ', e, e.poi.location.lat, ' ', e.poi.location.lng)
        this.center = [lng, lat]
        this.path = [[lng + 0.006084, lat + 0.004508], [lng - 0.006045, lat - 0.00402]]
        this.model.longitude1 = lng + 0.006084
        this.model.latitude1 = lat + 0.004508
        this.model.longitude2 = lng - 0.006045
        this.model.latitude2 = lat - 0.00402
        this.visible = true

      },
      click: (e) => {
        // console.log('点击', e, this.path, this.center)
      },
      toggleVisible() {
        this.visible = !this.visible
      },
      changeDraggable() {
        this.draggable = !this.draggable
      },
      toggleEdit() {
        this.editRan = !this.editRan
        console.log('点击', this.path, this.center)
      },
      adjust(e) {
        console.log('adjust:', e)
        this.model.longitude1 = e.bounds.southWest.lng
        this.model.latitude1 = e.bounds.southWest.lat
        this.model.longitude2 = e.bounds.northEast.lng
        this.model.latitude2 = e.bounds.northEast.lat
        console.log('adjust:', this.model)
      },
      move(e) {
        console.log('move:', e)
        this.model.longitude1 = e.bounds.southWest.lng
        this.model.latitude1 = e.bounds.southWest.lat
        this.model.longitude2 = e.bounds.northEast.lng
        this.model.latitude2 = e.bounds.northEast.lat
        console.log('move:', this.model)
      },
      add() {
        this.edit(this.modelDefault)
      },
    }
  }
</script>
<style>
  .amap-demo {
    height: 500px;
  }
</style>

通过 vid 属性给地图实例赋予了一个ID为 "amap"。:zoom 和 :center 分别表示地图的初始缩放级别(由变量 zoom 控制)和中心点坐标(由变量 center 控制)。ref="map" 是 Vue 中用来获取地图实例引用的方式,便于后续操作。.amap-demo 是 CSS 类名,可以自定义地图样式。

最终显示:

  • 8
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 1
    评论
Vue-amap 是一个基于 Vue.js高德地图组件库,支持在线地图和离线地图。实现高德离线地图,需要先下载离线地图数据,在 Vue-amap 中引入并配置离线地图数据即可。 以下是实现步骤: 1. 下载离线地图数据:在高德官网下载离线地图数据,下载后解压缩得到离线地图数据文件夹。 2. 在 Vue 项目中安装 vue-amap: ``` npm install vue-amap --save ``` 3. 在 main.js 中引入和配置 vue-amap: ``` import VueAMap from 'vue-amap'; Vue.use(VueAMap); VueAMap.initAMapApiLoader({ key: 'your amap key', plugin: ['AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.Geolocation'], // 默认高德 sdk 版本为 1.4.4 v: '1.4.4', // 配置离线地图数据路径,注意路径要使用绝对路径 uiVersion: '1.0', map: { // 配置离线地图数据路径,注意路径要使用绝对路径 mapStyle: 'amap://styles/whitesmoke', features: ['bg', 'road', 'building'], customMapStyle: { 'styleJson': [ { 'featureType': 'water', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'land', 'elementType': 'all', 'stylers': { 'color': '#f3f3f3' } }, { 'featureType': 'railway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'highway', 'elementType': 'all', 'stylers': { 'color': '#fdfdfd' } }, { 'featureType': 'highway', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'arterial', 'elementType': 'geometry', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'arterial', 'elementType': 'geometry.fill', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'poi', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'green', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'subway', 'elementType': 'all', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'manmade', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'local', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'arterial', 'elementType': 'labels', 'stylers': { 'visibility': 'off' } }, { 'featureType': 'boundary', 'elementType': 'all', 'stylers': { 'color': '#fefefe' } }, { 'featureType': 'building', 'elementType': 'all', 'stylers': { 'color': '#d1d1d1' } }, { 'featureType': 'label', 'elementType': 'labels.text.fill', 'stylers': { 'color': '#999999' } } ] }, // 配置离线地图数据路径,注意路径要使用绝对路径 zooms: [3, 20], viewMode: '3D', expandZoomRange: true }, // 配置离线地图数据路径,注意路径要使用绝对路径 filePath: '/path/to/offline/map/data' }); ``` 注意,需要将 filePath 设置为离线地图数据文件夹的绝对路径。 4. 在组件中使用 Vue-amap: ``` <template> <el-amap :zoom="13" :center="center"> <el-amap-marker :position="center"></el-amap-marker> </el-amap> </template> <script> export default { data () { return { center: [116.397428, 39.90923] }; } }; </script> ``` 在 el-amap 中设置 zoom 和 center 即可显示地图。el-amap-marker 可以设置标记点。 至此,就完成了 Vue-amap 实现高德离线地图的步骤。
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值