Vue使用高德地圖搭建實(shí)時(shí)公交應(yīng)用功能(地圖 + 附近站點(diǎn)+線路詳情 + 輸入提示+換乘詳情)
最近項(xiàng)目要使用高德地圖寫了一個(gè)實(shí)時(shí)公交的應(yīng)用,這邊分享一個(gè)小應(yīng)用主要熟悉下高德地圖在vue中的使用,常用api,vue的常用指令
先給大家看下頁面效果:
如果有需要源碼的童鞋請移步我的github地址 vue搭建實(shí)時(shí)公交 (歡迎star)
實(shí)現(xiàn)思路
在vue項(xiàng)目中導(dǎo)入高德地圖 具體功能調(diào)用相應(yīng)高德js APi
1.在vue項(xiàng)目中導(dǎo)入高德地圖
1.修改webpac.base.conf.js文件
externals: { 'AMap': 'AMap' }
2.引入sdk 引入有兩種方式,一種是在index頁面直接引入
<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=您申請的key值"></script>
還有一種是異步加載
<script src="http://webapi.amap.com/maps?v=1.3&key=您申請的key值&callback=init"></script> <script> function init(){ var map = new AMap.Map('container', { center: [117.000923, 36.675807], zoom: 6 }); map.plugin(["AMap.ToolBar"], function() { map.addControl(new AMap.ToolBar()); }); } </script>
注意不管是采用哪種方式,都要保證你想要加載地圖的js文件,在引入的sdk之后。這樣,在第三步的時(shí)候,才不會(huì)報(bào)錯(cuò)
3.在當(dāng)前需要加載vue頁面引入
import AMap from 'AMap'
原文鏈接: http://www.dbjr.com.cn/article/112413.htm
2.附近站點(diǎn)功能
AMap.service(['AMap.PlaceSearch'], function () { var placeSearch = new AMap.PlaceSearch({ // 構(gòu)造地點(diǎn)查詢類 pageSize: 4, typ: '', pageIndex: 1, city: '蘇州' // 城市 }) // 中心點(diǎn)坐標(biāo) // [currentLocation.lng,currentLocation.lat] // 120.6400961887,31.1411187922 var currentLocation = true if (currentLocation !== undefined) { placeSearch.searchNearBy('公交站點(diǎn)', [120.6400961887, 31.1411187922], 1500, function (status, result) { if (status === 'complete' && result.info === 'OK') { var pois = result.poiList.pois var random = [4, 4, 24, 14] pois.forEach((item, index) => { this.items.push({ site: item.name.substr(0, item.name.indexOf('(')), line: item.address, distance: item.distance, next_site: '', sitenum: random[index], siteId: item.id }) this.lineInfo(item.address.substr(0, item.address.indexOf(';') - 1), item.id, index) }) console.log(result.poiList) } }.bind(this)) } }.bind(this))
這邊主要調(diào)用高德周邊搜索API,構(gòu)造地點(diǎn)查詢類tye 設(shè)為空,采用公交站點(diǎn)為關(guān)鍵字進(jìn)行查詢,這邊中心點(diǎn)坐標(biāo)是寫死的,各位小伙伴可以調(diào)用高德定位api去獲得當(dāng)前坐標(biāo)
3.線路實(shí)時(shí)詳情
AMap.service(['AMap.LineSearch'], function () { var linesearch = new AMap.LineSearch({ pageIndex: 1, city: this.city, pageSize: 20, extensions: 'all' // 返回全部信息 }) linesearch.search(this.lineName, function (status, result) { // 取回公交路線查詢結(jié)果 if (status === 'complete' && result.info === 'OK') { this.lineInfo = result.lineInfo var tips = result.lineInfo[0] console.log(tips) this.from = tips.start_stop + '-' this.to = tips.end_stop this.lineId = tips.id if (tips.stime.length !== 0 && tips.length !== 0) { this.time_s = tips.stime.substr(0, 2) + ':' + tips.stime.substr(2, 2) this.time_e = tips.etime.substr(0, 2) + ':' + tips.etime.substr(2, 2) } else { this.time_s = '05:40' this.time_e = '18:40' } this.pay = tips.basic_price this.listWidth = tips.via_stops.length this.backImage.width = tips.via_stops.length + 'rem' tips.via_stops.forEach((item, index) => { if (item.id === this.siteId) { this.ind = index console.log(index) this.showActive(this.ind, this.siteName) } this.siteList.push({ siteName: item.name, siteLat: item.location.lat, siteLng: item.location.lng }) }) } else { // 無數(shù)據(jù)或者查詢失敗 } // setInterval(busposition(), 60000) }.bind(this)) }.bind(this)) },
這邊調(diào)用公交路線查詢接口,查詢相關(guān)路線詳情,這邊小車車的位置是一個(gè)寫死數(shù)組(實(shí)際情況可以根據(jù)班車GPS坐標(biāo)判斷班車在哪兩個(gè)站點(diǎn)之間),可以點(diǎn)擊相應(yīng)站點(diǎn)顯示最近班車相聚站點(diǎn)數(shù)
4.輸入提示
this.autocomplete.search(this.keywords, function (status, result) { if (status === 'complete' && result.info === 'OK') { var tips = result.tips this.hisTips = [] console.log('tips', tips) for (var i = 0; i < tips.length; i++) { if (tips[i].location !== '' && undefined !== tips[i].location && tips[i].district.substr(0, 6) === '江蘇省蘇州市') { this.hisTips.push({ lng: tips[i].location.lng, lat: tips[i].location.lat, name: tips[i].name, district: tips[i].district }) } } } else { } }.bind(this))
這邊使用指令v-on:input調(diào)用我們輸入提示的方法進(jìn)行列表展示
5.換乘詳情
AMap.service('AMap.Transfer', function () { // 回調(diào)函數(shù) // 實(shí)例化Transfer var transptions = { policy: 0, // 乘車策略,少時(shí)間0 少步行3 最少換乘2 city: '蘇州' // 城市 } this.transfer = new AMap.Transfer(transptions) this.Linesearch() }.bind(this)) this.transfer.search([this.$route.query.fromAddressLng, this.$route.query.fromAddressLat], [this.$route.query.toAddressLng, this.$route.query.toAddressLat], function (status, result) { console.log(status) console.log(result) if (status === 'complete' && result.info === 'OK') { var plans = result.plans console.log('plans', plans) for (var i = 0; i < plans.length; i++) { var cost = plans[i].cost var time = parseInt(plans[i].time / 60) var segments = plans[i].segments var trans = [] if (segments !== '' && segments !== undefined) { for (var j = 0; j < segments.length; j++) { if (segments[j].transit_mode === 'BUS') { const linename = segments[j].instruction trans.push(linename.substr(2, linename.indexOf('(') - 2)) } else if (segments[j].transit_mode === 'SUBWAY') { const linename = segments[j].instruction trans.push(linename.substr(2, linename.indexOf('線') - 2)) } } } this.plan.push({ cost: cost, time: time, trans: trans }) } } else { } }.bind(this))
這里我們調(diào)用transfer.search()
傳入起點(diǎn)和終點(diǎn)坐標(biāo),是通過輸入提示獲得的, 把得到結(jié)果進(jìn)行列表展示
總結(jié)
以上所述是小編給大家介紹的Vue使用高德地圖搭建實(shí)時(shí)公交應(yīng)用功能(地圖 + 附近站點(diǎn)+線路詳情 + 輸入提示+換乘詳情),希望對大家有所幫助,如果大家有任何疑問請給我留言,小編會(huì)及時(shí)回復(fù)大家的。在此也非常感謝大家對腳本之家網(wǎng)站的支持!
相關(guān)文章
iview table高度動(dòng)態(tài)設(shè)置方法
下面小編就為大家分享一篇iview table高度動(dòng)態(tài)設(shè)置方法,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2018-03-03Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼
Vue3的Hooks是一種新的 API,本文主要介紹了Vue3+Hooks實(shí)現(xiàn)4位隨機(jī)數(shù)和60秒倒計(jì)時(shí)的示例代碼,具有一定的參考價(jià)值,感興趣的可以了解一下2024-04-04vue使用neovis操作neo4j圖形數(shù)據(jù)庫及優(yōu)缺點(diǎn)
這篇文章主要介紹了vue使用neovis操作neo4j圖形數(shù)據(jù)庫,本文給大家介紹了與常規(guī)做法的優(yōu)缺點(diǎn)對比及使用技巧,對vue?neo4j圖形數(shù)據(jù)庫相關(guān)知識(shí)感興趣的朋友一起看看吧2022-02-02使用Vue父子組件通信實(shí)現(xiàn)todolist的功能示例代碼
這篇文章主要給大家介紹了關(guān)于如何使用Vue父子組件通信實(shí)現(xiàn)todolist的功能的相關(guān)資料,文中通過示例代碼介紹的非常詳細(xì),對大家學(xué)習(xí)或者使用Vue具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面來一起學(xué)習(xí)學(xué)習(xí)吧2019-04-04ElementUI的this.$notify.close()調(diào)用不起作用的解決
本文主要介紹了ElementUI的this.$notify.close()調(diào)用不起作用的解決,文中通過示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2021-08-08Vue拖動(dòng)截圖功能的簡單實(shí)現(xiàn)方法
最近項(xiàng)目上要做一個(gè)車牌識(shí)別的功能,就需要做拖動(dòng)截圖功能了,因?yàn)榍岸问莢ue,所以下面這篇文章主要給大家介紹了關(guān)于Vue拖動(dòng)截圖功能的簡單實(shí)現(xiàn)方法,需要的朋友可以參考下2021-07-07