[GIS] How to create KML file with OpenLayers

kmlopenlayers-2

I try to create kml file from features.

My code

var feature=myVecLayer.getFeatureByFid('filedata.1');
                    var format;
                        function GetKMLFromFeatures(feature) {
                             format = new OpenLayers.Format.KML({
                                'maxDepth':10,
                                'extractStyles':true,
                                'internalProjection': new OpenLayers.Projection("EPSG:4326"),
                                'externalProjection': new OpenLayers.Projection("EPSG:4326")
                            });
                        alert(format);  

                        alert(format);
                        return format.write(feature);
                        }

After i use this library to save file https://github.com/dcneiner/Downloadify

                            Downloadify.create('downloadify',{
                            filename: function(){
                                                        alert("okey");
                                return  'kml_test.kml';
                            },
                            data: function(){
                                return GetKMLFromFeatures(features);
                            },
                            onComplete: function(){
                                alert('Your File Has Been Saved!');
                            },
                            onCancel: function(){
                                alert('You have cancelled the saving of this file.');
                            },
                            onError: function(){
                                alert('You must put something in the File Contents or there will be nothing to save!');
                            },
                            transparent: false,
                            swf: 'media/downloadify.swf',
                            downloadImage: 'images/download.png',
                            width: 100,
                            height: 30,
                            transparent: true,
                            append: false
                        });

But nothing heppens. If firebug i get error

TypeError: f.el is null 

...tainer.id};if(f.options.enabled===false)f.enabled=false;if(f.options.transparent...

But more strange that i not get arert alert("okey");

Whats wrong? Or if someone know better way to get kml file please give me advice.

Best Answer

Keep in mind that by using Downloadify, your download will not work on mobile devices and pads. This is because Downloadify relies on flash and flash is dead. iPhones and iPads never supported it and now google has removed it from their play market.

I would write a handler in your back-end language of choice. Very easy, straight forward, and works everywhere.

HERE IS HOW

Update

Looks like there's a new way to do this client side. Please note the browser support.