Skip to content

Commit 91064fa

Browse files
author
ChenGuanglin
committed
2 parents 2163d1a + 84babe1 commit 91064fa

File tree

4 files changed

+301
-109
lines changed

4 files changed

+301
-109
lines changed

src/openlayers/core/StyleUtils.js

Lines changed: 76 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -610,6 +610,8 @@ export class StyleUtils {
610610
lineCap,
611611
src,
612612
scale,
613+
offsetX,
614+
offsetY,
613615
//size,
614616
//imgSize,
615617
anchor
@@ -647,14 +649,15 @@ export class StyleUtils {
647649
}
648650
} else {
649651
newImage = new CircleStyle({
650-
radius: radius,
652+
radius,
651653
fill: new FillStyle({
652654
color: fillColorArray
653655
}),
654656
stroke: new StrokeStyle({
655657
width: strokeWidth || ZERO,
656658
color: strokeColorArray
657-
})
659+
}),
660+
displacement: this.getCircleDisplacement(radius, offsetX, offsetY)
658661
});
659662
}
660663
olStyle.setImage(newImage);
@@ -692,6 +695,44 @@ export class StyleUtils {
692695
return olStyle;
693696
}
694697

698+
/**
699+
* @function ol.supermap.StyleUtils.getIconAnchor
700+
* @description 获取图标的锚点
701+
* @param {number} offsetX - X方向偏移分数
702+
* @param {number} offsetY - Y方向偏移分数
703+
* @returns {array}
704+
*/
705+
static getIconAnchor(offsetX, offsetY) {
706+
return [offsetX, offsetY];
707+
}
708+
/**
709+
* @function ol.supermap.StyleUtils.getCircleDisplacement
710+
* @description 获取圆圈的偏移
711+
* @param {number} radius - 圆圈半径
712+
* @param {number} offsetX - X方向偏移分数
713+
* @param {number} offsetY - Y方向偏移分数
714+
* @returns {array}
715+
*/
716+
static getCircleDisplacement(radius, offsetX, offsetY) {
717+
const dispX = radius*offsetX, dispY = radius*offsetY;
718+
return [dispX, -dispY];
719+
}
720+
/**
721+
* @function ol.supermap.StyleUtils.getTextOffset
722+
* @description 获取字体图标的偏移值
723+
* @param {string} fontSize - 字体大小,如12px
724+
* @param {number} offsetX - X方向偏移分数
725+
* @param {number} offsetY - Y方向偏移分数
726+
* @returns {object}
727+
*/
728+
static getTextOffset(fontSize, offsetX, offsetY) {
729+
const radius = fontSize.substr(0, fontSize.length - 2) / 2;
730+
return {
731+
x: radius*offsetX,
732+
y: radius*offsetY
733+
};
734+
}
735+
695736
/**
696737
* 获取文字标注对应的canvas
697738
* @param style
@@ -1033,23 +1074,28 @@ export class StyleUtils {
10331074

10341075
let fontSize = isRank ? 2 * parameters.radius + "px" : parameters.fontSize;
10351076

1077+
const {offsetX, offsetY, rotation} = parameters;
1078+
const offset = StyleUtils.getTextOffset(fontSize, offsetX, offsetY);
10361079
return new Style({
1037-
text: new Text({
1038-
text: text,
1039-
font: fontSize + " supermapol-icons",
1040-
placement: 'point',
1041-
textAlign: 'center',
1042-
fill: new FillStyle({
1043-
color: fillColor
1044-
}),
1045-
backgroundFill: new FillStyle({
1046-
color: [0, 0, 0, 0]
1047-
}),
1048-
stroke: new StrokeStyle({
1049-
width: parameters.strokeWidth || 0.000001,
1050-
color: strokeColor
1080+
text: new Text({
1081+
text: text,
1082+
font: fontSize + " supermapol-icons",
1083+
placement: 'point',
1084+
textAlign: 'center',
1085+
fill: new FillStyle({
1086+
color: fillColor
1087+
}),
1088+
backgroundFill: new FillStyle({
1089+
color: [0, 0, 0, 0]
1090+
}),
1091+
stroke: new StrokeStyle({
1092+
width: parameters.strokeWidth || 0.000001,
1093+
color: strokeColor
1094+
}),
1095+
offsetX: offset.x,
1096+
offsetY: offset.y,
1097+
rotation
10511098
})
1052-
})
10531099
});
10541100
}
10551101
/**
@@ -1063,14 +1109,18 @@ export class StyleUtils {
10631109
that.svgDiv = document.createElement('div');
10641110
document.body.appendChild(that.svgDiv);
10651111
}
1066-
StyleUtils.getCanvasFromSVG(styleParams.url, that.svgDiv, function (canvas) {
1112+
const { url, radius, offsetX, offsetY, fillOpacity, rotation } = styleParams;
1113+
let anchor = this.getIconAnchor(offsetX, offsetY);
1114+
StyleUtils.getCanvasFromSVG(url, that.svgDiv, function (canvas) {
10671115
style = new Style({
10681116
image: new Icon({
10691117
img: that.setColorToCanvas(canvas, styleParams),
1070-
scale: styleParams.radius / canvas.width,
1118+
scale: 2 * radius / canvas.width,
10711119
imgSize: [canvas.width, canvas.height],
1072-
anchor: [0.5, 0.5],
1073-
opacity: styleParams.fillOpacity
1120+
anchor: anchor || [0.5, 0.5],
1121+
opacity: fillOpacity,
1122+
anchorOrigin: 'bottom-right',
1123+
rotation
10741124
})
10751125
});
10761126
});
@@ -1111,14 +1161,17 @@ export class StyleUtils {
11111161
//要组装成完整的url
11121162
imgDom.src = imageInfo.url;
11131163
}
1164+
const { offsetX, offsetY, rotation } = styleParams;
1165+
let anchor = this.getIconAnchor(offsetX, offsetY);
11141166
return new Style({
11151167
image: new Icon({
11161168
img: imgDom,
11171169
scale,
11181170
imgSize: [size.w, size.h],
1119-
anchor: [0.5, 0.5]
1171+
anchor: anchor || [0.5, 0.5],
1172+
anchorOrigin: 'bottom-right',
1173+
rotation
11201174
})
11211175
});
11221176
}
1123-
11241177
}

0 commit comments

Comments
 (0)