Openlayers export to KML and keep my map styles

旧城冷巷雨未停 提交于 2020-08-02 22:36:51

问题


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

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!