Question

Want to create a web-application running entirely in the browser without a backend server component that creates KML on-the-fly using Google Earth API where clicking a "download" button activates the normal content download feature of the web browser either prompting to save a file with .kml extension or automatically launching Google Earth by way of the KML mime type.

I can construct a "data:" URL to activate a download of a KML file created within JavaScript using Google Earth API but this only works in Chrome. Works partially in FireFox but saved with a ".part" file extension so user must explicitly rename the file to open in Google Earth. This is completely ignored by IE. IE 9, for example, doesn't support Javascript generated file downloads. I'm looking for a cross-browser solution with Chrome + FireFox + IE at a minimum.

<script type="text/javascript">
var ge;
var placemark;

google.load("earth", "1");

function init() {
  google.earth.createInstance('map3d', initCallback, failureCallback);    
}

... some code to create some KmlObject

function download() {
  // serialize as KML text to export
  var placemarkText = placemark.getKml();
  if (placemarkText) {
    var uriContent = "data:application/vnd.google-earth.kml+xml;charset=UTF-8," +
        encodeURIComponent(placemarkText); 
    window.open(uriContent, 'KML Download');
  }
}

</script>
</head>

<body onload="init()" style="font-family: arial, sans-serif; font-size: 13px; border: 0;">
 <INPUT TYPE="button" NAME="button2" Value="Download KML" onClick="javascript:download()">
 <div id="map3d" style="width: 600px; height: 380px;"></div>
</body>
</html>

I've seen the hack with http://code.google.com/p/download-data-uri/ but that only works on Chrome so that is not a practical solution.

Was it helpful?

Solution

One possible way to do this is to use Downloadify. It is ...

"a javascript and Flash library that enables the creation and download of text files without server interaction."

You can see a working example here. Unfortunately I don't believe there is anyway to do this natively across legacy browsers.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top