use*_*032 6 c# asp.net google-maps c#-4.0 sharpkml
我已经在 asp.net(C#) 中编写了 webapp,它从数据库中获取坐标并创建 .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 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>
首先,请查看下面我的示例:
1.创建<Document>节点
2.添加与<Placemark>节点同级的<Style>节点
3.引用<Placemark>内<styleUrl>节点中的<Style>节点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>
| 归档时间: | 
 | 
| 查看次数: | 4944 次 | 
| 最近记录: |