【问题标题】:Allow download of KML file created from JavaScript and Google Earth API允许下载从 JavaScript 和 Google Earth API 创建的 KML 文件
【发布时间】:2014-03-28 08:53:47
【问题描述】:

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

我可以构建一个“数据:”URL 来激活下载使用 Google 地球 API 在 JavaScript 中创建的 KML 文件,但这仅适用于 Chrome。部分在 FireFox 中工作,但使用“.part”文件扩展名保存,因此用户必须明确重命名文件才能在 Google 地球中打开。 IE完全忽略了这一点。例如,IE 9 不支持 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/ 的 hack,但它只适用于 Chrome,所以这不是一个实用的解决方案。

【问题讨论】:

标签: javascript google-earth-plugin data-uri


【解决方案1】:

一种可能的方法是使用Downloadify。这是……

"一个支持创建和下载的 javascript 和 Flash 库 无需服务器交互的文本文件。”

您可以看到working example here。不幸的是,我不相信在旧版浏览器中本机可以做到这一点。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多