是否可以從OpenLayers編寫和保存KML?任何人都知道一個出口的例子嗎?Openlayers根據您的地圖編寫並保存KML
回答
只能載體功能導出爲KML。
function GetKMLFromFeatures(features) {
var format = new OpenLayers.Format.KML({
'maxDepth':10,
'extractStyles':true,
'internalProjection': map.baseLayer.projection,
'externalProjection': new OpenLayers.Projection("EPSG:4326")
});
return format.write(features);
}
UPDATE
爲了強制瀏覽器下載KML字符串作爲KML文件,你需要該字符串發送回服務器端,因此它可以被返回給瀏覽器作爲文件下載。
您還沒有指定在服務器端使用的是哪種語言/平臺/ etc但這是我在C#中所做的。
我創建了一個處理函數,它接受來自查詢字符串的文件名和來自textarea表單的KML。
KMLDownload.ashx:
<%@ WebHandler Language="C#" Class="KMLDownload" %>
using System;
using System.Web;
public class KMLDownload : IHttpHandler
{
public void ProcessRequest(HttpContext context)
{
HttpResponse response = context.Response;
string kml = context.Request["kml"];
string filename = context.Request.QueryString["filename"];
if (String.IsNullOrEmpty(kml))
{
context.Response.ContentType = "text/plain";
context.Response.Write("{\"error\":\"No files recevied\"}");
}
else
{
if (String.IsNullOrEmpty(filename)){
filename = "Features_KML.kml";
}
// force a download of the kml file.
response.Clear();
response.ContentType = "application/kml";
response.AddHeader("Content-Disposition", "attachment; filename=" + filename);
response.AddHeader("content-legth", kml.Length.ToString());
response.Write(kml.ToString());
response.End();
}
}
public bool IsReusable
{
get
{
return false;
}
}
}
然後從我的JavaScript身邊,我只是把這個開始下載:如果您使用的OpenLayers 3或4
var filename = "NameofKMLfileI_WANT.kml";
var url = "secure/KMLDownload.ashx";
if (filename) {
url += "?filename=" + filename;
}
var input = '<TEXTAREA name="kml">' + kml + '</TEXTAREA>';
//send request
jQuery('<form action="' + url + '" method="post">' + input + '</form>').appendTo('body').submit().remove();
不錯。您將如何編寫或保存KML? – user1040259 2012-02-20 16:22:09
具體。你想保存到數據庫,寫入文件,還是強制瀏覽器啓動從KML字符串下載KML文件,上述函數返回? – capdragon 2012-02-20 16:33:27
感謝您的幫助。強制瀏覽器啓動KML的下載。 – user1040259 2012-02-20 16:36:13
下面是一些JQuery的行動拯救:
$('#saveKML').click(function() {
var kmlFormat = new OpenLayers.Format.KML();
var newWindow = window.open('',
'KML Export ' + (new Date()).getTime(), "width=300,height=300");
newWindow.document.write('<textarea id="kml" style="width: 100%; height: 100%">' +
kmlFormat.write(features) + '</textarea>');
});
,你會發現以前(2012)答案的語法不再適用。
這並不:
function GetKMLFromFeatures(features) {
var format = new ol.format.KML();
var kml = format.writeFeatures(features, {featureProjection: 'EPSG:3857'});
return kml;
}
function GetGeoJSONFromFeatures(features) {
var format = new ol.format.GeoJSON();
var geoJSON = format.writeFeatures(features, {featureProjection: 'EPSG:3857'});
return geoJSON;
}
function GetFeaturesFromLayer(layer) {
var source = layer.getSource();
var features = source.getFeatures();
return features;
}
- 1. Openlayers導出爲KML並保留我的地圖樣式
- 2. 將KML文件上傳到數據庫並顯示在OpenLayers地圖上
- 3. OpenLayers,Bing和KML
- 4. 谷歌地圖的OpenLayers KML圖標名稱
- 5. 如何從本地源向OpenLayers地圖添加KML?
- 6. 的OpenLayers:顯示遠程KML
- 7. XSLT如何根據描述值編輯KML地標風格
- 8. 使用OpenLayers保存策略而不向地圖添加圖層
- 9. OpenLayers谷歌地圖投影問題帶有KML
- 10. Openlayers 3:如何確保地圖渲染
- 11. openlayers kml與外部風格
- 12. 陣營本地地圖數據,並保存所有的項目
- 13. 根據視圖在定製的Google地圖上隱藏/顯示kml圖層
- 14. 如何在Android中的圖片上編寫文本並保存?
- 15. 下載並保存圖像根
- 16. Openlayers繪製功能:保存地圖狀態
- 17. 的OpenLayers保存功能
- 18. vb.net根據條件編輯文件並將其保存
- 19. 使用C#編寫KML - ScreenOverlay
- 20. 如何在Openlayers 3中創建KML時保留Polygon的樣式?
- 21. 存儲的地圖像谷歌地圖或OpenLayers
- 22. 谷歌地圖kml
- 23. OpenLayers打印地圖
- 24. 在Openlayers中加載爲Google地球生成的KML瓷磚
- 25. Openlayers中的KML層在本地主機上不起作用
- 26. Geomesa:在Accumulo中保存KML
- 27. 谷歌地圖API從KML圖層反向地理編碼
- 28. OpenLayers支持KML的範圍有多廣?
- 29. KML數據未在地圖上顯示?
- 30. 谷歌地圖顯示KML數據
這裏有一個類似的解決方案:可以幫助? http://gis.stackexchange.com/questions/17031/openlayers-format-kml-write-style – user1040259 2012-02-20 16:00:49