This project has moved and is read-only. For the latest updates, please go here.

styling KML files in c# asp.net

Apr 11, 2014 at 8:25 AM
i have written webapp in asp.net(C#) which takes coordinates from database and create .KML fole which i then put into Google maps and works fine i.e drawing line string but i want to put styles into it i.e. changing place mark styles, colors, size etc. There is no help regarding it on web.

Code:
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 IMEI=@txtIMEI";
        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();
        }
    }
}
output 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>
Apr 11, 2014 at 11:55 AM
help please.
Apr 12, 2014 at 9:44 AM
I'm not sure where your code is to create a style?

If you take a look at the style KML reference you should be able to translate that into the relevant method calls, using the AddStyle method of Document to add the style to the document and then setting the StyleUrl of the Placemark to the id of the style you created.

Hope that gets you going in the right direction,
Sam
Apr 12, 2014 at 11:01 AM
@samcragg: sir, I can't figure out the right way, Via asp.net how to get it correctly ?\