Vue-amp制作地图基于高德地图

一、安装相关依赖

npm install vue-amp --save

二、在main.js文件中引用

然后在main.js入口文件中引入高德地图,分发出去,让每个组件都可以使用,

    import AMap from 'vue-amap'

    Vue.use(AMap)

初始化地图

AMap.initAMapApiLoader({

// 高德的key

key: 'a33ac630e30cd71cf228e2f465e8a96b',

// 插件集合

plugin: ['AMap.Autocomplete', 'AMap.PlaceSearch', 'AMap.Scale', 'AMap.OverView', 'AMap.ToolBar', 'AMap.MapType', 'AMap.PolyEditor', 'AMap.CircleEditor'],

v: '1.4.4'

});

三、vue-amap | 基于 Vue 2.x 与高德的地图组件

官方网址:组件 | vue-amap

四、某一页面引入地图组件

import mapAdd from './mapAdd.vue'//高德地图

map-add ref="mapEdit" @obtain="position" :index="index">map-add>

五、地图为单独一个组件

<template>
    <div>
      <el-dialog :title="title" :visible.sync="editFormVisible" :modal-append-to-body="false" @close="close" >
        <div id="dialog_content" style="height: 360px;overflow-y: scroll" v-loading="loading">
          <div class="amap-page-container">
            <el-amap-search-box class="search-box" :search-option="searchOption" :on-search-result="onSearchResult" ref="input"></el-amap-search-box>
            <el-amap vid="amapDemo" :center="mapCenter" :zoom="12" class="amap-demo">
              <el-amap-marker v-for="(marker, index) in markers" :key="index" :position="marker"  :draggable="true" :events="marker.events" :visible="marker.visible"></el-amap-marker>
            </el-amap>
          </div>
        </div>
        <div slot="footer" class="dialog-footer">
          <dj-button-save @click="handleSave"/>
          <dj-button-cancel @click="editFormVisible=false"/>
        </div>
      </el-dialog>
    </div>
</template>

<script>

  export default {
    props:['index'],
   data(){
     return{
       editFormVisible:false,
       loading:false,
       title:'',
       ruleForm:{
         name:''
       },
       list:'',//传过来的坐标值
       searchName:'',//传过来的搜索值
       markers: [],
       searchOption: {
         city: '济南',
         citylimit: false
       },
        mapCenter:  [117.11946,36.654395]
     }
   },
    created(){

    },
    methods:{
      onSearchResult(pois) {
        let latSum = 0;
        let lngSum = 0;
        this.markers = []
        if (pois.length > 0) {
          this.markers.push([pois[0].lng, pois[0].lat]);
          pois.forEach(poi => {
            let {lng, lat} = poi;
            lngSum += lng;
            latSum += lat;
          });
          let center = {
            lng: lngSum / pois.length,
            lat: latSum / pois.length
          };
          this.mapCenter = [center.lng, center.lat];
        }
        console.log(this.markers,'marke')
      },
      close(){
        this.loading = true
        this.markers = []
        this.ruleForm.name = ''
        this.$refs['input'].keyword = ''
      },
      handleSave(){
        const position = this.markers[0][0]+'/'+this.markers[0][1];
        const site =  this.$refs['input'].keyword;
        console.log(site)
        this.$emit('obtain',position,this.index,site)
        this.$nextTick(()=>{
          this.editFormVisible = false
        })
      },
      show(visible = true) {
        this.editFormVisible = visible
        this.loading = false
        this.$nextTick(()=>{
          // console.log(this.list,'9999')
          this.$refs['input'].keyword = this.searchName
          this.markers.push(this.list)
          this.mapCenter = this.list
        })
      },
      /**
       * 处理新建
       */
      handleCreate(title,list,searchName) {
        this.title = title
        this.list = list
        this.searchName = searchName
        // 显示弹框
        this.show()
      },
    }
  }
</script>

<style scoped>
#container{
  width: 96%;
  margin-left: 2%;
  height: 350px;
  position: absolute;
  top: 150px;
}
.amap-demo {
  height: 350px;
}

.search-box {
  position: absolute;
  top: 25px;
  left: 20px;
}

.amap-page-container {
  position: relative;
}
</style>

  • 7
    点赞
  • 6
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
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 实现高德离线地图的步骤。

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值