@@ -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,96 @@ 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 ,
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+ var outlineStroke = new Style ( {
1202+ stroke : new StrokeStyle ( {
1203+ width : strokeWidth + 2 , //外部宽度=内部样式宽度 + 2
1204+ color : outlineColorArray ,
1205+ lineCap : lineCap || 'round' ,
1206+ lineDash : [ 0 ]
1207+ } )
1208+ } ) ;
1209+ return [ outlineStroke , stroke ] ;
1210+ }
1211+ /**
1212+ * @function ol.supermap.StyleUtils.getPathway 获取铁路样式
1213+ * @param {object } style - 样式参数
1214+ * @param {object } outlineStyle - 轮廓样式参数
1215+ * @returns {Object } style对象
1216+ */
1217+ static getPathway ( style , outlineStyle ) {
1218+ let { strokeWidth= ZERO , strokeColor, strokeOpacity } = style ;
1219+ // 道路线都是solid, lineCap都是直角
1220+ const lineDash = ( w => [ w , w + strokeWidth * 2 ] ) ( 4 * strokeWidth ) , lineCap = 'square' ;
1221+ let strokeColorArray = this . hexToRgb ( strokeColor ) ;
1222+ strokeColorArray && strokeColorArray . push ( strokeOpacity ) ;
1223+ var stroke = new Style ( {
1224+ stroke : new StrokeStyle ( {
1225+ width : strokeWidth ,
1226+ color : strokeColorArray ,
1227+ lineCap,
1228+ lineDash
1229+ } )
1230+ } ) ;
1231+
1232+ const { strokeColor : outlineColor } = outlineStyle ;
1233+ let outlineColorArray = this . hexToRgb ( outlineColor ) ;
1234+ // opacity使用style的透明度。保持两根线透明度一致
1235+ outlineColorArray && outlineColorArray . push ( strokeOpacity ) ;
1236+ var outlineStroke = new Style ( {
1237+ stroke : new StrokeStyle ( {
1238+ width : strokeWidth ,
1239+ color : outlineColorArray ,
1240+ lineCap,
1241+ lineDash,
1242+ lineDashOffset : ( lineDash [ 0 ] + lineDash [ 1 ] ) / 2
1243+ } )
11751244 } ) ;
1245+ return [ outlineStroke , stroke ] ;
11761246 }
11771247}
0 commit comments