简体   繁体   English

允许下载通过JavaScript和Google Earth API创建的KML文件

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

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. 想要创建一个完全在浏览器中运行的Web应用程序,而没有后端服务器组件,该组件使用Google Earth API即时创建KML,在该组件中,单击“下载”按钮将激活Web浏览器的正常内容下载功能,或者提示保存具有.kml扩展名的文件,或通过KML MIME类型自动启动Google Earth。

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. 我可以构造一个“数据:” URL来激活对使用Google Earth API在JavaScript中创建的KML文件的下载,但这仅适用于Chrome。 Works partially in FireFox but saved with a ".part" file extension so user must explicitly rename the file to open in Google Earth. 在FireFox中部分工作,但是以“ .part”文件扩展名保存,因此用户必须显式重命名文件才能在Google Earth中打开。 This is completely ignored by IE. IE完全忽略了这一点。 IE 9, for example, doesn't support Javascript generated file downloads. 例如,IE 9不支持Javascript生成的文件下载。 I'm looking for a cross-browser solution with Chrome + FireFox + IE at a minimum. 我正在寻找至少使用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>

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. 我已经看到了使用http://code.google.com/p/download-data-uri/进行的黑客攻击,但这仅适用于Chrome浏览器,因此这不是实际的解决方案。

One possible way to do this is to use Downloadify . 一种可行的方法是使用Downloadify It is ... 它是 ...

"a javascript and Flash library that enables the creation and download of text files without server interaction." “一个JavaScript和Flash库,无需服务器交互即可创建和下载文本文件。”

You can see a working example here . 您可以在此处看到一个有效的示例 Unfortunately I don't believe there is anyway to do this natively across legacy browsers. 不幸的是,我不认为可以在旧版浏览器中本地执行此操作。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM