2012-02-20 62 views

回答

9

只能載體功能導出爲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(); 
+0

不錯。您將如何編寫或保存KML? – user1040259 2012-02-20 16:22:09

+0

具體。你想保存到數據庫,寫入文件,還是強制瀏覽器啓動從KML字符串下載KML文件,上述函數返回? – capdragon 2012-02-20 16:33:27

+0

感謝您的幫助。強制瀏覽器啓動KML的下載。 – user1040259 2012-02-20 16:36:13

3

下面是一些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>'); 
}); 
0

,你會發現以前(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; 
     }