C#实现KML生成(支持点、线、面接口):
点击下载:接口和代码下载地址
using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Threading.Tasks;
using System.Xml;
using System.Xml.Serialization;
using System.IO;
namespace GenerateKml
{
[XmlRoot("kml", Namespace = "http://www.opengis.net/kml/2.2")]
public class Kml
{
[XmlElement("Document")]
public KmlDocument document { get; set; }
// constructor
public Kml()
{
}
public Kml(KmlDocument doc)
{
document = doc;
}
public void GenerateKmlFile(string KmlFileName)
{
using (FileStream fs = new FileStream(KmlFileName, FileMode.Create, FileAccess.ReadWrite))
{
using (StreamWriter sw = new StreamWriter(fs, System.Text.Encoding.UTF8))
{
XmlSerializer serializer = new XmlSerializer(this.GetType());
serializer.Serialize(sw, this);
}
}
}
/// <summary>
/// coordinates lon,lat,alt 经纬高度多个用;隔开
/// </summary>
public class dataFormat
{
public string type { get; set; }
public string coordinates { get; set; }
public string name { get; set; }
public string description { get; set; }
public string styleUrl { get; set; }
}
/// <summary>
/// 调用此方法
/// </summary>
/// <param name="xmlname">xml名称</param>
/// <param name="filename">导出文件名</param>
/// <param name="lst">数据</param>
/// <returns></returns>
public static int CreateKml(string xmlname,string filename,List<dataFormat> lst)
{
List<KmlPlacemark> ls = new List<KmlPlacemark>();
foreach (dataFormat d in lst)
{
string type = d.type;
string coords = d.coordinates.Trim();
string styleUrl = d.styleUrl;
string name = d.name;
if (string.IsNullOrEmpty(name))
{
name = "";
}
string description = d.description;
if (string.IsNullOrEmpty(description))
{
description = "";
}
if (type == "point")
{
if (string.IsNullOrEmpty(styleUrl))
{
styleUrl = "#downArrowIcon";
}
KmlPoint p = new KmlPoint(coords);
KmlPlacemark placemark = new KmlPlacemark(name, description, styleUrl, p, null, null);
ls.Add(placemark);
}
else if (type == "line")
{
if (string.IsNullOrEmpty(styleUrl))
{
styleUrl = "#blue";
}
KmlLineString line = new KmlLineString(coords.Split(';').ToList());
KmlPlacemark placemark = new KmlPlacemark(name, description, styleUrl, null, line, null);
ls.Add(placemark);
}
else if (type == "polygon")
{
if (string.IsNullOrEmpty(styleUrl))
{
styleUrl = "#blue";
}
KmlPolygon polygon = new KmlPolygon(coords.Split(';').ToList());
KmlPlacemark placemark = new KmlPlacemark(name, description, styleUrl, null, null, polygon);
ls.Add(placemark);
}
}
try
{
KmlDocument document = new KmlDocument(xmlname, filename, ls);
Kml kml = new Kml(document);
kml.GenerateKmlFile("a.kml");
return 1;
}
catch (Exception e)
{
return 0;
}
}
//测试
public static void Test()
{
List<dataFormat> l = new List<dataFormat>();
dataFormat d = new dataFormat();
d.name = "点";
d.type = "point";
d.styleUrl = "#downArrowIcon";
d.coordinates = "116, 40, 0";
d.description = "点的描述";
l.Add(d);
dataFormat d1 = new dataFormat();
d1.name = "线";
d1.type = "line";
d1.styleUrl = "#blue";
d1.coordinates = "116,40,0;116,39,0;116,36,0";
d1.description = "线的描述";
l.Add(d1);
dataFormat d3 = new dataFormat();
d3.name = "面";
d3.type = "polygon";
d3.styleUrl = "#poly1";
d3.coordinates = "-122.0857412771483,37.42227033155257,17;-122.0858169768481,37.42231408832346,17;-122.085852582875,37.42230337469744,17;-122.0858799945639,37.42225686138789,17;-122.0858860101409,37.4222311076138,17;-122.0858069157288,37.42220250173855,17;-122.0858379542653,37.42214027058678,17;-122.0856732640519,37.42208690214408,17;-122.0856022926407,37.42214885429042,17;-122.0855902778436,37.422128290487,17;-122.0855841672237,37.42208171967246,17;-122.0854852065741,37.42210455874995,17;-122.0855067264352,37.42214267949824,17;-122.0854430712915,37.42212783846172,17;-122.0850990714904,37.42251282407603,17;-122.0856769818632,37.42281815323651,17;-122.0860162273783,37.42244918858722,17;-122.0857260327004,37.42229239604253,17;-122.0857412771483,37.42227033155257,17";
d3.description = "面的描述";
l.Add(d3);
CreateKml("测试","a",l);
}
}
}
如果需要点、线、面kml的接口,请私信我。