Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

I would like to apply a style to the KML that gets generated from the write() operation of OpenLayers.Format.KML. The current style that is on the vector layer does not get exported with this code:

function GetKMLFromFeatures = function (features) {
    var format = new OpenLayers.Format.KML({
        'maxDepth':10,
        'extractStyles':true,
        'internalProjection': map.baseLayer.projection,
        'externalProjection': new OpenLayers.Projection("EPSG:4326")
    });

    return format.write(features);
}

I would like to add something like this to the KML:

<Style id="PolyStyle">
    <PolyStyle>
        <color>ff0000cc</color>
        <fill>0</fill>
        <!-- boolean -->
        <outline>1</outline>
        <!-- boolean -->
    </PolyStyle>
</Style>

So that i can specify the fill property. How annoying is this:

enter image description here

I want this:

enter image description here

share|improve this question

1 Answer 1

up vote 0 down vote accepted

For the moment i'm just using the javascript replace funtion until a better solution or feature is released/proposed. (I will give you credit if it comes a long)

function GetKMLFromFeatures = function (features) {
    var format = new OpenLayers.Format.KML({
        'maxDepth':10,
        'extractStyles':true,
        'internalProjection': map.baseLayer.projection,
        'externalProjection': new OpenLayers.Projection("EPSG:4326")
    });

    var withStyleHeading = format.write(features).replace(/<Folder>/g, '<Folder>' + kmlStyle);
    var withEverything = withStyleHeading.replace(/lacemark><name>/g, 'lacemark><styleUrl>#PolyStyle</styleUrl><name>');

    return withEverything;
}
share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.