问题
I successfully write a KML from Openlayers, however no styles (colors, stroke, etc.) are present in the kml file. Is it possible to generate the KML with the styles?
I found a similar question here: https://gis.stackexchange.com/questions/17031/openlayers-format-kml-write-style
Thanks in advance.
回答1:
As of yet the WRITE method does not make use of the 'extractStyles':true,
property as you can see here. Only the READ method does.
The only way I saw was simply to recreate them. In the example below, I've created the KML style I wanted and Injected it into openlayers created kml string.
myorg.Util.GetKMLFromFeatures = function (features, strfolderName, strfolderDescription) {
var format = new OpenLayers.Format.KML({
'maxDepth': 10,
'extractStyles': true,
'internalProjection': myorg.UI.Map.getMap().baseLayer.projection,
'externalProjection': myorg.UI.Map.Projections.Geographic
});
var kmlStyle = "<Style id='OutlineOnlyStyle'><PolyStyle><color>ff0000cc</color><fill>0</fill><outline>1</outline></PolyStyle></Style>";
format.foldersName = strfolderName;
format.foldersDesc = strfolderDescription;
//add style description
var kml = format.write(features).replace(/<Folder>/g, '<Folder>' + kmlStyle)
.replace(/><name>/g, '><styleUrl>#OutlineOnlyStyle</styleUrl><name>');
return kml;
};
来源:https://stackoverflow.com/questions/11990692/openlayers-export-to-kml-and-keep-my-map-styles