vue中使用heatmapjs的示例代碼(結(jié)合百度地圖)
業(yè)務(wù)需求:將heatmap引入頁(yè)面中,做成一個(gè)可引入的框架,使用于所有頁(yè)面。代碼如下。
<!DOCTYPE html> <html> <head> <meta charset="UTF-8"> <title></title> <style> .heatmap { width:1900px; height:1900px; } </style> <script src="js/heatmap.min.js"></script> <script src="js/jquery.js"></script> </head> <body> <input id=xxx type=hidden value=""> <input id=yyy type=hidden value=""> <input id="array" type="button" value="點(diǎn)擊查看數(shù)組內(nèi)容" onclick="getindex()"/> <div class="demo-wrapper"> <div class="heatmap" style="position: relative;"> <div><img src="image/1.jpg" ></div> </div> </div> </body> <script src="js/heatmap.min.js"></script> <script> <!--heapmap熱力圖--> var heatmapInstance = h337.create({ container: document.querySelector('.heatmap'), radius: 50 }); document.querySelector('.demo-wrapper').onmousemove = function(ev) { heatmapInstance.addData({ x: ev.layerX, y: ev.layerY, value: 1 }); }; <!--鼠標(biāo)點(diǎn)擊--> var pointx = new Array(); var pointy = new Array(); var i = 0;//數(shù)組下標(biāo) function mouseMove(ev) { Ev = ev || window.event; var mousePos = mouseCoords(ev); document.getElementById("xxx").value = mousePos.x; pointx[i] = document.getElementById("xxx").value ;//x坐標(biāo)值寫(xiě)入數(shù)組 console.log("x:"+document.getElementById("xxx").value); document.getElementById("yyy").value = mousePos.y; pointy[i] = document.getElementById("yyy").value;//y坐標(biāo)值寫(xiě)入數(shù)組 console.log("y:"+document.getElementById("yyy").value); // 執(zhí)行完之后數(shù)組下標(biāo)加一 i++; console.log(i);//打印下標(biāo) } function mouseCoords(ev) { if (ev.pageX || ev.pageY) { return {x: ev.pageX, y: ev.pageY}; } return { x: ev.clientX + document.body.scrollLeft - document.body.clientLeft, y: ev.clientY + document.body.scrollTop - document.body.clientTop }; } document.onmousemove = mouseMove; $(function(){ var s =""; s += window.screen.height+"*"+window.screen.width; console.log("當(dāng)前屏幕分辨率是:"+s); <!--動(dòng)態(tài)改變div寬高--> $(".heatmap").width($("body").width()); $(".heatmap").height($("body").height()); }); </script> </html>
需要引入的js在https://github.com/pa7/heatmap.js 獲取。
vue中使用heatmapjs
百度地圖怎么使用就不說(shuō)了,主要講講這個(gè)heatmap,直接貼代碼了,注釋挺詳細(xì)的
//vue組件中 data(){ return{ heatmapOverlay:"" } }, mounted() { //引用heatmap.js //你也可以在index.html中直接插個(gè) <script type="text/javascript" src="http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"></script> let script = document.createElement("script"); script.type = "text/javascript"; script.src = "http://api.map.baidu.com/library/Heatmap/2.0/src/Heatmap_min.js"; document.body.appendChild(script); //創(chuàng)建地圖,這個(gè)寫(xiě)自己的創(chuàng)建地圖方法,請(qǐng)確認(rèn)你的地圖能加載出來(lái) this.creatMap(); //一定要先讓地圖加載出來(lái)才加載熱力圖,我這里做演示直接寫(xiě)個(gè)setTimeout了 setTimeout(()=>{this.getHeatmap()},2000) }, methods:{ getHeatmap() { //請(qǐng)求雷達(dá)數(shù)據(jù),雷達(dá)數(shù)據(jù)需要lng,lat,count 三種數(shù)據(jù) this.$http .get("../../../static/radar20.json") .then(res => { if (res.data.code == "success") { console.log("獲取radar成功"); var bigdata = res.data.data; //關(guān)鍵是下面的三行 //其中map是new BMap.Map()創(chuàng)建的地圖實(shí)例,其他的熱力圖屬性(radius,opacity這種)可以在百度地圖接口實(shí)例中調(diào)試體驗(yàn),http://lbsyun.baidu.com/jsdemo.htm#c1_15 this.heatmapOverlay = new BMapLib.HeatmapOverlay({ radius: 40,opacity:0.4}); map.addOverlay(this.heatmapOverlay); this.heatmapOverlay.setDataSet({ data: bigdata, max: 20 }); } else { console.log("err"); } }) .catch(err => { console.log(err); }); }, }
效果圖:
以上就是本文的全部?jī)?nèi)容,希望對(duì)大家的學(xué)習(xí)有所幫助,也希望大家多多支持腳本之家。
相關(guān)文章
Vue?props傳入function時(shí)的this指向問(wèn)題解讀
這篇文章主要介紹了Vue?props傳入function時(shí)的this指向問(wèn)題解讀,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。如有錯(cuò)誤或未考慮完全的地方,望不吝賜教2023-01-01vue3.0引入百度地圖并標(biāo)記點(diǎn)的實(shí)現(xiàn)代碼
這篇文章主要介紹了vue3.0引入百度地圖并標(biāo)記點(diǎn),本文通過(guò)實(shí)例代碼給大家介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或工作具有一定的參考借鑒價(jià)值,需要的朋友可以參考下2022-08-08Vuex實(shí)現(xiàn)計(jì)數(shù)器以及列表展示效果
這篇文章主要為大家詳細(xì)介紹了Vuex實(shí)現(xiàn)計(jì)數(shù)器以及列表展示效果,文中示例代碼介紹的非常詳細(xì),具有一定的參考價(jià)值,感興趣的小伙伴們可以參考一下2018-03-03vue-vuex中使用commit提交mutation來(lái)修改state的方法詳解
今天小編就為大家分享一篇vue-vuex中使用commit提交mutation來(lái)修改state的方法詳解,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2018-09-09vue打包后出現(xiàn)空白頁(yè)的原因及解決方式詳解
在項(xiàng)目中很多時(shí)候需要用到vue打包成html不需要放在服務(wù)器上就能瀏覽,根據(jù)官網(wǎng)打包出來(lái)的html直接打開(kāi)是顯示空白,下面這篇文章主要給大家介紹了關(guān)于vue打包后出現(xiàn)空白頁(yè)的原因及解決方式的相關(guān)資料,需要的朋友可以參考下2022-07-07vue前端實(shí)現(xiàn)導(dǎo)出頁(yè)面為pdf(分頁(yè)導(dǎo)出、不分頁(yè)導(dǎo)出及分模塊導(dǎo)出)
在實(shí)際應(yīng)用中可能用戶希望將系統(tǒng)中一個(gè)頁(yè)面展示的所有數(shù)據(jù)報(bào)表,用PDF的文件格式下載下來(lái),以便于其他用途,這篇文章主要給大家介紹了關(guān)于vue前端實(shí)現(xiàn)導(dǎo)出頁(yè)面為pdf(分頁(yè)導(dǎo)出、不分頁(yè)導(dǎo)出及分模塊導(dǎo)出)的相關(guān)資料,需要的朋友可以參考下2024-06-06