OpenLayers.Format.KML will allow you to get all your features as a KML string.
like so:
Save as string:
function GetKMLFromFeatures(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);
}
Download the string:
But if what you actually want is to download a KML file of the string, you will face the issue that the KML string already resides on the client's browser so you can't download it because it's already there. To Download it you'll have to send it back to a handler on the server side to return a response with the proper file extension and content type application/kml
;
This will initiate a browser download.
Check my more indepth answer on StackOverflow.
Update:
A comment on SO pointed me to a new way of doing this. Though, make sure browser compatibility is appropriate.
Well, I don't really know why your code does not work, but I have never managed to colour a wms layer in OpenLayers, so I have always done it with sld in geoserver. More info on that in the "sld cookbook"
With this it is also possible to colour features based on their attributes.
However, I don't see where is the content of your vector layer "polygon", what is the purpose of this layer?
Best Answer
I found a cool sample that illustrates the handling. Take a look at this (the source code :-D) http://edgemaster.dev.openstreetmap.org/oldemo/10.htm
Your vector layer needs a server part two handle the persistence. The save strategy only triggers the write back.