2014-04-11 75 views
1

我已经在asp.net(C#)中编写了webapp,它从数据库获取坐标并创建.KML文件,然后将其放入Google地图并正常工作,即绘制线串,但我想将样式放入它即改变地点标记的样式,颜色,大小等。在网络上没有任何帮助。在c#中设计KML文件样式

代码:

using SharpKml.Base; 
using SharpKml.Dom; 
using SharpKml.Engine; 


    protected void Button1_Click(object sender, EventArgs e) 
    { 
     var document = new Document(); 
     document.Id = "null"; 
     document.Name = "null"; 
     LineString linestring = new LineString(); 
     CoordinateCollection coordinates = new CoordinateCollection(); 

     SqlConnection sqlcon = new SqlConnection(conStr); 
     // String com = "select Latitude, Longitude from Coordinates where [email protected]"; 
     SqlCommand sqlcom = new SqlCommand("GetLatLon", sqlcon); 
     sqlcom.CommandType = CommandType.StoredProcedure; 
     sqlcom.Parameters.Add("@IMEI", SqlDbType.VarChar).Value= TextBox1.Text; 

     DataTable dt = new DataTable(); 
     SqlDataAdapter sda = new SqlDataAdapter(sqlcom); 
     sda.Fill(dt); 

     try 
     { 
      sqlcon.Open(); 
      sqlcom.ExecuteNonQuery(); 

      foreach (DataRow dr in dt.Rows) 
      { 
       double lon = double.Parse(dr["Longitude"].ToString()); 
       double lat = double.Parse(dr["Latitude"].ToString()); 
       coordinates.Add(new Vector(lat, lon)); 
      } 

      linestring.Coordinates = coordinates; 
      Placemark placemark = new Placemark(); 
      placemark.Name = "ryan"; 
      placemark.Geometry = linestring; 
      document.AddFeature(placemark); 
      var kml = new Kml(); 
      kml.Feature = document; 
      kml.Feature = placemark; 
      KmlFile kmlFile = KmlFile.Create(kml, true); 
      using (var stream = System.IO.File.OpenWrite("C:/"+"IMEI-"+TextBox1.Text+".kml")) 
      { 
       kmlFile.Save(stream); 
       Response.Write("KML Created"); 
      } 

     } 
     catch (Exception exc) 
     { 
      Response.Write(exc.Message); 
     } 
     finally 
     { 
      sqlcon.Close(); 
     } 
    } 
} 

输出KML:

<?xml version="1.0" encoding="utf-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
    <Placemark> 
    <name>ryan</name> 
    <LineString> 
     <coordinates>9000,11 
71.5460372,34.0000941 
71.5460327,34.00009426 
71.54603299,34.000094272 
71.5460329,34.000094277 
71.54603299,34.000094277 
71.5460329,34.000094279 
71.54603299,34.000094279 
71.5460371,34.0000943 
71.5460372,34.0000943 
71.5460372,34.00009477 
71.5460372,34.00009486 
71.5460371,34.0000956 
71.5460371,34.0000959 
71.546037,34.000096 
71.546037,34.0000969 
71.5460375,34.0000981 
71.5460376,34.0000982 
71.5460378,34.0000986 
71.546038,34.000099</coordinates> 
    </LineString> 
    </Placemark> 
</kml> 

回答

0

作为开始,看看下面我的例子:

1.创建一个<文献>节点

2 .Add a <风格>节点在<styleUrl>节点内<标>

注相同的水平<标>节点

3.Reference的<样式>节点id属性(ID = yellowLineGreenPoly):一种有用的地方,以测试KML文件是 - http://display-kml.appspot.com/

<?xml version="1.0" encoding="utf-8"?> 
<kml xmlns="http://www.opengis.net/kml/2.2"> 
<Document> 
<Style id="yellowLineGreenPoly"> 
     <LineStyle> 
     <color>7f00ffff</color> 
     <width>4</width> 
     </LineStyle> 
     <PolyStyle> 
     <color>7f00ff00</color> 
     </PolyStyle> 
     <IconStyle> 
     <Icon> 
      <href>http://maps.google.com/mapfiles/kml/paddle/red-stars.png</href> 
     </Icon> 
     </IconStyle> 
</Style> 
<Placemark> 
    <name>stu</name> 
    <styleUrl>#yellowLineGreenPoly</styleUrl> 
    <Point> 
     <coordinates>71.5460372,34.0000941</coordinates> 
    </Point> 
</Placemark> 
<Placemark> 
    <name>ryan</name> 
    <styleUrl>#yellowLineGreenPoly</styleUrl> 
    <LineString> 
     <coordinates>9000,11 
71.5460372,34.0000941 
71.5460327,34.00009426 
71.54603299,34.000094272 
71.5460329,34.000094277 
71.54603299,34.000094277 
71.5460329,34.000094279 
71.54603299,34.000094279 
71.5460371,34.0000943 
71.5460372,34.0000943 
71.5460372,34.00009477 
71.5460372,34.00009486 
71.5460371,34.0000956 
71.5460371,34.0000959 
71.546037,34.000096 
71.546037,34.0000969 
71.5460375,34.0000981 
71.5460376,34.0000982 
71.5460378,34.0000986 
71.546038,34.000099</coordinates> 
    </LineString> 
    </Placemark> 
</Document> 
</kml> 
+0

兄弟,不是这样,不是直接编辑KML文件,而是通过C#代码 – user3518032

1

变化这样的工作对我来说

linestring.Coordinates = coordinates; 
    Placemark placemark = new Placemark(); 
    placemark.Name = "ryan"; 
    placemark.Geometry = linestring; 

    // Create Style with id 
    SharpKml.Dom.LineStyle lineStyle = new SharpKml.Dom.LineStyle(); 
    lineStyle.Color = Color32.Parse("ff0000ff"); 
    lineStyle.Width = 2; 

    Style roadStyle = new Style(); 
    roadStyle.Id = "RoadStyle"; 
    roadStyle.Line = lineStyle; 

    // Add style to document 
    document.AddStyle(roadStyle); 

    // Specify style for your placemark by url 
    placemark.StyleUrl = new Uri("#RoadStyle", UriKind.Relative); 

    document.AddFeature(placemark); 
+0

这是一个很好的小例子,谢谢。 –