vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能
1、準(zhǔn)備工作
可以先看官方的介紹,JSAPI結(jié)合Vue使用,這個(gè)不需要在main.js中引入
2、index.html中
//如果只需要獲取經(jīng)緯度可以跳過(guò)這步,經(jīng)緯度逆解析為詳細(xì)地址時(shí)需要配置這個(gè) <script type="text/javascript"> window._AMapSecurityConfig = { securityJsCode: 'XXX', //所申請(qǐng)的安全密鑰 注意這是安全密鑰而不是key } </script>
3、index.vue的html部分
//我是封裝在antd的彈窗組件中 <template> <a-modal title="選擇區(qū)域" :visible="visible" @ok="handleOk" @cancel="handleCancel" okText="提交" cancelText="取消"> <div id="container"></div> <div class="toolbar"> 當(dāng)前坐標(biāo): {{ point[0] }}, {{ point[1] }} <br /> 地址: {{ address }} </div> </a-modal> </template>
4、index.vue的script部分
<script> import AMapLoader from '@amap/amap-jsapi-loader' export default { name: 'MapDialog', data() { return { visible: false, //彈窗顯隱 center: [115.97539, 28.715532], //地圖中心點(diǎn) point: [], //經(jīng)緯度-lng lat map: null, //地圖實(shí)例 marker: null, //地圖icon geocoder: null, //逆解析實(shí)例 address: null //地址 } }, methods: { // 打開(kāi)彈窗 open({ point, address }) { // 如果父組件攜帶了參數(shù) 賦值做回顯 if (point && address) { this.address = address this.point = point this.center = point } // 打開(kāi)彈窗 this.visible = true //地圖初始化 this.initMap() }, //DOM初始化完成進(jìn)行地圖初始化 initMap() { AMapLoader.load({ key: 'XXX', // 申請(qǐng)好的Web端開(kāi)發(fā)者Key,首次調(diào)用 load 時(shí)必填 version: '2.0', // 指定要加載的 JSAPI 的版本,缺省時(shí)默認(rèn)為 1.4.15 plugins: [ 'AMap.Geocoder', // 逆向地理解碼插件 'AMap.Marker' // 點(diǎn)標(biāo)記插件 ] // 需要使用的的插件列表 }) .then(AMap => { this.map = new AMap.Map('container', { //設(shè)置地圖容器id zoom: 12, //初始化地圖級(jí)別 center: this.center //初始化地圖中心點(diǎn)位置 }) // 如果父組件傳入了有效值 回顯一個(gè)icon if (this.point.length > 0) { this.addMarker() } //監(jiān)聽(tīng)用戶(hù)的點(diǎn)擊事件 this.map.on('click', e => { let lng = e.lnglat.lng let lat = e.lnglat.lat this.point = [lng, lat] // 增加點(diǎn)標(biāo)記 this.addMarker() // 獲取地址 this.getAddress() }) }) .catch(e => { console.log(e) }) }, // 增加點(diǎn)標(biāo)記 addMarker() { // 清除其他icon if (this.marker) { this.marker.setMap(null) this.marker = null } // 重新渲染icon this.marker = new AMap.Marker({ icon: '//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png', position: this.point, //icon經(jīng)緯度 offset: new AMap.Pixel(-13, -30) //icon中心點(diǎn)的偏移量 }) this.marker.setMap(this.map) //設(shè)置icon }, // 將經(jīng)緯度轉(zhuǎn)換為地址 getAddress() { const self = this // 這里通過(guò)高德 SDK 完成。 this.geocoder = new AMap.Geocoder({ city: '全國(guó)', //地理編碼時(shí),設(shè)置地址描述所在城市; 默認(rèn)全國(guó); 可選值:城市名(中文或中文全拼)、citycode、adcode radius: 1000, //逆地理編碼時(shí),以給定坐標(biāo)為中心點(diǎn); 默認(rèn)1000; 取值范圍(0-3000) extensions: 'all' //逆地理編碼時(shí),返回信息的詳略; 默認(rèn)值:base,返回基本地址信息; 默認(rèn)值:base,返回基本地址信息 }) //調(diào)用逆解析方法 個(gè)人開(kāi)發(fā)者調(diào)用量上限5000(次/日) this.geocoder.getAddress(this.point, function(status, result) { if (status === 'complete' && result.info === 'OK') { if (result && result.regeocode) { // this指向改變 self.address = result.regeocode.formattedAddress } } }) }, // 提交回調(diào) handleOk() { this.$emit('callback', { point: this.point, address: this.address }) this.map = null this.marker = null this.visible = false }, // 取消回調(diào) handleCancel() { this.map = null this.marker = null this.visible = false } } } </script>
5、index.vue的css部分
<style lang="less" scoped> /deep/ .ant-modal { width: 100vw !important; max-width: 100vw !important; top: 0; padding-bottom: 0; .ant-modal-body { height: calc(100vh - 55px - 53px); overflow-y: auto; padding: 0; .search-box { width: 100%; height: 150px; } #container { padding: 0px; margin: 0px; width: 100%; height: 100%; .amap-icon img, .amap-marker-content img { width: 25px; height: 34px; } } .toolbar { position: absolute; bottom: 50px; left: 0; width: 100%; height: 100px; background-color: #fff; font-size: 20px; text-align: center; line-height: 50px; } } } </style>
6、頁(yè)面效果
逆解析經(jīng)緯度得到的詳細(xì)地址
總結(jié)
到此這篇關(guān)于vue項(xiàng)目接入高德地圖點(diǎn)擊地圖獲取經(jīng)緯度以及省市區(qū)功能的文章就介紹到這了,更多相關(guān)vue點(diǎn)擊地圖獲取經(jīng)緯度省市區(qū)內(nèi)容請(qǐng)搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
vue3?typescript封裝axios過(guò)程示例
這篇文章主要為大家介紹了vue3?typescript封裝axios過(guò)程示例詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2022-10-10vue.js利用defineProperty實(shí)現(xiàn)數(shù)據(jù)的雙向綁定
本篇文章主要介紹了用Node.js當(dāng)作后臺(tái)、jQuery寫(xiě)前臺(tái)AJAX代碼實(shí)現(xiàn)用戶(hù)登錄和注冊(cè)的功能的相關(guān)知識(shí)。具有很好的參考價(jià)值。下面跟著小編一起來(lái)看下吧2017-04-04關(guān)于vue2強(qiáng)制刷新,解決頁(yè)面不會(huì)重新渲染的問(wèn)題
今天小編就為大家分享一篇關(guān)于vue2強(qiáng)制刷新,解決頁(yè)面不會(huì)重新渲染的問(wèn)題,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2019-10-10vue使用keep-alive如何實(shí)現(xiàn)多頁(yè)簽并支持強(qiáng)制刷新
這篇文章主要介紹了vue使用keep-alive如何實(shí)現(xiàn)多頁(yè)簽并支持強(qiáng)制刷新,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2022-08-08vue.js 1.x與2.0中js實(shí)時(shí)監(jiān)聽(tīng)input值的變化
這篇文章主要介紹了vue.js 1.x與vue.js2.0中js實(shí)時(shí)監(jiān)聽(tīng)input值的變化的相關(guān)資料,文中介紹的非常詳細(xì),對(duì)大家具有一定的參考價(jià)值,需要的朋友們下面來(lái)一起看看吧。2017-03-03vuejs 動(dòng)態(tài)添加input框的實(shí)例講解
今天小編就為大家分享一篇vuejs 動(dòng)態(tài)添加input框的實(shí)例講解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-08-08Django+vue跨域問(wèn)題解決的詳細(xì)步驟
這篇文章主要介紹了Django+vue跨域問(wèn)題解決的詳細(xì)步驟,文中通過(guò)示例代碼介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧2019-01-01