百度 html 3d,百度地图实现3D棱柱

如图:

32bf670d5932

lz.png

1.项目根目录下下载百度地图插件

npm install vue-baidu-map –save

2.在首页index.html中引入百度地图:

3.在显示地图的组件中 template 中:

4.在显示地图的组件中 script 中:// path , // path array 这里是3D棱柱的重要部分

import {addPrisms} from '../../statics/js/map3D' // 这里是将构成3D棱柱的方法单独放在一个js文件中了,js见下文

import styleJson2 from '../../statics/js/styleJson2' // 此处引入的地图的样式(不重要,可以忽略)

export default {

data () {

return {

value: true,

val: true,

mode: true,

map: '',

polyline: '',

polylines: '',

polylinein: '',

items: [],

item: {},

item1: [],

item2: [],

item3: [],

log: '',

lat: '',

id: '',

}

},

mounted () {

// 百度地图API功能

this.maps()

},

methods: {

maps () {

// 百度地图API功能

this.map = new BMapGL.Map('container', {

style: {

// 地图样式

styleJson: styleJson2

}

});

this.map.enableScrollWheelZoom(true);

this.map.setTilt(50);

this.$api({

method: "get",

url: `/analysis/bireport/${this.id}`,

headers: { tenant: 'HO', 'token': localStorage.getItem("token")}

})

.then(res => {

this.item = res.data.data.reoprtFlight

this.items = []

this.item1 = []

this.item2 = []

// 经纬度

var lonandlat = res.data.data.reoprtFlight.lonandlat

var planLonandlat = res.data.data.reoprtFlight.planLonandlat

var actualDirectLocus = res.data.data.reoprtFlight.actualDirectLocus

// 转化为JSON格式(因为后端传过来的数据格式不能直接用,所以需要转换格式)

this.items = JSON.parse(lonandlat)

for (var i = 0; i < this.items.length; i++) {

this.log = this.items[0].longitude

this.lat = this.items[0].latitude

}

// 设置地图中心点

this.map.centerAndZoom(new BMapGL.Point(this.log, this.lat), 7);

// 计划时长

this.item1 = JSON.parse(planLonandlat)

this.item3 = JSON.parse(actualDirectLocus)

var pois = []

for (var i = 0; i < this.item3.length; i++) {

const start = {

longitude: this.item3[i].longitudeStart,

latitude: this.item3[i].latitudeStart,

pointname: this.item3[i].pointnameStart

}

const end = {

longitude: this.item3[i].longitudeEnd,

latitude: this.item3[i].latitudeEnd,

pointname: this.item3[i].pointnameEnd

}

pois.push(start, end);

}

// path

this.add = addPrisms(this.map, this.items);

this.first = addPrisms(this.map, this.item1, {prism:{topFillColor:'#4f77e1',sideFillColor:'#4f77e1'}});

this.item2 = [pois]

// path array

this.dest = []

this.item2.forEach(path => {

const prisms = addPrisms(this.map, path, {prism:{topFillColor:'#feb837',sideFillColor:'#feb837'}});

this.dest.push(...prisms)

})

})

.catch(function(error) {

console.log(error)

})

},

PS:上文中 this.item3和pois的数据格式

32bf670d5932

js.png

使用按钮控制3D棱柱的显示与隐藏

template:

计划航路

v-model="value"

color="green"

@input="clicBox(value)"

/>

实际直飞

v-model="val"

color="green"

@input="practical(val)"

/>

实际航路

v-model="mode"

color="green"

@input="fires(mode)"

/>

script部分

clicBox(value){

this.add.forEach(a => a[value ? 'show' : 'hide']())

},

practical(val){

this.dest.forEach(c => c[val ? 'show' : 'hide']())

},

fires(mode){

this.first.forEach(b => b[mode ? 'show' : 'hide']())

},

}

}

map3D.js

export {addPrisms}

function items2path(items) {

return (() => {

const __arr = [];

for (let __i = 0; __i < items.length; __i++) {

let item = items[__i];

__arr.push([ item.longitude, item.latitude ]);

}

return __arr;

})();

}

function addPrisms(map, items, op= {}) {

const prisms = items2prisms(items, op);

for (let __i = 0; __i < prisms.length; __i++) {

let prism = prisms[__i];

map.addOverlay(prism);

}

return prisms

}

function items2prisms(items, op) {

const path = items2path(items, op);

const prisms = (() => {

const __arr = [];

for (let i = 1; i < path.length; i++) {

let p = path[i];

__arr.push(path2prism(path[i - 1], p, op));

}

return __arr;

})();

return prisms;

}

function path2prism(p1, p2, op = {}) {

const path = path2prismPath(p1, p2);

const points = path2points(path);

const prism = new BMapGL.Prism(points, 50000, {

topFillColor: '#2cc78e',

topFillOpacity: 0.6,

sideFillColor: '#2cc78e',

sideFillOpacity: 0.9,

...(op.prism || {})

});

return prism;

}

function path2prismPath(p1, p2) {

const p12 = p1.slice(0);

const p22 = p2.slice(0);

const offset = .01;

for (let __a = [ p12, p22 ], __i = 0; __i < __a.length; __i++) {

let p = __a[__i];

p[1] *= 1;

p[1] += offset;

}

return [

p1,

p2,

p22,

p12

];

}

function path2points(path) {

return (() => {

const __arr = [];

for (let __i = 0; __i < path.length; __i++) {

let p = path[__i];

__arr.push(new BMapGL.Point(...p));

}

return __arr;

})();

}

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值