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

關(guān)于vue的element-ui web端引入高德地圖并獲取經(jīng)緯度

 更新時(shí)間:2023年04月25日 09:48:18   作者:wendycwb  
這篇文章主要介紹了關(guān)于vue的element-ui web端引入高德地圖并獲取經(jīng)緯度,高德地圖首先要去申請key和密鑰,文中提供了部分實(shí)現(xiàn)代碼和解決思路,感興趣的朋友可以學(xué)習(xí)一下

發(fā)版前接到一個(gè)臨時(shí)新需求 ,需要在web端地址選擇時(shí)用地圖,并獲取經(jīng)緯度。 臨陣發(fā)版之際加需求,真的是很頭疼,于是趕緊找度娘,找api。 我引入的是高德地圖,首先要去申請key , 和密鑰,

首先用npm 安裝loader

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

然后在main.js里引入

在這里插入圖片描述

這里要注意,還需要在index.html文件里引入這一段,開始我沒有引入這段,后面請求高德接口時(shí)就會報(bào)錯

在這里插入圖片描述

這里我寫了一個(gè)組件,后面直接引用就可以 組件內(nèi)容如下:

在這里插入圖片描述

內(nèi)容有點(diǎn)多,截不完圖,下面附上源碼:

<template lang="html">
  <el-dialog v-dialogDrag title="選擇地點(diǎn)" append-to-body width="600px" :visible.sync="mvisible" :close-on-click-modal="false"
             @close="cancel"
  >
    <div id="amap-container">
      <el-input
        id="search-input"
        v-model="searchValue"
        class="input-with"
        placeholder="請輸入地址"
        clearable
        @clear="handelclearInput"
        @keyup.native.enter="onhandelSearch"
      >
        <el-button
          slot="append"
          size="small"
          type="primary"
          icon="el-icon-search"
          @click="onhandelSearch"
        >搜索
        </el-button>
      </el-input>
      <el-row class="margin-top-10 address">
        當(dāng)前地址是: {{ formattedAddress }}
      </el-row>
      <div id="custom-amap" />
    </div>
    <div slot="footer" class="dialog-footer">
      <el-button type="primary" @click="handelSave">保 存</el-button>
      <el-button @click="cancel">取 消</el-button>
    </div>
  </el-dialog>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader'
// const AMap = window.AMap
export default {
  name: 'AMap',
  props: {
    defaultValue: {
      type: String,
      default: ''
    },
    visible: {
      type: Boolean,
      default: false
    }
  },
  data() {
    return {
      mvisible: false,
      defaultCity: '',
      // 地圖對象
      map: null,
      // 定位默認(rèn)地址 | 搜索后選擇的地址
      formattedAddress: null,
      // 地址對應(yīng)的經(jīng)緯度信息
      position: {},
      // 檢索關(guān)鍵字
      searchValue: '',
      // 檢索函數(shù)對象
      placeSearch: null,
      // 檢索結(jié)果數(shù)據(jù)數(shù)據(jù)
      searchInfoList: [],
      // 地圖標(biāo)記
      marker: '',
      // 地址解析(正向)
      geocoder: '',
      // 地址名稱
      name: '',
      adcode: ''
    }
  },
  watch: {
    defaultValue() {
      this.searchValue = this.defaultValue
    },
    visible() {
      this.mvisible = this.visible
      this.searchValue = this.defaultValue
      // this.searchValue = '四川省成都市武侯區(qū)'
      this.formattedAddress = this.defaultValue
      // 初始化地圖頁面
      this.initMap()
    }
  },
  beforeDestroy() {
    // 銷毀地圖
    this.map.destroy()
  },
  methods: {
    //   初始化地圖頁面
    initMap() {
      AMapLoader.load({
        key: 'dc4da34d26ef0a0851ce91ce099f6f46', // 申請好的Web端開發(fā)者Key,首次調(diào)用 load 時(shí)必填
        version: '2.0', // 指定要加載的 JSAPI 的版本,缺省時(shí)默認(rèn)為 1.4.15
        plugins: [''] // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      }).then((AMap) => {
        this.map = new AMap.Map('custom-amap', { // 設(shè)置地圖容器id
          viewMode: '3D', // 是否為3D地圖模式
          zoom: 5, // 初始化地圖級別
          resizeEnable: true,
          center: [105.602725, 37.076636] // 初始化地圖中心點(diǎn)位置
        })
        // 添加maker
        this.setMaker()
        // 添加鼠標(biāo)點(diǎn)選地圖選擇地址
        this.addAmapGeocoder()
        this.onhandelSearch()
      }).catch(e => {
        console.log(e)
      })
    },
    // 添加maker
    setMaker() {
      // eslint-disable-next-line no-undef
      this.marker = new AMap.Marker()
      this.map.add(this.marker)
      // 添加解析地理位置插件
      this.map.plugin('AMap.Geocoder', () => {
        // 異步加載插件
        this.geocoder = new AMap.Geocoder({
          city: this.defaultCity, // 默認(rèn):“全國”
          radius: 1000 // 范圍,默認(rèn):500
        })
      })
    },
    // 添加鼠標(biāo)點(diǎn)選地圖選擇地址
    addAmapGeocoder() {
      // 添加maker
      this.setMaker()
      // 地圖添加點(diǎn)擊事件
      this.map.on('click', function(e) {
        console.log('e.lnglat.getLng()', e.lnglat.getLng())
        // document.getElementById("lnglat").value = e.lnglat.getLng() + ',' + e.lnglat.getLat()
      })
      this.map.on('click', e => {
        console.log('e====', e)
        const lnglat = [e.lnglat.getLng(), e.lnglat.getLat()]
        this.marker.setPosition(lnglat)
        this.geocoder.getAddress(lnglat, (status, result) => {
          if (status === 'complete' && result.regeocode) {
            const res = result.regeocode
            const { adcode, province, city, district } = res.addressComponent
            this.searchValue = res.formattedAddress
            const name = province + city + district
            const sdata = { adcode, lng: lnglat[0], lat: lnglat[1], name }
            this.searchSuccessData(sdata)
            console.log('result', result)
          } else {
            console.log(JSON.stringify(result))
          }
        })
      })
    },
    // 按鈕觸發(fā)檢索
    onhandelSearch() {
      const that = this
      this.geocoder.getLocation(this.searchValue, function(status, result) {
        if (status === 'complete' && result.geocodes.length) {
          const { lng, lat } = result.geocodes[0].location
          const { province, city, district } = result.geocodes[0].addressComponent
          const { adcode } = result.geocodes[0]
          const name = province + city + district
          const sdata = { adcode, lng, lat, name }
          that.searchSuccessData(sdata)
          that.marker.setPosition([lng, lat])
          that.map.add(that.marker)
          that.map.setFitView(that.marker)
        } else {
          this.$message.error('根據(jù)地址查詢位置失敗')
        }
      })
    },
    searchSuccessData(res) {
      this.formattedAddress = this.searchValue
      this.adcode = res.adcode
      this.name = res.name
      this.position = { lng: res.lng, lat: res.lat }
    },
    // 清除input里的值,清除搜索結(jié)果,再次初始化map
    handelclearInput() {
      document.querySelector('#searchResultPanel').innerHTML = ''
    },
    // 保存當(dāng)前選擇的地址,分發(fā)事件
    handelSave() {
      this.searchValue = this.formattedAddress
      const { lat, lng } = this.position
      if (lat && lng) {
        const data = {
          name: this.name,
          adcode: this.adcode,
          // 地址名稱
          address: this.formattedAddress,
          // 緯度lat
          lat,
          // 經(jīng)度lng
          lng
        }
        this.$emit('getPosition', true, data)
      } else {
        this.$message.error('請選擇地址獲取經(jīng)緯度')
      }
    },
    cancel() {
      this.$emit('getPosition', false)
    }
  }
}
</script>

<style scoped lang="scss">
#amap-container {
  margin: 20px;

  .el-input__clear {
    line-height: 34px;
    /*top: 20px;*/
  }

  #custom-amap {
    height: 30vh;
    width: 100%;
    margin-top: 10px;
    border: 1px solid #ccc;
  }

  .input-with {
    /*position: fixed;*/
    /*top: 40px;*/
    z-index: 1;
    width: 580px;
  }

  .address {
    color: #373737;
  }

}

.amap-sug-result {
  z-index: 99999;
}
</style>

然后在需要的文件里引入就可以: 當(dāng)我點(diǎn)擊這個(gè)輸入框時(shí),就會彈出地圖組件

在這里插入圖片描述

這個(gè)是地圖組件:

在這里插入圖片描述

引用組件的代碼如下:

 <el-input v-model="basicFormValidate.businessAddressDetail" @click.native="initMapConfirm" />
 <amap :visible="amapVisible" :default-value="basicFormValidate.businessAddressDetail" :business-province-id="basicFormValidate.businessProvinceId" @getPosition="mapConfirm" />
 import Amap from '@/views/common/Amap'
 components: { Amap }

    initMapConfirm() {
      this.amapVisible = true
    },
    mapConfirm(flag, data) {
      this.amapVisible = false
      if (flag) {
        this.basicFormValidate.businessAddressDetail = data.address
        this.basicFormValidate.businessAddressLatitude = data.lat
        this.basicFormValidate.businessAddressLongitude = data.lng
        this.basicFormValidate.businessProvinceId = data.businessProvinceId
      }
    }

最后的結(jié)果就是這樣的

在這里插入圖片描述

如果說之前有地址,那會代入并反向定位,獲取其經(jīng)緯度

在這里插入圖片描述

到此這篇關(guān)于關(guān)于vue的element-ui web端引入高德地圖并獲取經(jīng)緯度的文章就介紹到這了,更多相關(guān)element-ui高德地圖獲取經(jīng)緯度內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!

相關(guān)文章

  • vue2中l(wèi)ess的安裝以及使用教程

    vue2中l(wèi)ess的安裝以及使用教程

    less是css預(yù)處理器,對原先css進(jìn)行了擴(kuò)展和補(bǔ)充,下面這篇文章主要給大家介紹了關(guān)于vue2中l(wèi)ess的安裝以及使用的相關(guān)資料,文中通過實(shí)例代碼介紹的非常詳細(xì),需要的朋友可以參考下
    2022-12-12
  • elementui中使用el-tabs切換實(shí)時(shí)更新數(shù)據(jù)

    elementui中使用el-tabs切換實(shí)時(shí)更新數(shù)據(jù)

    這篇文章主要介紹了elementui中使用el-tabs切換實(shí)時(shí)更新數(shù)據(jù),具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • 又一款MVVM組件 Vue基礎(chǔ)語法和常用指令(1)

    又一款MVVM組件 Vue基礎(chǔ)語法和常用指令(1)

    這篇文章主要為大家分享了一款MVVM組件,詳細(xì)介紹了Vue基礎(chǔ)語法和常用指令,具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下
    2016-11-11
  • vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理

    vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理

    這篇文章主要介紹了vue使用formData時(shí)候傳遞參數(shù)是個(gè)空值的情況處理,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-05-05
  • Vue百度地圖實(shí)現(xiàn)定位和marker拖拽監(jiān)聽功能

    Vue百度地圖實(shí)現(xiàn)定位和marker拖拽監(jiān)聽功能

    這篇文章主要介紹了Vue百度地圖實(shí)現(xiàn)定位和marker拖拽監(jiān)聽功能,實(shí)現(xiàn)思路非常簡單,本文結(jié)合實(shí)例代碼效果圖給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下
    2022-11-11
  • ant-design-vue table分頁onShowSizeChange后的pageNo解決

    ant-design-vue table分頁onShowSizeChange后的pageNo解決

    這篇文章主要介紹了ant-design-vue table分頁onShowSizeChange后的pageNo的問題及解決,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2023-04-04
  • Vuex中如何getters動態(tài)獲取state的值

    Vuex中如何getters動態(tài)獲取state的值

    這篇文章主要介紹了Vuex中如何getters動態(tài)獲取state的值,具有很好的參考價(jià)值,希望對大家有所幫助。如有錯誤或未考慮完全的地方,望不吝賜教
    2022-08-08
  • vue3+ts代理的使用

    vue3+ts代理的使用

    本文主要介紹了vue3+ts代理的使用,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2023-12-12
  • Vue2.0系列之過濾器的使用

    Vue2.0系列之過濾器的使用

    這篇文章主要介紹了Vue2.0系列之過濾器的使用,小編覺得挺不錯的,現(xiàn)在分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧
    2018-03-03
  • Element Rate 評分的使用方法

    Element Rate 評分的使用方法

    這篇文章主要介紹了Element Rate 評分的使用方法,文中通過示例代碼介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧
    2020-07-07

最新評論