欧美bbbwbbbw肥妇,免费乱码人妻系列日韩,一级黄片

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

 更新時間:2019年03月28日 10:33:16   作者:樹沙&  
這篇文章主要介紹了vue-cli中使用高德地圖的方法示例,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧

第一步 去高德地圖開放平臺申請密鑰  高德地圖開放平臺


第二部 在vue-cli項目目錄結(jié)構(gòu) 


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

config.js里面是這樣的  主要是導出密鑰 

// 高德地圖 key
export const MapKey = '你的密鑰key'
// 地圖限定城市
export const MapCityName = '武漢'

utils文件夾里面 新建路一個remoteLoad.js

主要是動態(tài)創(chuàng)建script標簽 封裝了一個函數(shù) 傳入URL地址()

export default function remoteLoad (url, hasCallback) {
 return createScript(url)
 /**
  * 創(chuàng)建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的路徑參數(shù)為模塊名中 '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'
     }))
    })
    // 創(chuàng)建地圖拖拽
    let positionPicker = new PositionPicker({
     mode: 'dragMap', // 設定為拖拽地圖模式,可選'dragMap'、'dragMarker',默認為'dragMap'
     map: map // 依賴地圖對象
    })
    // 拖拽完成發(fā)送自定義 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>

上面 地圖初始化渲染的方法 直接拿別人封裝好的東西

最后運行后


以上就是本文的全部內(nèi)容,希望對大家的學習有所幫助,也希望大家多多支持腳本之家。

相關文章

  • vue?elementUi中的tabs標簽頁使用教程

    vue?elementUi中的tabs標簽頁使用教程

    Tabs 組件提供了選項卡功能,默認選中第一個標簽頁,下面這篇文章主要給大家介紹了關于vue?elementUi中的tabs標簽頁使用的相關資料,文中通過圖文介紹的非常詳細,需要的朋友可以參考下
    2023-03-03
  • Vue多種方法實現(xiàn)表頭和首列固定的示例代碼

    Vue多種方法實現(xiàn)表頭和首列固定的示例代碼

    本篇文章主要介紹了Vue多種方法實現(xiàn)表頭和首列固定的示例代碼,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個參考。一起跟隨小編過來看看吧
    2018-02-02
  • Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)

    Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時)

    這篇文章主要介紹了Vue實現(xiàn)手機號、驗證碼登錄(60s禁用倒計時),幫助大家更好的理解和使用vue,感興趣的朋友可以了解下
    2020-12-12
  • vue單應用在ios系統(tǒng)中實現(xiàn)微信分享功能操作

    vue單應用在ios系統(tǒng)中實現(xiàn)微信分享功能操作

    這篇文章主要介紹了vue單應用在ios系統(tǒng)中實現(xiàn)微信分享功能操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-09-09
  • Vue.js圖片滑動驗證的實現(xiàn)示例

    Vue.js圖片滑動驗證的實現(xiàn)示例

    為了防止有人惡意使用腳本進行批量操作,會設置圖片滑動驗證,本文就介紹了Vue.js圖片滑動驗證的實現(xiàn)示例,感興趣的可以了解一下
    2023-05-05
  • nuxt.js寫項目時增加錯誤提示頁面操作

    nuxt.js寫項目時增加錯誤提示頁面操作

    這篇文章主要介紹了nuxt.js寫項目時增加錯誤提示頁面操作,具有很好的參考價值,希望對大家有所幫助。一起跟隨小編過來看看吧
    2020-11-11
  • Vue3使用contenteditable打造定制化輸入

    Vue3使用contenteditable打造定制化輸入

    contenteditable 屬性為網(wǎng)頁開發(fā)者提供了一種靈活的方式來創(chuàng)建可編輯的內(nèi)容區(qū)域,使用戶可以直接在網(wǎng)頁上進行內(nèi)容編輯,而無需依賴傳統(tǒng)的輸入框,本文將利用contenteditable打造定制化輸入,感興趣的可以了解下
    2023-12-12
  • 如何實現(xiàn)vue加載指令 v-loading

    如何實現(xiàn)vue加載指令 v-loading

    在日常的開發(fā)中,加載效果是非常常見的,但是怎么才能方便的使用,本文介紹如何實現(xiàn)vue加載指令 v-loading,感興趣的朋友一起看看吧
    2024-01-01
  • vue項目中怎樣嵌入其它項目的頁面

    vue項目中怎樣嵌入其它項目的頁面

    這篇文章主要介紹了vue項目中怎樣嵌入其它項目的頁面問題,具有很好 的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue3使用elementPlus進行table合并處理的示例詳解

    vue3使用elementPlus進行table合并處理的示例詳解

    虛擬數(shù)據(jù)中公司下有多個客戶,公司一樣的客戶,公司列需要合并,客戶如果一樣也需要合并進行展示,所以本文給大家介紹了vue3使用elementPlus進行table合并處理的實例,文中通過代碼示例介紹的非常詳細,需要的朋友可以參考下
    2024-02-02

最新評論