Skip to content

Commit ab0983a

Browse files
author
ChenGuanglin
committed
【feature】webmap对接mvt底图
1 parent 704659a commit ab0983a

File tree

1 file changed

+145
-8
lines changed

1 file changed

+145
-8
lines changed

src/openlayers/mapping/WebMap.js

Lines changed: 145 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -186,11 +186,24 @@ export class WebMap extends ol.Observable {
186186
return;
187187
}
188188

189-
that.addBaseMap(mapInfo);
190-
if (!mapInfo.layers || mapInfo.layers.length === 0) {
191-
that.sendMapToUser(0);
189+
if (mapInfo.baseLayer && mapInfo.baseLayer.layerType === 'MAPBOXSTYLE') {
190+
// 添加矢量瓦片服务作为底图
191+
that.addMVTBaseMap(mapInfo).then(() => {
192+
if (!mapInfo.layers || mapInfo.layers.length === 0) {
193+
that.sendMapToUser(0);
194+
} else {
195+
that.addLayers(mapInfo);
196+
}
197+
that.createView(mapInfo);
198+
});
192199
} else {
193-
that.addLayers(mapInfo);
200+
that.addBaseMap(mapInfo);
201+
if (!mapInfo.layers || mapInfo.layers.length === 0) {
202+
that.sendMapToUser(0);
203+
} else {
204+
that.addLayers(mapInfo);
205+
}
206+
that.createView(mapInfo);
194207
}
195208
}).catch(function (error) {
196209
that.errorCallback && that.errorCallback(error, 'getMapFaild', that.map);
@@ -320,6 +333,36 @@ export class WebMap extends ol.Observable {
320333
this.map.addLayer(labelLayer);
321334
}
322335
}
336+
337+
/**
338+
* @private
339+
* @function ol.supermap.WebMap.prototype.addMVTBaseMap
340+
* @description 添加矢量瓦片服务底图
341+
* @param {object} mapInfo - 地图信息
342+
*/
343+
addMVTBaseMap(mapInfo) {
344+
// 获取地图详细信息
345+
return this.getMBStyle(mapInfo).then(baseLayerInfo => {
346+
// 创建图层
347+
return this.createMVTLayer(baseLayerInfo).then(layer => {
348+
let layerID = Util.newGuid(8);
349+
if (baseLayerInfo.name) {
350+
layer.setProperties({
351+
name: baseLayerInfo.name,
352+
layerID: layerID,
353+
layerType: 'VECTOR_TILE'
354+
});
355+
}
356+
357+
//否则没有ID,对不上号
358+
baseLayerInfo.layer = layer;
359+
baseLayerInfo.layerID = layerID;
360+
361+
this.map.addLayer(layer);
362+
});
363+
});
364+
}
365+
323366
/**
324367
* @private
325368
* @function ol.supermap.WebMap.prototype.createView
@@ -328,7 +371,7 @@ export class WebMap extends ol.Observable {
328371
*/
329372
createView(options) {
330373
let oldcenter = options.center,
331-
zoom = options.level || 1,
374+
zoom = options.level !== undefined ? options.level : 1,
332375
extent,
333376
projection = this.baseProjection;
334377
let center = [];
@@ -348,11 +391,11 @@ export class WebMap extends ol.Observable {
348391

349392
// 计算当前最大分辨率
350393
let maxResolution;
351-
if(options.baseLayer && options.baseLayer.layerType === "TILE" && extent && extent.length === 4){
394+
if(options.baseLayer && ['TILE', 'MAPBOXSTYLE'].includes(options.baseLayer.layerType) && extent && extent.length === 4){
352395
let width = extent[2] - extent[0];
353396
let height = extent[3] - extent[1];
354-
let maxResolution1 = width/256;
355-
let maxResolution2 = height/256;
397+
let maxResolution1 = width / 512;
398+
let maxResolution2 = height / 512;
356399
maxResolution = Math.max(maxResolution1, maxResolution2);
357400
}
358401

@@ -3188,6 +3231,100 @@ export class WebMap extends ol.Observable {
31883231
});
31893232
})
31903233
}
3234+
3235+
/**
3236+
* @private
3237+
* @function ol.supermap.WebMap.prototype.getMBStyle
3238+
* @description 生成图层信息
3239+
* @param {object} mapInfo - 地图信息
3240+
*/
3241+
getMBStyle(mapInfo) {
3242+
let baseLayer = mapInfo.baseLayer,
3243+
url = baseLayer.dataSource.url,
3244+
layerInfo = {};
3245+
return FetchRequest.get(url).then(result => {
3246+
return result.json();
3247+
}).then(styles => {
3248+
let extent = styles.metadata.mapbounds;
3249+
baseLayer.extent = extent; // 这里把extent保存一下
3250+
3251+
layerInfo.projection = mapInfo.projection,
3252+
layerInfo.epsgCode = mapInfo.projection,
3253+
layerInfo.visible = baseLayer.visible,
3254+
layerInfo.name = baseLayer.name,
3255+
layerInfo.url = url,
3256+
layerInfo.sourceType = 'VECTOR_TILE',
3257+
layerInfo.layerType = 'VECTOR_TILE',
3258+
layerInfo.styles = styles,
3259+
layerInfo.extent = extent,
3260+
layerInfo.bounds = {
3261+
bottom: extent[1],
3262+
left: extent[0],
3263+
leftBottom: { x: extent[0], y: extent[1] },
3264+
right: extent[2],
3265+
rightTop: { x: extent[2], y: extent[3] },
3266+
top: extent[3]
3267+
}
3268+
return layerInfo;
3269+
})
3270+
}
3271+
3272+
/**
3273+
* @private
3274+
* @function ol.supermap.WebMap.prototype.createMVTLayer
3275+
* @description 创建矢量瓦片图层
3276+
* @param {object} mapInfo - 图层信息
3277+
*/
3278+
createMVTLayer(layerInfo) {
3279+
let styles = layerInfo.styles;
3280+
let resolutions = this.getMVTResolutions(layerInfo.bounds);
3281+
// 创建MapBoxStyle样式
3282+
let mapboxStyles = new ol.supermap.MapboxStyles({
3283+
style: styles,
3284+
source: styles.name,
3285+
resolutions,
3286+
map: this.map
3287+
});
3288+
return new Promise((resolve) => {
3289+
mapboxStyles.on('styleloaded', function () {
3290+
let url = Object.values(styles.sources)[0].tiles[0];
3291+
let layer = new ol.layer.VectorTile({
3292+
//设置避让参数
3293+
declutter: true,
3294+
source: new ol.source.VectorTile({
3295+
url,
3296+
projection: layerInfo.projection,
3297+
format: new ol.format.MVT({
3298+
featureClass: ol.Feature
3299+
}),
3300+
wrapX: false
3301+
}),
3302+
style: mapboxStyles.featureStyleFuntion,
3303+
visible: layerInfo.visible
3304+
});
3305+
resolve(layer);
3306+
})
3307+
})
3308+
}
3309+
3310+
/**
3311+
* @private
3312+
* @function ol.supermap.WebMap.prototype.getMVTResolutions
3313+
* @description 创建矢量瓦片图层
3314+
* @param {array} extent - 瓦片范围
3315+
* @param {number} numbers - 缩放层级数
3316+
*/
3317+
getMVTResolutions(extent, numbers = 22) {
3318+
let { left, right, top, bottom } = extent;
3319+
let dx = right - left;
3320+
let dy = top - bottom;
3321+
let calcArgs = dx - dy > 0 ? dx : dy;
3322+
let resolutions = [calcArgs / 512];
3323+
for (let i = 1; i < numbers; i++) {
3324+
resolutions.push(resolutions[i - 1] / 2)
3325+
}
3326+
return resolutions;
3327+
}
31913328
}
31923329

31933330
ol.supermap.WebMap = WebMap;

0 commit comments

Comments
 (0)