データベースから座標を取得して .KML を作成する asp.net(C#) で webapp を作成しました。これを Google マップに入れて正常に動作します。つまり、線の文字列を描画しますが、スタイルを入れたい、つまり場所マークのスタイル、色を変更します。 、サイズなどweb上では仕方ありません。
コード:
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();
}
}
}
出力 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>