如何在将点从 sqlserver 获取到 asp.net 时通过处理程序生成 kml

How to Generate a kml through a handler while getting the points from sqlserver into a asp.net

我是 asp.net 的新手,我不了解如何使用通用处理程序文件生成 kml 文件。我能够创建一个处理程序文件并混淆了在何处以及如何连接数据库并获取纬度和经度并在 kml 中定义它并且应该能够生成 kml。

我有一个像 pmis_gpspoints 这样的数据库 table,其中我有 p_latitude,p_longitude,p_cd.

我会将 p_cd 值传递给所需 kml 文件的通用处理程序文件。

任何人请帮助我提供任何代码片段或想法或参考,以便我可以解决?

尝试将所有代码放在处理请求中。

最初我们从查询字符串中检索 p_cd

然后将连接到数据库并获取所需的 gps 点。

然后我们将使用 XMLTextWriter.

编写 kml
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 文件都会使用代码中提到的文件名下载。