最新国产好看的视频,伊人天堂AV在线,国产Aaaaaa视频,蜜臀视频在线观看一区,人妻av色图,密臀久久久精品影片,青青视频免费观看毛片,久草在线观看视,国产三级精品色情在线

Vue引入高德地圖實現(xiàn)流程分步講解

 更新時間:2022年12月01日 15:57:29   作者:清雨未盡時  
這篇文章主要介紹了Vue引入高德地圖實現(xiàn)流程,實現(xiàn)步驟是通過vue的方法引入地圖,初始化地圖,設(shè)置寬和高,本文通過實例代碼給大家介紹的非常詳細,需要的朋友可以參考下

一、功能需求

1.根據(jù)輸入內(nèi)容進行模糊查詢,選擇地址后在地圖上插上標記,并更新經(jīng)緯度坐標顯示

2.在地圖點擊后,根據(jù)回傳的左邊更新地址信息和坐標顯示

二、準備

1.申請高德地圖賬號,創(chuàng)建應(yīng)用

2.在應(yīng)用管理中 獲得key 和安全密鑰

三、在webstorm中安裝

npm i @amap/amap-jsapi-loader -S

四、防止在使用中AMap無法識別問

在eslintrc.js中加入配置:

  globals:{
    "AMap": "true"
  }

五、正式開發(fā)

1.創(chuàng)建頁面

<template>
  <div>
    <label>消息管理</label>
    <div style="margin-top: 20px">
      <div style="height:520px;">
        <div id="all" style="height:100%">
          <div class="posInput">
            <el-input style="width:100%"
                      id="tipinput"
                      class="form-control input-style"
                      type="text"
                      placeholder="請輸入搜索地址"
                      prefix-icon="el-icon-search"
                      v-model="formatAdress"
            >
            </el-input>
          </div>
          <div id="allmap"></div>
          <div class="posSubmit">
            <el-form  ref="form"  label-width="85px" >
              <div class="btn_box" >
                <el-form-item label="經(jīng)度:" >
                  <el-input style="width:400px" disabled class="form-control input-style" type="text" v-model="longitude"> </el-input>
                </el-form-item>
                <el-form-item label="緯度:" >
                  <el-input style="width:400px"  disabled class="form-control input-style" type="text" v-model="latitude"> </el-input>
                </el-form-item>
              </div>
            </el-form>
          </div>
        </div>
      </div>
    </div>
  </div>
</template>

2.頁面樣式

<style scoped>
#all{
  position: relative;
}
#allmap{
  width: 100%;  height: calc(100%  - 50px);
  font-family: "微軟雅黑";
}
.posInput{
  position: absolute;
  z-index: 1;
  width: 80%;
  margin-top: 20px;  margin-left: 10%;
}
.posSubmit{
  position: absolute; z-index: 1; bottom: 0;
  margin-left: 5%;
  width: 90%;
  display: flex;  justify-content: flex-start; align-items: center;
}
.btn_box{
  width: 100%;
  height: 100%;
  display: flex;  ; align-items: center;
}
::v-deep .el-form-item{
  margin-bottom: 0 !important;
}
</style>

3.存儲的數(shù)據(jù)項

data () {
    return {
      map: null,
      marker: null,
      startSeacrh: [],
      stratInfo: {},
      dprops: {
        zoom: 15
      },
      formatAdress: '',
      longitude: '', // 經(jīng)度
      latitude: '', // 緯度
    }
  }

4.創(chuàng)建地圖方法

  mounted () {
    this.initMap()
  },
  methods: {
    initMap () {
      const that = this
      init('allmap', that.dprops).then(AMap => {
        that.map = AMap
        that.map.on('click', that.clickHandler) // 地圖點擊事件 可獲取經(jīng)緯度等信息
        initScaleTools(that.map, true, false)
        searchAutocomplete(that.map, 'tipinput', function (event) {
          that.handleStartSelect(event)
        })
      }).catch(err => {
        this.$message.error(err)
      })
    },
    clickHandler (event) {
      console.log(event, '起點經(jīng)緯度 [lng,lat]')
      if (event.lnglat === '') {
        this.$message({
          type: 'warning',
          message: '該地點無經(jīng)緯度數(shù)據(jù),請輸入具體一點的地點!',
          duration: 5 * 1000
        })
        return
      }
      if (this.marker) {
        this.map.remove(this.marker)
        this.marker = null
      }
      this.startSeacrh = []
      this.startSeacrh = [event.lnglat.lng, event.lnglat.lat]
      this.marker = new AMap.Marker({
        position: this.startSeacrh
      })
      this.map.add(this.marker)
      this.map.setCenter(this.startSeacrh)
      this.longitude = event.lnglat.lng
      this.latitude = event.lnglat.lat
      let that = this
      getAddressByLngLat(this.startSeacrh, function (status, result) {
        if (status === 'complete') {
          that.formatAdress = result.regeocode.formattedAddress
          let adrComponent = result.regeocode.addressComponent
          that.stratInfo = {
            district: adrComponent.province,
            address: adrComponent.district,
            name: adrComponent.township + adrComponent.street + adrComponent.streetNumber,
            fullAdr: result.regeocode.formattedAddress
          }
        }
      })
    },
    handleStartSelect (event) {
      console.log(event, '起點經(jīng)緯度 [lng,lat]')
      if (event.poi.location === '') {
        this.$message({
          type: 'warning',
          message: '該地點無經(jīng)緯度數(shù)據(jù),請輸入具體一點的地點!',
          duration: 5 * 1000
        })
        return
      }
      if (this.marker) {
        this.map.remove(this.marker)
        this.marker = null
      }
      this.startSeacrh = []
      this.startSeacrh = [event.poi.location.lng, event.poi.location.lat]
      this.formatAdress = event.poi.district + event.poi.address + event.poi.name
      this.longitude = event.poi.location.lng
      this.latitude = event.poi.location.lat
      this.marker = new AMap.Marker({
        position: this.startSeacrh
      })
      this.map.add(this.marker)
      this.map.setCenter(this.startSeacrh)
      this.stratInfo = {
        district: event.poi.district,
        address: event.poi.address,
        name: event.poi.name,
        fullAdr: this.formatAdress
      }
    }
  }

5.封裝好的js文件方法

initMap.js

import AMapLoader from '@amap/amap-jsapi-loader'
window._AMapSecurityConfig = {
  securityJsCode: '安全密鑰'
}
const initMap = async (config) => {
  return new Promise((resolve, reject) => {
    AMapLoader.load({
      'key': config.key,
      'version': '1.4.15',
      'plugins': [
        'AMap.PolygonEditor' // 插件
      ],
      'AMapUI': {
        'version': '1.1',
        'plugins': []
      },
      'Loca': {
        'version': '1.3.2'
      }
    }).then((AMap) => {
      resolve(AMap)
    }).catch(err => {
      reject(err)
    })
  })
}
export default initMap

map.js

import initMap from './initMap.js'
export const init = (container, props) => {
  const config = {
    key: 'key'
  }
  return new Promise((resolve, reject) => {
    initMap(config).then(AMap => {
      resolve(new AMap.Map(container, { ...props }))
    }).catch(err => {
      reject(err)
    })
  })
}
/**
 * @param {*} map 地圖實例
 * @param {Boolean} noScale 不需要比例尺  true表示不需要
 * @param {Boolean} noToolBar 不需要工具欄 true表示不需要
 */
export const initScaleTools = (map, noScale, noToolBar) => {
  if (!noScale) {
    map.plugin(['AMap.Scale'], function () {
      var scale = new AMap.Scale()
      map.addControl(scale)
    })
  }
  if (!noToolBar) {
    map.plugin(['AMap.ToolBar'], function () {
      var tool = new AMap.ToolBar()
      map.addControl(tool)
    })
  }
}
//模糊查詢
export const searchAutocomplete = (map, keyword, commpletHandle) => {
  map.clearMap()
  AMap.plugin(['AMap.PlaceSearch', 'AMap.Autocomplete'], function () {
    let autoOptions1 = { input: keyword, city: '全國' }
    let startAutoComplete = new AMap.Autocomplete(autoOptions1)
    AMap.PlaceSearch({
      map: map
    })
    AMap.event.addListener(startAutoComplete, 'select', commpletHandle)
  })
}
/**
 *
 * @param {String} LngLat 經(jīng)緯度
 * @param {Function} callback 回調(diào)函數(shù)
 * @param {Object} otherProps 其他參數(shù)
 */
export const getAddressByLngLat = (LngLat, callback, otherProps) => {
  AMap.plugin('AMap.Geocoder', function () {
    let geocoder = new AMap.Geocoder({
      ...otherProps
    })
    geocoder.getAddress(LngLat, function (status, result) {
      callback(status, result)
    })
  })
}
const mapJS = {
  init,
  initScaleTools,
  searchAutocomplete,
  getAddressByLngLat
}
export default mapJS

在文件中導(dǎo)入 map.js方法

import {
  init,
  initScaleTools,
  searchAutocomplete,
  getAddressByLngLat
} from '../../utils/map'

六、步驟總結(jié)

1.在mounted()中調(diào)用initMap ()初始化地圖

2.初始化成功后、添加事件監(jiān)聽:地圖點擊、模糊查詢。添加放大縮小工具欄

init('allmap', that.dprops).then(AMap => {
        that.map = AMap
        that.map.on('click', that.clickHandler) // 地圖點擊事件 可獲取經(jīng)緯度等信息
        initScaleTools(that.map, true, false)
        searchAutocomplete(that.map, 'tipinput', function (event) {
          that.handleStartSelect(event)
        })
      })

七、效果

到此這篇關(guān)于Vue引入高德地圖實現(xiàn)流程分步講解的文章就介紹到這了,更多相關(guān)Vue高德地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue?serve及其與vue-cli-service?serve之間的關(guān)系解讀

    vue?serve及其與vue-cli-service?serve之間的關(guān)系解讀

    這篇文章主要介紹了vue?serve及其與vue-cli-service?serve之間的關(guān)系,具有很好的參考價值,希望對大家有所幫助,如有錯誤或未考慮完全的地方,望不吝賜教
    2023-10-10
  • vue3+vite使用jsx和tsx詳情

    vue3+vite使用jsx和tsx詳情

    這篇文章主要介紹了vue3+vite使用jsx和tsx詳情,文章通過安裝@vitejs/plugin-vue-jsx展開詳細的內(nèi)容介紹,具有一定的參考價值,需要的小伙伴可以參考一下
    2022-05-05
  • uni-app自定義組件詳細代碼示例

    uni-app自定義組件詳細代碼示例

    組件是vue技術(shù)中非常重要的部分,組件使得與ui相關(guān)的輪子可以方便的制造和共享,進而使得vue使用者的開發(fā)效率大幅提升,這篇文章主要給大家介紹了關(guān)于uni-app自定義組件的相關(guān)資料,需要的朋友可以參考下
    2024-02-02
  • vue3項目如何使用樣式穿透修改elementUI默認樣式

    vue3項目如何使用樣式穿透修改elementUI默認樣式

    這篇文章主要介紹了vue3項目使用樣式穿透修改elementUI默認樣式,本文通過實例代碼給大家介紹的非常詳細,對大家的學習或工作具有一定的參考借鑒價值,需要的朋友可以參考下
    2023-03-03
  • Vue中使一個div鋪滿全屏的實現(xiàn)

    Vue中使一個div鋪滿全屏的實現(xiàn)

    最近在項目開發(fā)中,就遇到了這個問題,Vue中如何使一個div鋪滿全屏,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習學習吧
    2024-07-07
  • Vue中Vue router和axios的封裝使用教程

    Vue中Vue router和axios的封裝使用教程

    當用戶登錄后,后臺會返回一個token給前端,前端下次進入首頁后,會先判斷token是否過期,如果過期自動進入登錄頁面,本文給大家介紹Vue中Vue router和axios的封裝使用教程,感興趣的朋友一起看看吧
    2023-11-11
  • Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器全面詳細講解

    Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器全面詳細講解

    這篇文章主要介紹了Vue聲明式導(dǎo)航與編程式導(dǎo)航及導(dǎo)航守衛(wèi)和axios攔截器,文中通過示例代碼介紹的非常詳細,對大家的學習或者工作具有一定的參考學習價值,需要的朋友們下面隨著小編來一起學習吧
    2023-01-01
  • vue實現(xiàn)圖片拖拽功能

    vue實現(xiàn)圖片拖拽功能

    這篇文章主要為大家詳細介紹了vue實現(xiàn)圖片拖拽功能,文中示例代碼介紹的非常詳細,具有一定的參考價值,感興趣的小伙伴們可以參考一下
    2021-11-11
  • vue3動態(tài)路由刷新出現(xiàn)空白頁的原因與最優(yōu)解

    vue3動態(tài)路由刷新出現(xiàn)空白頁的原因與最優(yōu)解

    頁面刷新白屏其實是因為vuex引起的,由于刷新頁面vuex數(shù)據(jù)會丟失,這篇文章主要給大家介紹了關(guān)于vue3動態(tài)路由刷新出現(xiàn)空白頁的原因與最優(yōu)解的相關(guān)資料,需要的朋友可以參考下
    2023-11-11
  • vue2+element?ui?中的el-table?選中當前行當前行變色的實現(xiàn)代碼

    vue2+element?ui?中的el-table?選中當前行當前行變色的實現(xiàn)代碼

    這篇文章主要介紹了vue2+element?ui?中的el-table?選中當前行當前行變色的實現(xiàn)代碼,本文通過實例代碼給大家介紹的非常詳細,感興趣的朋友跟隨小編一起看看吧
    2024-07-07

最新評論

新巴尔虎右旗| 格尔木市| 隆回县| 广宁县| 宕昌县| 灵宝市| 二连浩特市| 七台河市| 建湖县| 中方县| 阿克苏市| 蓬莱市| 桂林市| 若羌县| 来安县| 连州市| 哈巴河县| 龙川县| 沽源县| 名山县| 遵义县| 禄劝| 和林格尔县| 洞头县| 达州市| 旬邑县| 印江| 西盟| 兴业县| 开原市| 宝清县| 绥中县| 龙里县| 西林县| 中卫市| 文山县| 西丰县| 财经| 申扎县| 南岸区| 临洮县|