vue2.0中使用百度BMapGL的画面以及切换3d棱柱

本文主要是介绍vue2.0中使用百度BMapGL的画面以及切换3d棱柱,希望对大家解决编程问题提供一定的参考价值,需要的开发者们随着小编来一起学习吧!

  1.使用百度地图首先得申请他的ak去 地址:登录百度帐号 记得选择浏览器端

 2.下面咱们开始代码方面,首先在index.html中引入百度api

<!DOCTYPE html>
<html><head><meta charset="utf-8"><meta name="viewport" content="width=device-width,initial-scale=1.0"><title>baidu-map</title><script type="text/javascript" src="http://api.map.baidu.com/api?type=webgl&v=1.0&ak=你的密钥"></script><!-- 去掉百度地图logo --><style type="text/css">.BMap_cpyCtrl {display: none;}</style><style type="text/css">.anchorBL{display:none;}</style></head><body><div id="app"></div><!-- built files will be auto injected --></body>
</html>

3.然后开始在页面中实例地图 具体代码中注释很详细 有问题评论区提问哈

<template><div class="bai-du-map"><div id="map" /><div class="item-city"><el-select v-model="value" filterable placeholder="请选择城市" @change="cityChange"><el-optionv-for="item in options":key="item.value":label="item.label":value="item.value"/></el-select></div><div class="on-off"><el-switchv-model="switchVal"style="color:#fff;"inactive-text="开启3d画面"active-color="#13ce66"inactive-color="#CDCFD0"@change="switchChange"/><el-button type="danger" round size="mini" @click="removeDrawing">清空图层</el-button></div></div>
</template>
<script>
export default {name: 'Baidumap',data() {return {map: null, // 实例地图polygon: null, // 实例画面mapStyle: [], // 地图样式 这里的样式让我删了太长了 自行下载去吧https://lbsyun.baidu.com/customv2/help.htmloptions: [], // 城市列表value: '', // 选中的城市idswitchVal: false, // 是否开启3d画面cityName: '', // 选中城市名称marker: null// 打点实例}},mounted() {this.mapBox()this.cityList()},methods: {/*** @description:初始化百度地图* @param {*}* @return {*}*/mapBox() {// eslint-disable-next-line no-undefthis.map = new BMapGL.Map('map') // 创建Map实例// eslint-disable-next-line no-undefthis.map.centerAndZoom(new BMapGL.Point(116.28019, 40.049191), 8) // 初始化地图,设置中心点坐标和地图级别this.map.enableScrollWheelZoom(true) // 开启鼠标滚轮缩放this.map.setHeading(0) // 视觉方向this.map.setTilt(55) // 视觉角度this.map.setMapStyleV2({ styleJson: this.mapStyle }) // 地图样式},/*** @description: 清除图层按钮* @param {*}* @return {*}*/removeDrawing() {this.value = ''this.map.clearOverlays()// 清除图层},/*** @description:开关的回调函数  开关选择监听判断2d和3d* @param {*} d 布尔值* @return {*}*/switchChange(d) {if (d) {this.drawSurface3D(this.cityName)} else {this.drawSurface2D(this.cityName)}},/*** @description:选框选择城市地图变化* @param {*}* @return {*}*/cityChange(val) {if (val) {let obj = {}obj = this.options.find(item => {return item.value === val // 筛选出匹配数据})this.cityName = obj.labelif (this.switchVal) {this.drawSurface3D(obj.label)} else {this.drawSurface2D(obj.label)}} else {this.drawSurface2D('中国')}},/*** @description: 城市列表请求* @param {*}* @return {*}*/cityList() {this.$http.getFun('../../../static/config/json/cityname.json').then((res) => {if (res.status === 200) {this.options = res.data} else {this.$message({showClose: true,message: res.statusText,type: 'error'})}})},/*** @description: 2d画区图* @param {*} val 传入城市的名称 也可以是具体某个区* @return {*}*/drawSurface2D(val) {// 判断val是否有值 空不执行方法if (val === '') {return} else {this.map.clearOverlays()// 清除图层不然图层会叠加// eslint-disable-next-line no-undefvar bd = new BMapGL.Boundary()bd.get(val, (res) => {// eslint-disable-next-line no-undefvar hole = new BMapGL.Polygon(res.boundaries, {strokeColor: '#02b5ec',strokeWeight: 2,strokeOpacity: 0.5,fillColor: '#0058c6',fillOpacity: 0.7})this.map.addOverlay(hole)})// 下面是加载自己画的json// this.map.clearOverlays()// 清楚图层不然图层会叠加// // eslint-disable-next-line no-undef// this.$http.getFun(`../../../static/config/json/${val}.json`).then((res) => {//   const features = res.features//   features.forEach((feature) => {//     var coords = feature.geometry.coordinates//     for (let i = 0; i < coords.length; i++) {//       for (let j = 0; j < coords[i].length; j++) {//         var pointArr = []//         for (let k = 0; k < coords[i][j].length; k++) {//           var xy = coords[i][j][k]//           // eslint-disable-next-line no-undef//           pointArr.push(new BMapGL.Point(xy[0], xy[1]))//         }//         // eslint-disable-next-line no-undef//         this.polygon = new BMapGL.Polygon(pointArr, {//           strokeColor: '#02b5ec',//           strokeWeight: 2,//           strokeOpacity: 0.5,//           fillColor: '#0058c6',//           fillOpacity: 0.7//         })//         this.map.addOverlay(this.polygon)//       }//     }//   })// })}},/*** @description: 3d画区图* @param {*} val 传入城市的名称* @return {*}*/drawSurface3D(val) {if (val === '') {return} else {this.map.clearOverlays()// 清除图层不然图层会叠加// eslint-disable-next-line no-undefvar dq = new BMapGL.Boundary()dq.get(val, (rs) => {var count = rs.boundaries.length // 行政区域的点有多少个for (var i = 0; i < count; i++) {var path = []const str = rs.boundaries[i].replace(' ', '')const points = str.split(';')for (var j = 0; j < points.length; j++) {var lng = points[j].split(',')[0]var lat = points[j].split(',')[1]// eslint-disable-next-line no-undefpath.push(new BMapGL.Point(lng, lat))}// eslint-disable-next-line no-undefvar prism = new BMapGL.Prism(path, 6000, {topFillColor: '#02b5ec',topFillOpacity: 0.5,sideFillColor: '#0058c6',sideFillOpacity: 0.9})this.map.addOverlay(prism)}})}}}
}
</script>
<style lang="scss" scoped>
.bai-du-map {position:relative;overflow: hidden;#map {@include computed_W(1920);@include computed_H(1080);}.item-city {z-index: 999;position:absolute;top:50px;left: 50px;}.on-off {z-index: 999;position:absolute;top:20px;left: 50px;/deep/ .el-switch__label--left{color: #fff;}}
}
</style>

具体也也可以自行去看百度的api文档也比较具体 jspopularGL | 百度地图API SDK,

这篇关于vue2.0中使用百度BMapGL的画面以及切换3d棱柱的文章就介绍到这儿,希望我们推荐的文章对编程师们有所帮助!



http://www.chinasem.cn/article/663924

相关文章

使用Python创建一个功能完整的Windows风格计算器程序

《使用Python创建一个功能完整的Windows风格计算器程序》:本文主要介绍如何使用Python和Tkinter创建一个功能完整的Windows风格计算器程序,包括基本运算、高级科学计算(如三... 目录python实现Windows系统计算器程序(含高级功能)1. 使用Tkinter实现基础计算器2.

在.NET平台使用C#为PDF添加各种类型的表单域的方法

《在.NET平台使用C#为PDF添加各种类型的表单域的方法》在日常办公系统开发中,涉及PDF处理相关的开发时,生成可填写的PDF表单是一种常见需求,与静态PDF不同,带有**表单域的文档支持用户直接在... 目录引言使用 PdfTextBoxField 添加文本输入域使用 PdfComboBoxField

Git可视化管理工具(SourceTree)使用操作大全经典

《Git可视化管理工具(SourceTree)使用操作大全经典》本文详细介绍了SourceTree作为Git可视化管理工具的常用操作,包括连接远程仓库、添加SSH密钥、克隆仓库、设置默认项目目录、代码... 目录前言:连接Gitee or github,获取代码:在SourceTree中添加SSH密钥:Cl

Python中模块graphviz使用入门

《Python中模块graphviz使用入门》graphviz是一个用于创建和操作图形的Python库,本文主要介绍了Python中模块graphviz使用入门,具有一定的参考价值,感兴趣的可以了解一... 目录1.安装2. 基本用法2.1 输出图像格式2.2 图像style设置2.3 属性2.4 子图和聚

windows和Linux使用命令行计算文件的MD5值

《windows和Linux使用命令行计算文件的MD5值》在Windows和Linux系统中,您可以使用命令行(终端或命令提示符)来计算文件的MD5值,文章介绍了在Windows和Linux/macO... 目录在Windows上:在linux或MACOS上:总结在Windows上:可以使用certuti

CentOS和Ubuntu系统使用shell脚本创建用户和设置密码

《CentOS和Ubuntu系统使用shell脚本创建用户和设置密码》在Linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设置密码,本文写了一个shell... 在linux系统中,你可以使用useradd命令来创建新用户,使用echo和chpasswd命令来设

Python使用Matplotlib绘制3D曲面图详解

《Python使用Matplotlib绘制3D曲面图详解》:本文主要介绍Python使用Matplotlib绘制3D曲面图,在Python中,使用Matplotlib库绘制3D曲面图可以通过mpl... 目录准备工作绘制简单的 3D 曲面图绘制 3D 曲面图添加线框和透明度控制图形视角Matplotlib

Pandas中统计汇总可视化函数plot()的使用

《Pandas中统计汇总可视化函数plot()的使用》Pandas提供了许多强大的数据处理和分析功能,其中plot()函数就是其可视化功能的一个重要组成部分,本文主要介绍了Pandas中统计汇总可视化... 目录一、plot()函数简介二、plot()函数的基本用法三、plot()函数的参数详解四、使用pl

使用Python实现IP地址和端口状态检测与监控

《使用Python实现IP地址和端口状态检测与监控》在网络运维和服务器管理中,IP地址和端口的可用性监控是保障业务连续性的基础需求,本文将带你用Python从零打造一个高可用IP监控系统,感兴趣的小伙... 目录概述:为什么需要IP监控系统使用步骤说明1. 环境准备2. 系统部署3. 核心功能配置系统效果展

使用Java将各种数据写入Excel表格的操作示例

《使用Java将各种数据写入Excel表格的操作示例》在数据处理与管理领域,Excel凭借其强大的功能和广泛的应用,成为了数据存储与展示的重要工具,在Java开发过程中,常常需要将不同类型的数据,本文... 目录前言安装免费Java库1. 写入文本、或数值到 Excel单元格2. 写入数组到 Excel表格