Skip to content

Commit 8c2dc6b

Browse files
author
潘卓然ParnDeedlit
committed
【SDK】【服务】【新增注记样式的Marker的MapboxGL实现】
1 parent 9af9517 commit 8c2dc6b

File tree

4 files changed

+118
-30
lines changed

4 files changed

+118
-30
lines changed

src/service/base/style/ExtrudeStyle.js

Lines changed: 45 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,7 @@ import { Symbol } from './Symbol';
77
* 拉伸体样式
88
* @class mapgis.style.ExtrudeStyle
99
* @classdesc 拉伸体样式
10-
* @param {Number} [outlineWidth = 0] 拉伸体外边线宽度,默认为1
11-
* @param {String} [outlineColor = #FFFFFF] 拉伸体外边线颜色,16进制颜色或rgb值或rgba值,默认#FFFFFF,白色
10+
* @param {String} [color = #FFFFFF] 拉伸体外边线颜色,16进制颜色或rgb值或rgba值,默认#FFFFFF,白色
1211
* @param {Object} [shadowStyle = undefined] 阴影样式,默认undefined
1312
* @param {Object} [symbolStyle = undefined] 填充图案样式,默认undefined
1413
* @param {Object} [outlineSymbolStyle = undefined] 拉伸体外边线填充图案样式,默认undefined
@@ -17,14 +16,54 @@ export default class ExtrudeStyle extends VectorStyle {
1716
constructor(option) {
1817
super();
1918
var options = option ? option : {};
20-
const { symbol } = options;
19+
const { color = '#FFFFFF', opacity = 1.0, symbol } = options;
20+
const { castShadows = true, edges, size = 1, material } = options;
21+
2122
this.type = 'extrude';
22-
this.outlineWidth = 1;
23-
this.outlineColor = '#FFFFFF';
24-
this.shadowStyle = undefined;
23+
24+
this.color = color;
25+
this.opacity = opacity;
2526
this.symbolStyle = symbol || new Symbol();
27+
28+
this.castShadows = castShadows;
29+
this.edges = edges;
30+
this.material = material || color;
31+
this.size = size;
32+
2633
extend(this, options);
2734
}
35+
36+
/**
37+
* @param {Boolean} [highlight = false] 是否激活高亮样式
38+
* @link https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#fill-extrusion
39+
* @returns MapboxGL线格式的样式
40+
*/
41+
toMapboxStyle(options) {
42+
options = options || {};
43+
const { highlight = false } = options;
44+
let { material, size, opacity, symbolStyle } = this;
45+
const { pattern, xoffset, yoffset } = symbolStyle;
46+
let style = {
47+
filter: ['==', '$type', 'Polygon'],
48+
paint: {
49+
'fill-extrusion-base': 0,
50+
'fill-extrusion-color': material,
51+
'fill-extrusion-height': size,
52+
'fill-extrusion-opacity': opacity,
53+
'fill-extrusion-translate': [xoffset, yoffset],
54+
'fill-extrusion-translate-anchor': 'map',
55+
'fill-extrusion-vertical-gradient': true
56+
}
57+
};
58+
if (symbolStyle && pattern) {
59+
style.paint['fill-extrusion-pattern'] = pattern;
60+
}
61+
if (highlight) {
62+
style.paint['fill-extrusion-color'] = ['case', ['boolean', ['feature-state', 'hover'], false], material, 'rgba(0,0,0,0)'];
63+
style.paint['fill-extrusion-base'] = ['case', ['boolean', ['feature-state', 'hover'], false], 100, 0];
64+
}
65+
return style;
66+
}
2867
}
2968

3069
export { ExtrudeStyle };

src/service/base/style/MarkerStyle.js

Lines changed: 52 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -25,35 +25,76 @@ export default class MarkerStyle extends VectorStyle {
2525
extend(this, options);
2626
}
2727

28+
/**
29+
* @param {Boolean} [highlight = false] 是否激活高亮样式
30+
* @link https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#symbol
31+
* @returns MapboxGL点格式的样式
32+
*/
33+
toMapboxStyle(options) {
34+
options = options || {};
35+
const { highlight = false } = options;
36+
const { symbol } = this;
37+
const { allowOverlap, color, pattern, opacity } = symbol;
38+
const { size, rotate, xoffset, yoffset } = symbol;
39+
40+
let mapstyle = {
41+
paint: {
42+
'icon-color': color,
43+
'icon-opacity': opacity
44+
},
45+
layout: {
46+
'icon-allow-overlap': allowOverlap,
47+
'icon-offset': [xoffset, yoffset],
48+
'icon-size': size,
49+
'icon-rotate': rotate
50+
}
51+
};
52+
if (pattern) {
53+
mapstyle.layout['icon-image'] = pattern;
54+
}
55+
if (highlight) {
56+
}
57+
return mapstyle;
58+
}
59+
2860
/**
2961
* @link https://sandcastle.cesium.com/index.html?src=Circles%20and%20Ellipses.html&label=Geometries
3062
* @returns Cesium点格式的样式
3163
*/
3264
toCesiumStyle(options, feature, Cesium) {
3365
const {
34-
field = "", scale = 1, backgroundColor = "#FFFFFF", backgroundOpacity = 0, color = "#FFFFFF", opacity = 1,
35-
outlineColor = "#FFFFFF", outlineOpacity = 1, outlineWidth = 0,xOffset = 0 , yOffset = 0, text, show = true
36-
} = options;
37-
const {
38-
url = "", rotation = 0, imageScale = 1, width, height
66+
field = '',
67+
scale = 1,
68+
backgroundColor = '#FFFFFF',
69+
backgroundOpacity = 0,
70+
color = '#FFFFFF',
71+
opacity = 1,
72+
outlineColor = '#FFFFFF',
73+
outlineOpacity = 1,
74+
outlineWidth = 0,
75+
xOffset = 0,
76+
yOffset = 0,
77+
text,
78+
show = true
3979
} = options;
80+
const { url = '', rotation = 0, imageScale = 1, width, height } = options;
4081
let labelText;
41-
if(field && feature.properties && feature.properties[field]){
82+
if (field && feature.properties && feature.properties[field]) {
4283
labelText = feature.properties[field];
4384
}
44-
if(text){
85+
if (text) {
4586
labelText = text;
4687
}
4788
let billboard = {
4889
show: show,
4990
image: url,
5091
rotation: rotation,
5192
scale: imageScale
52-
}
53-
if(width){
93+
};
94+
if (width) {
5495
billboard.width = width;
5596
}
56-
if(height){
97+
if (height) {
5798
billboard.height = height;
5899
}
59100
return {
@@ -66,7 +107,7 @@ export default class MarkerStyle extends VectorStyle {
66107
outlineColor: Cesium.Color.fromCssColorString(outlineColor).withAlpha(outlineOpacity),
67108
outlineWidth: outlineWidth,
68109
backgroundColor: Cesium.Color.fromCssColorString(backgroundColor).withAlpha(backgroundOpacity),
69-
pixelOffset: new Cesium.Cartesian2(xOffset, yOffset * -1),
110+
pixelOffset: new Cesium.Cartesian2(xOffset, yOffset * -1)
70111
},
71112
billboard: billboard
72113
};

src/service/base/style/Symbol.js

Lines changed: 14 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@ import { Anchor } from './Enum';
66
* 符号样式
77
* @class mapgis.style.SymbolStyle
88
* @classdesc 符号样式
9-
* @param {String} [symbol = ""] 符号名称或url
9+
* @param {String} [pattern = ""] 符号名称或url
1010
* @param {Number} [opacity = 1] 透明度,0~1之间的值,默认为1,不透明
1111
* @param {String} [color = #FFFFFF] 颜色,十六进制或RGB,默认为#FFFFFF,白色
1212
* @param {Number} [size = 1] 符号大小
@@ -18,15 +18,20 @@ import { Anchor } from './Enum';
1818
export default class Symbol {
1919
constructor(option) {
2020
var options = option ? option : {};
21-
const { symbol } = options;
22-
this.symbol = symbol || '';
23-
this.opacity = 1;
24-
this.color = '#FFFFFF';
25-
this.size = 1;
26-
this.rotate = 0;
27-
this.offsetX = 0;
28-
this.offsetY = 0;
21+
const { pattern = undefined, opacity = 1.0, allowOverlap = false } = options;
22+
const { color = '#FFFFFF', size = 1, rotate = 0 } = options;
23+
const { xoffset = 0, yoffset = 0 } = options;
24+
25+
this.allowOverlap = allowOverlap;
26+
this.pattern = pattern;
27+
this.opacity = opacity;
28+
this.color = color;
29+
this.size = size;
30+
this.rotate = rotate;
31+
this.xoffset = xoffset;
32+
this.yoffset = yoffset;
2933
this.anchor = Anchor.center;
34+
3035
extend(this, options);
3136
}
3237
}

src/service/base/style/TextStyle.js

Lines changed: 7 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -28,13 +28,15 @@ export default class TextStyle extends VectorStyle {
2828
constructor(option) {
2929
super();
3030
var options = option ? option : {};
31-
const { angle = 0, color = '#000000', font } = options;
31+
const { angle = 0, allowOverlap = false } = options;
3232
const { backgroundColor, borderLineColor, borderLineSize } = options;
33+
const { color = '#000000', font } = options;
3334
const { haloColor = '#000000', haloSize = 0, horizontalAlignment = HorizontalAlignment.center } = options;
3435
const { kerning = true, lineHeight = 1.0, lineWidth = 192 } = options;
3536
const { rotated = false, text = '', type = 'text', verticalAlignment = VerticalAlignment.baseline } = options;
3637
const { xoffset = 0, yoffset = 0 } = options;
3738

39+
this.allowOverlap = allowOverlap;
3840
this.angle = angle;
3941
this.backgroundColor = backgroundColor;
4042
this.borderLineColor = borderLineColor;
@@ -59,13 +61,13 @@ export default class TextStyle extends VectorStyle {
5961

6062
/**
6163
* @param {Boolean} [highlight = false] 是否激活高亮样式
62-
* @link https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#circle
64+
* @link https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#symbol
6365
* @returns MapboxGL点格式的样式
6466
*/
6567
toMapboxStyle(options) {
6668
options = options || {};
6769
const { highlight = false } = options;
68-
const { angle, color, font } = this;
70+
const { angle, allowOverlap, color, font } = this;
6971
const { family, size, style, weight } = font;
7072
const { backgroundColor, borderLineColor, borderLineSize } = this;
7173
const { haloColor, haloSize, horizontalAlignment } = this;
@@ -86,7 +88,8 @@ export default class TextStyle extends VectorStyle {
8688
'text-max-width': lineWidth,
8789
'text-field': text,
8890
'text-offset': [xoffset, yoffset],
89-
'text-size': size
91+
'text-size': size,
92+
'text-allow-overlap': allowOverlap
9093
}
9194
};
9295
if (highlight) {

0 commit comments

Comments
 (0)