@@ -14,6 +14,7 @@ import StrokeStyle from 'ol/style/Stroke';
1414import Text from 'ol/style/Text' ;
1515
1616var padding = 8 , doublePadding = padding * 2 ;
17+ const ZERO = 0.0000001 ;
1718
1819/**
1920 * @class ol.supermap.StyleUtils
@@ -599,7 +600,6 @@ export class StyleUtils {
599600 style = style || this . getDefaultStyle ( ) ;
600601 let olStyle = new Style ( ) ;
601602 let newImage , newFill , newStroke ;
602- const ZERO = 0.0000001 ;
603603 let {
604604 fillColor,
605605 fillOpacity,
@@ -1152,26 +1152,95 @@ export class StyleUtils {
11521152 * @returns {Object } style对象
11531153 */
11541154 static getImageStyle ( styleParams ) {
1155- let size = styleParams . imageInfo . size ,
1156- scale = 2 * styleParams . radius / size . w ;
1157- let imageInfo = styleParams . imageInfo ;
1158- let imgDom = imageInfo . img ;
1159- if ( ! imgDom || ! imgDom . src ) {
1160- imgDom = new Image ( ) ;
1161- //要组装成完整的url
1162- imgDom . src = imageInfo . url ;
1163- }
1164- const { offsetX, offsetY, rotation } = styleParams ;
1165- let anchor = this . getIconAnchor ( offsetX , offsetY ) ;
1166- return new Style ( {
1167- image : new Icon ( {
1168- img : imgDom ,
1169- scale,
1170- imgSize : [ size . w , size . h ] ,
1171- anchor : anchor || [ 0.5 , 0.5 ] ,
1172- anchorOrigin : 'bottom-right' ,
1173- rotation
1174- } )
1155+ let size = styleParams . imageInfo . size ,
1156+ scale = 2 * styleParams . radius / size . w ;
1157+ let imageInfo = styleParams . imageInfo ;
1158+ let imgDom = imageInfo . img ;
1159+ if ( ! imgDom || ! imgDom . src ) {
1160+ imgDom = new Image ( ) ;
1161+ //要组装成完整的url
1162+ imgDom . src = imageInfo . url ;
1163+ }
1164+ const { offsetX, offsetY, rotation } = styleParams ;
1165+ let anchor = this . getIconAnchor ( offsetX , offsetY ) ;
1166+ return new Style ( {
1167+ image : new Icon ( {
1168+ img : imgDom ,
1169+ scale,
1170+ imgSize : [ size . w , size . h ] ,
1171+ anchor : anchor || [ 0.5 , 0.5 ] ,
1172+ anchorOrigin : 'bottom-right' ,
1173+ rotation
1174+ } )
1175+ } ) ;
1176+ }
1177+ /**
1178+ * @function ol.supermap.StyleUtils.getRoadPath 获取道路样式
1179+ * @param {object } style - 样式参数
1180+ * @param {object } outlineStyle - 轮廓样式参数
1181+ * @returns {Object } style对象
1182+ */
1183+ static getRoadPath ( style , outlineStyle ) {
1184+ const { strokeWidth= ZERO , lineCap, strokeColor, strokeOpacity } = style ;
1185+ // 道路线都是solid
1186+ let strokeColorArray = this . hexToRgb ( strokeColor ) ;
1187+ strokeColorArray && strokeColorArray . push ( strokeOpacity ) ;
1188+ var stroke = new Style ( {
1189+ stroke : new StrokeStyle ( {
1190+ width : strokeWidth || ZERO ,
1191+ color : strokeColorArray ,
1192+ lineCap : lineCap || 'round' ,
1193+ lineDash : [ 0 ]
1194+ } )
1195+ } ) ;
1196+
1197+ const { strokeColor : outlineColor } = outlineStyle ;
1198+ let outlineColorArray = this . hexToRgb ( outlineColor ) ;
1199+ // opacity使用style的透明度。保持两根线透明度一致
1200+ outlineColorArray && outlineColorArray . push ( strokeOpacity ) ;
1201+ let outlineWidth = strokeWidth === 0 ? ZERO : strokeWidth + 2 ; //外部宽度=内部样式宽度 + 2
1202+ var outlineStroke = new Style ( {
1203+ stroke : new StrokeStyle ( {
1204+ width : outlineWidth , //外部宽度=内部样式宽度 + 2
1205+ color : outlineColorArray ,
1206+ lineCap : lineCap || 'round' ,
1207+ lineDash : [ 0 ]
1208+ } )
1209+ } ) ;
1210+ return [ outlineStroke , stroke ] ;
1211+ }
1212+ /**
1213+ * @function ol.supermap.StyleUtils.getPathway 获取铁路样式
1214+ * @param {object } style - 样式参数
1215+ * @param {object } outlineStyle - 轮廓样式参数
1216+ * @returns {Object } style对象
1217+ */
1218+ static getPathway ( style , outlineStyle ) {
1219+ let { strokeWidth= ZERO , strokeColor, strokeOpacity } = style ;
1220+ // 道路线都是solid, lineCap都是直角
1221+ const lineDash = ( w => [ w , w + strokeWidth * 2 ] ) ( 4 * strokeWidth ) , lineCap = 'square' ;
1222+ let strokeColorArray = this . hexToRgb ( strokeColor ) ;
1223+ strokeColorArray && strokeColorArray . push ( strokeOpacity ) ;
1224+ var stroke = new Style ( {
1225+ stroke : new StrokeStyle ( {
1226+ width : strokeWidth * 0.5 || ZERO ,
1227+ color : strokeColorArray ,
1228+ lineCap,
1229+ lineDash
1230+ } )
1231+ } ) ;
1232+
1233+ const { strokeColor : outlineColor } = outlineStyle ;
1234+ let outlineColorArray = this . hexToRgb ( outlineColor ) ;
1235+ // opacity使用style的透明度。保持两根线透明度一致
1236+ outlineColorArray && outlineColorArray . push ( strokeOpacity ) ;
1237+ var outlineStroke = new Style ( {
1238+ stroke : new StrokeStyle ( {
1239+ width : strokeWidth || ZERO ,
1240+ color : outlineColorArray ,
1241+ lineCap
1242+ } )
11751243 } ) ;
1244+ return [ outlineStroke , stroke ] ;
11761245 }
11771246}
0 commit comments