diff --git a/src/ol/format/kmlformat.js b/src/ol/format/kmlformat.js index 4c1765eb52..1613333fd6 100644 --- a/src/ol/format/kmlformat.js +++ b/src/ol/format/kmlformat.js @@ -10,6 +10,8 @@ goog.require('goog.Uri'); goog.require('goog.array'); goog.require('goog.asserts'); goog.require('goog.dom.NodeType'); +goog.require('goog.object'); +goog.require('goog.string'); goog.require('ol'); goog.require('ol.Feature'); goog.require('ol.FeatureStyleFunction'); @@ -240,16 +242,27 @@ ol.format.KML.DEFAULT_STROKE_STYLE_ = new ol.style.Stroke({ }); +/** + * @const + * @type {ol.style.Stroke} + * @private + */ +ol.format.KML.DEFAULT_TEXT_STROKE_STYLE_ = new ol.style.Stroke({ + color: [51, 51, 51, 1], + width: 2 +}); + + /** * @const * @type {ol.style.Text} * @private */ ol.format.KML.DEFAULT_TEXT_STYLE_ = new ol.style.Text({ - font: 'normal 16px Helvetica', + font: 'bold 16px Helvetica', fill: ol.format.KML.DEFAULT_FILL_STYLE_, - stroke: ol.format.KML.DEFAULT_STROKE_STYLE_, - scale: 1 + stroke: ol.format.KML.DEFAULT_TEXT_STROKE_STYLE_, + scale: 0.8 }); @@ -286,29 +299,109 @@ ol.format.KML.ICON_ANCHOR_UNITS_MAP_ = { }; +/** + * @param {ol.style.Style|undefined} foundStyle Style. + * @param {string} name Name. + * @return {ol.style.Style} style Style. + * @private + */ +ol.format.KML.createNameStyleFunction_ = function(foundStyle, name) { + /** @type {?ol.style.Text} */ + var textStyle = null; + var textOffset = [0, 0]; + var textAlign = 'start'; + if (foundStyle.getImage()) { + var imageSize = foundStyle.getImage().getImageSize(); + if (!goog.object.isEmpty(imageSize)) { + // Offset the label to be centered to the right of the icon, if there is + // one. + textOffset[0] = foundStyle.getImage().getScale() * imageSize[0] / 2; + textOffset[1] = -foundStyle.getImage().getScale() * imageSize[1] / 2; + textAlign = 'left'; + } + } + if (!goog.object.isEmpty(foundStyle.getText())) { + textStyle = /** @type {ol.style.Text} */ + (goog.object.clone(foundStyle.getText())); + textStyle.setText(name); + textStyle.setTextAlign(textAlign); + textStyle.setOffsetX(textOffset[0]); + textStyle.setOffsetY(textOffset[1]); + } else { + textStyle = new ol.style.Text({ + text: name, + offsetX: textOffset[0], + offsetY: textOffset[1], + textAlign: textAlign + }); + } + var nameStyle = new ol.style.Style({ + fill: undefined, + image: undefined, + text: textStyle, + stroke: undefined, + zIndex: undefined + }); + return nameStyle; +}; + + /** * @param {Array.|undefined} style Style. * @param {string} styleUrl Style URL. * @param {Array.} defaultStyle Default style. - * @param {Object.|string)>} sharedStyles - * Shared styles. + * @param {Object.|string)>} sharedStyles Shared + * styles. * @return {ol.FeatureStyleFunction} Feature style function. * @private */ -ol.format.KML.createFeatureStyleFunction_ = function( - style, styleUrl, defaultStyle, sharedStyles) { +ol.format.KML.createFeatureStyleFunction_ = function(style, styleUrl, + defaultStyle, sharedStyles) { return ( /** - * @param {number} resolution Resolution. + * @param {number} + * resolution Resolution. * @return {Array.} Style. * @this {ol.Feature} */ function(resolution) { + var drawName = false; + /** @type {ol.style.Style|undefined} */ + var nameStyle; + /** @type {string} */ + var name = ''; + if (this.getGeometry()) { + drawName = (this.getGeometry().getType() === + ol.geom.GeometryType.POINT); + } + + if (drawName) { + name = /** @type {string} */ (this.getProperties()['name']); + drawName = drawName && !goog.string.isEmptySafe(name); + } + if (style) { + if (drawName) { + nameStyle = ol.format.KML.createNameStyleFunction_(style[0], + name); + return [style, nameStyle]; + } return style; } if (styleUrl) { - return ol.format.KML.findStyle_(styleUrl, defaultStyle, sharedStyles); + var foundStyle = ol.format.KML.findStyle_(styleUrl, defaultStyle, + sharedStyles); + if (drawName) { + nameStyle = ol.format.KML.createNameStyleFunction_(foundStyle[0], + name); + return foundStyle.concat(nameStyle); + } + return foundStyle; + } + if (drawName) { + nameStyle = ol.format.KML.createNameStyleFunction_(defaultStyle[0], + name); + return defaultStyle.concat(nameStyle); } return defaultStyle; });