2012-02-20 7 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文字列をダウンロードするには、ブラウザを強制するためには、ダウンロードするファイルとして。

あなたはサーバー側で使用している言語/プラットフォーム/などを指定していませんが、これはC#で行ったことです。

私は、テキスト領域のフォームからクエリ文字列と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の側から私は単純にダウンロードを開始するには、これを呼び出す:

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

+0

具体的にしてください。上記の関数が返すKML文字列からデータベースへの保存、ファイルへの書き込み、またはブラウザによるKMLファイルのダウンロードの開始を強制しますか? – capdragon

+0

ご協力ありがとうございます。ブラウザがKMLのダウンロードを開始するように強制します。 – user1040259

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

あなたはOpenLayersを3または4を使用している場合、あなたは見つけるでしょう以前の(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; 
     } 
関連する問題