我是asp.net的新手,我不了解如何使用Generic Handler文件生成kml文件。我能够创建一个处理程序文件并混淆在何处以及如何连接数据库并获取纬度和经度并以kml定义它并且应该能够生成kml。
我有一个像pmis_gpspoints
这样的数据库表,其中我有p_latitude,p_longitude,p_cd
列。
我会将p_cd
值传递给所需kml文件的通用处理程序文件。
任何人都可以帮我解决任何代码片段或想法或参考资料,以便我可以解决这个问题吗?
答案 0 :(得分:0)
尝试将所有代码放在Process Request中。
最初我们从查询字符串中检索XMLTextWriter
。
然后将连接到数据库并获得所需的gps点。
然后我们将使用public void ProcessRequest(HttpContext context)
{
//context.Response.ContentType = "text/plain";
context.Response.ContentType = "application/vnd.google-earth.kml+xml";
string id = context.Request.QueryString["ID"].ToString();
FileName = "KML File";
context.Response.AddHeader("Content-Disposition", "attachment; filename=" + FileName + ".kml");
Query = "select * from pmis_hh_gpspoints where p_cd='" + id + "' order by p_gps_cd ";
SqlConnection conn = new SqlConnection(conStr);
conn.Open();
SqlDataAdapter da = new SqlDataAdapter(Query, conn);
DataSet ds = new DataSet();
da.Fill(ds);
XmlTextWriter kml = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
kml.Formatting = Formatting.Indented;
kml.Indentation = 3;
kml.WriteStartDocument();
kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2"); /*1*/
kml.WriteStartElement("Document"); /*2*/
kml.WriteStartElement("Style"); /*3*/
kml.WriteAttributeString("id", "bsr");
kml.WriteStartElement("LineStyle"); /*4*/
kml.WriteElementString("width", "4");
kml.WriteElementString("color", "ff0000ff");
kml.WriteEndElement(); /*-4-*/
kml.WriteStartElement("PolyStyle"); /*5*/
kml.WriteElementString("color", "51400FF");
kml.WriteEndElement(); /*-5-*/
kml.WriteEndElement(); /*-3-*/
kml.WriteStartElement("Placemark"); /*6*/
kml.WriteElementString("styleUrl", "#bsr");
kml.WriteStartElement("Polygon"); /*7*/
kml.WriteElementString("extrude", "1");
kml.WriteElementString("tessellate", "1");
kml.WriteElementString("altitudeMode", "ALTITUDE_CLAMP_TO_GROUND");
kml.WriteStartElement("outerBoundaryIs"); /*8*/
kml.WriteStartElement("LinearRing"); /*9*/
kml.WriteStartElement("coordinates"); /*10*/
for (int i = 0; i <= ds.Tables[0].Rows.Count - 1; i++)
{
kml.WriteValue("" + ds.Tables[0].Rows[i]["p_longitude"].ToString() + "," + ds.Tables[0].Rows[i]["p_latitude"].ToString() + ",0 ");
}
kml.WriteValue("" + ds.Tables[0].Rows[0]["p_longitude"].ToString() + "," + ds.Tables[0].Rows[0]["p_latitude"].ToString() + ",0 ");
kml.WriteEndElement(); /*-10-*/
kml.WriteEndElement(); /*-9-*/
kml.WriteEndElement(); /*-8-*/
kml.WriteEndElement(); /*-7-*/
kml.WriteEndElement(); /*-6-*/
kml.WriteEndElement(); /*-2-*/
kml.WriteEndElement(); /*-1-*/
kml.WriteEndDocument();
kml.Close();
conn.Close();
}
编写kml。
GWT DevMode
使用正确的参数引用处理程序文件,无论您在哪里尝试显示它是否在网格中或任何其他位置......使用代码中提到的文件名下载kml文件。