UNiAPP中如何使用render.js繪制高德地圖
什么是render.js
renderjs
是一個(gè)運(yùn)行在視圖層的js。它比WXS更加強(qiáng)大。它只支持app-vue和h5。 renderjs
的主要作用有2個(gè):
- 大幅降低邏輯層和視圖層的通訊損耗,提供高性能視圖交互能力
- 在視圖層操作dom,運(yùn)行for web的js庫
使用方式
設(shè)置 script 節(jié)點(diǎn)的 lang 為 renderjs
<view id="test"></view> <script module="test" lang="renderjs"> export default { mounted() { // ... }, methods: { // ... } } </script>
在uniAPP中使用render.js 繪制高德地圖
明確需求
1. 在uni中的vue文件下使用地圖 2. 需要在地圖根據(jù)經(jīng)緯度標(biāo)記點(diǎn),并且可點(diǎn)擊 3. 需要在標(biāo)記點(diǎn)與點(diǎn)之間連線 4. 地圖上需要懸浮兩個(gè)按鈕
解決思路
uni自帶的有map組件,功能還是比較強(qiáng)大,但是在vue文件下很多功能受限,必須在nvue文件中才能發(fā)揮出功能。
在本次編寫中因?yàn)槠渌驘o法使用nvue文件,所以不得不想其他方法,以及在地圖上懸浮按鈕,解決層級問題也是一個(gè)難點(diǎn),所以放棄了uni的map組件。
最后多次嘗試后,選擇了使用render.js 來調(diào)用高德地圖,能夠完美解決上述需求和問題,特此記錄與分享。
編寫代碼
vue頁面使用render.js
render.js 主要是通過script標(biāo)簽引入,如下圖所示:
view就是一個(gè)render.js的容器,用于地圖展示,然后在script標(biāo)簽里面編寫地圖的引入與初始化代碼
初始化地圖
data(){ map:null, myData:[], }, //以下是寫在methods中 //引入高德地圖SDK init(){ if (typeof window.AMap === 'function') { this.initAmap() } else { // 動態(tài)引入較大類庫避免影響頁面展示 const script = document.createElement('script') script.src = 'https://webapi.amap.com/maps?v=1.4.15&key=' + '你的key' script.onload = this.initAmap.bind(this) document.head.appendChild(script) console.log('eles'); } }, //初始化地圖 initAmap() { this.map = new AMap.Map('amap', { resizeEnable: true, center: [this.myData[0].longitude,this.myData[0].latitude], zooms: [4, 20], //設(shè)置地圖級別范圍 zoom: 18 }) this.map.on('complete',()=>{ console.log('加載完成'); }) this.getItem(this.myData) }, // 給地圖繪制點(diǎn) Makers addMaker(item){ let marker = new AMap.Marker({ //經(jīng)緯度位置 position: new AMap.LngLat(item.longitude, item.latitude), //便宜量 offset: new AMap.Pixel(-10, -24), //圖標(biāo) icon: new AMap.Icon({ //大小 size: new AMap.Size(20, 25), imageSize: new AMap.Size(20, 25), image:'imgpath' }), //圖標(biāo)展示層級,防止被隱藏時(shí)編寫 zIndex:100, //圖標(biāo)旁邊展示內(nèi)容 label:{ content:`<view>content</view>`, offset: new AMap.Pixel(10, -18) } }) //給圖標(biāo)添加點(diǎn)擊事件 marker.on('click', (e) => { console.log(item,e); }) //將圖標(biāo)添加到地圖中 this.map.add(marker) }, //繪制點(diǎn)與點(diǎn)之間的線段 Polyline類 initLine(start,end){ let polyline = new AMap.Polyline({ //線段粗細(xì) strokeWeight:5, //顏色 strokeColor:'#007AFF', //形狀 lineCap:'round', lineJoin:'round', //是否顯示方向 showDir:true, //起點(diǎn)與終點(diǎn)經(jīng)緯度[[longitudeStart,latitudeStart],[longitudeEnd,latitudeEnd]] path:[start,end] }) //向地鐵圖添加線段 this.map.add(polyline) },
實(shí)現(xiàn)效果
關(guān)于高德地圖的具體使用請參考高德API
lbs.amap.com/api/javascr…
render.js中的通信
render.js 所在的script標(biāo)簽和vue頁面的script標(biāo)簽是無法使用this進(jìn)行數(shù)據(jù)通信的,必須通過其他方式進(jìn)行通信,類似于vue中的組件傳值。
1、數(shù)據(jù)的綁定
2、數(shù)據(jù)的接收
3、render.js中向vue頁面發(fā)送數(shù)據(jù)
原理和上面差不多 在render.js中,拋出一個(gè)方法,然后在頁面中編寫該方法監(jiān)聽,具體如下
//render.js //向vue頁面拋出數(shù)據(jù) sendMsg(){ this.$ownerInstance.callMethod('reciveMsg', '我是render.js的數(shù)據(jù)') } //針對頁面點(diǎn)擊或直接調(diào)用 sendMsg2(e,ownerInstance){ ownerInstance.callMethod('reciveMsg', '我是render.js的數(shù)據(jù)') }
//vue頁面接收數(shù)據(jù) reciveMsg(data){ console.log(data) //我是render.js的數(shù)據(jù) } 復(fù)制代碼
總結(jié)
render.js主要用于對DOM操作很頻繁的情況,如echarts的使用,地圖的使用等。
最后附上UNI官方鏈接和高德API鏈接
uniapp.dcloud.io/frame?id=re…
到此這篇關(guān)于UNiAPP中如何使用render.js繪制高德地圖的文章就介紹到這了,更多相關(guān)render.js高德地圖內(nèi)容請搜索腳本之家以前的文章或繼續(xù)瀏覽下面的相關(guān)文章希望大家以后多多支持腳本之家!
相關(guān)文章
在Webpack中用url-loader處理圖片和字體的問題
這篇文章主要介紹了在Webpack中用url-loader處理圖片和字體的問題及解決方法,本文給大家介紹的非常詳細(xì),對大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2020-04-04JS中showModalDialog關(guān)閉子窗口刷新主窗口用法詳解
這篇文章主要介紹了JS中showModalDialog關(guān)閉子窗口刷新主窗口用法,結(jié)合具體實(shí)例形式較為詳細(xì)的分析了showModalDialog常見用法與相關(guān)使用技巧,需要的朋友可以參考下2017-03-03整理一些JavaScript的IE和火狐的兼容性注意事項(xiàng)
整理一些JavaScript的IE和火狐的兼容性解決方法,有更好的方法多多交流2011-03-03模擬javascript中的sort排序(簡單實(shí)例)
下面小編就為大家?guī)硪黄Mjavascript中的sort排序(簡單實(shí)例)。小編覺得挺不錯(cuò)的,現(xiàn)在就分享給大家,也給大家做個(gè)參考。一起跟隨小編過來看看吧2016-08-08深入理解JavaScript系列(46):代碼復(fù)用模式(推薦篇)詳解
這篇文章主要介紹了深入理解JavaScript系列(46):代碼復(fù)用模式(推薦篇)詳解,本文講解了原型繼承、復(fù)制所有屬性進(jìn)行繼承、混合(mix-in)、借用方法等模式,需要的朋友可以參考下2015-03-03(推薦一個(gè)超好的JS函數(shù)庫)S.Sams Lifexperience ScriptClassLib
(推薦一個(gè)超好的JS函數(shù)庫)S.Sams Lifexperience ScriptClassLib...2007-04-04