允许下载从JavaScript和谷歌地球API创建的KML文件

Allow download of KML file created from JavaScript and Google Earth API

本文关键字:API 谷歌地球 创建 文件 KML JavaScript 许下载 下载      更新时间:2023-09-26

想要创建一个完全在浏览器中运行的web应用程序,而没有使用Google Earth API即时创建KML的后端服务器组件,其中单击"下载"按钮激活web浏览器的正常内容下载功能,提示以。KML扩展名保存文件或通过KML mime类型自动启动Google Earth。

我可以构造一个"data:"URL来激活使用Google Earth API在JavaScript中创建的KML文件的下载,但这只适用于Chrome。部分适用于FireFox,但保存时使用"。"part"文件扩展名,因此用户必须显式重命名文件以在Google Earth中打开。IE完全忽略了这一点。例如,ie9不支持Javascript生成的文件下载。我正在寻找一个跨浏览器的解决方案与Chrome + FireFox + IE在最低限度。

<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>

我已经看到了黑客http://code.google.com/p/download-data-uri/,但这只适用于Chrome,所以这不是一个实用的解决方案。

一种可能的方法是使用download。它是……

"一个javascript和Flash库,可以创建和下载的文本文件,无需服务器交互。"

您可以在这里看到一个工作示例。不幸的是,我不相信有任何方式可以在传统浏览器上实现这一点。