当前位置: 移动技术网 > IT编程>脚本编程>vue.js > vue-cli中使用高德地图的方法示例

vue-cli中使用高德地图的方法示例

2019年07月23日  | 移动技术网IT编程  | 我要评论

第一步 去高德地图开放平台申请密钥  


第二部 在vue-cli项目目录结构 


里面多了config文件夹和 utils文件夹 

config.js里面是这样的  主要是导出密钥 

// 高德地图 key
export const mapkey = '你的密钥key'
// 地图限定城市
export const mapcityname = '武汉'

utils文件夹里面 新建路一个remoteload.js

主要是动态创建script标签 封装了一个函数 传入url地址()

export default function remoteload (url, hascallback) {
 return createscript(url)
 /**
  * 创建script
  * @param url
  * @returns {promise}
  */
 function createscript (url) {
  var scriptelement = document.createelement('script')
  document.body.appendchild(scriptelement)
  var promise = new promise((resolve, reject) => {
   scriptelement.addeventlistener('load', e => {
    removescript(scriptelement)
    if (!hascallback) {
     resolve(e)
    }
   }, false)

   scriptelement.addeventlistener('error', e => {
    removescript(scriptelement)
    reject(e)
   }, false)

   if (hascallback) {
    window.____callback____ = function () {
     resolve()
     window.____callback____ = null
    }
   }
  })

  if (hascallback) {
   url += '&callback=____callback____'
  }

  scriptelement.src = url

  return promise
 }

 /**
  * 移除script标签
  * @param scriptelement script dom
  */
 function removescript (scriptelement) {
  document.body.removechild(scriptelement)
 }
}

第三步 在home组件中

<template>
 <div class="m-map">
  <div class="search" v-if="placesearch">
   <input type="text" placeholder="请输入关键字" v-model="searchkey">
   <button type="button" @click="handlesearch">搜索</button>
   <div id="js-result" v-show="searchkey" class="result"></div>
  </div>
  <div id="js-container" class="map"></div>
 </div>
</template>

<script>
import remoteload from '@/utils/remoteload.js'
import { mapkey, mapcityname } from '@/config/config'
export default {
 props: ['lat', 'lng'],
 data () {
  return {
   searchkey: '',
   placesearch: null,
   dragstatus: false,
   amapui: null,
   amap: null
  }
 },
 watch: {
  searchkey () {
   if (this.searchkey === '') {
    this.placesearch.clear()
   }
  }
 },
 methods: {
  // 搜索
  handlesearch () {
   if (this.searchkey) {
    this.placesearch.search(this.searchkey)
   }
  },
  // 实例化地图
  initmap () {
   // 加载positionpicker,loadui的路径参数为模块名中 'ui/' 之后的部分
   let amapui = this.amapui = window.amapui
   let amap = this.amap = window.amap
   amapui.loadui(['misc/positionpicker'], positionpicker => {
    let mapconfig = {
     zoom: 16,
     cityname: mapcityname
    }
    if (this.lat && this.lng) {
     mapconfig.center = [this.lng, this.lat]
    }
    let map = new amap.map('js-container', mapconfig)
    // 加载地图搜索插件
    amap.service('amap.placesearch', () => {
     this.placesearch = new amap.placesearch({
      pagesize: 5,
      pageindex: 1,
      citylimit: true,
      city: mapcityname,
      map: map,
      panel: 'js-result'
     })
    })
    // 启用工具条
    amap.plugin(['amap.toolbar'], function () {
     map.addcontrol(new amap.toolbar({
      position: 'rb'
     }))
    })
    // 创建地图拖拽
    let positionpicker = new positionpicker({
     mode: 'dragmap', // 设定为拖拽地图模式,可选'dragmap'、'dragmarker',默认为'dragmap'
     map: map // 依赖地图对象
    })
    // 拖拽完成发送自定义 drag 事件
    positionpicker.on('success', positionresult => {
     // 过滤掉初始化地图后的第一次默认拖放
     if (!this.dragstatus) {
      this.dragstatus = true
     } else {
      this.$emit('drag', positionresult)
     }
    })
    // 启动拖放
    positionpicker.start()
   })
  }
 },
 async created () {
  // 已载入高德地图api,则直接初始化地图
  if (window.amap && window.amapui) {
   this.initmap()
  // 未载入高德地图api,则先载入api再初始化
  } else {
   await remoteload(`http://webapi.amap.com/maps?v=1.3&key=${mapkey}`)
   await remoteload('http://webapi.amap.com/ui/1.0/main.js')
   this.initmap()
  }
 }
}
</script>

<style lang="css">
.m-map{ min-width: 500px; min-height: 300px; position: relative; }
.m-map .map{ width: 100%; height: 100%; }
.m-map .search{ position: absolute; top: 10px; left: 10px; width: 285px; z-index: 1; }
.m-map .search input{ width: 180px; border: 1px solid #ccc; line-height: 20px; padding: 5px; outline: none; }
.m-map .search button{ line-height: 26px; background: #fff; border: 1px solid #ccc; width: 50px; text-align: center; }
.m-map .result{ max-height: 300px; overflow: auto; margin-top: 10px; }
</style>

第四步  在app.vue中 导入组件

<template>
 <div id="app">
  <div class="g-wraper">
   <div class="m-part">
    <mapdrag @drag="dragmap" class="mapbox"></mapdrag>
   </div>
  </div>

 </div>
</template>

<script>
import mapdrag from './components/home.vue'
export default {
 name: 'app',
 components: {
  mapdrag
 },
 data () {
  return {
   dragdata: {
    lng: null,
    lat: null,
    address: null,
    nearestjunction: null,
    nearestroad: null,
    nearestpoi: null
   }
  }
 },
 methods: {
  dragmap (data) {
   console.log(data)
   this.dragdata = {
    lng: data.position.lng,
    lat: data.position.lat,
    address: data.address,
    nearestjunction: data.nearestjunction,
    nearestroad: data.nearestroad,
    nearestpoi: data.nearestpoi
   }
  }
 }
}
</script>

<style>
body{ margin: 0; }
.page-header{
 color: #fff; text-align: center; background: #159957;
 background-image: -webkit-linear-gradient(330deg,#155799,#159957);
 background-image: linear-gradient(120deg,#155799,#159957);
 padding: 3rem 4rem; margin-bottom: 30px;
}
.page-header h1{ margin: 0; font-size: 40px; }
.page-header p{ color: #ccc; margin: 0; margin-bottom: 30px; }
.page-header a{ display: inline-block; border: 1px solid #fff; margin-right: 10px; line-height: 40px; padding: 0 20px; border-radius: 4px; color: #fff; text-decoration: none; transition: all .3s; }
.page-header a:hover{ background: #fff; color: #333; }
.g-wraper{ width: 1000px; margin: 0 auto; color: #666; font-size: 16px; line-height: 30px; }
.m-part{ margin-bottom: 30px; }
.m-part::after{ content: ''; display: block; clear: both; }
.m-part .title{ font-size: 30px; line-height: 60px; margin-bottom: 10px; color: #333; }
.m-part .mapbox{ width: 600px; height: 400px; margin-bottom: 20px; float: left; }
.m-part .info{ margin: 0; padding: 0; list-style: none; line-height: 30px; margin-left: 620px; }
.m-part .info span{ display: block; color: #999; }
.m-part ol{ line-height: 40px; margin-left: 0; padding-left: 0; }
.m-part pre{ padding: 10px 20px; line-height: 30px; border-radius: 3px; box-shadow: 0 0 15px rgba(0,0,0,.5); }
.m-footer{ background: #eee; line-height: 60px; text-align: center; color: #999; font-size: 12px; }
.m-footer a{ margin: 0 5px; color: #999; text-decoration: none; }
</style>

上面 地图初始化渲染的方法 直接拿别人封装好的东西

最后运行后


以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持移动技术网。

如对本文有疑问, 点击进行留言回复!!

相关文章:

验证码:
移动技术网