Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/csharp/275.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/google-maps/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
C# 从SQL Server地理数据类型在Google地图上显示多边形_C#_Google Maps - Fatal编程技术网

C# 从SQL Server地理数据类型在Google地图上显示多边形

C# 从SQL Server地理数据类型在Google地图上显示多边形,c#,google-maps,C#,Google Maps,我有一个SQL Server 2008数据库,其中包含一列类型为geography的数据,用于存储澳大利亚各个地区的形状。我希望能够在谷歌地图上绘制这些形状 这是一个ASP.NET C网站 我已经搜索了如何进行此操作的任何示例,但找不到任何内容 有没有人有一些示例可以说明如何做到这一点,特别是使用SQL Server中的地理数据?我过去曾使用KML文件在网页上覆盖多边形 我建议读谷歌 创建一个从数据库中读取的函数 创建KML文件 从googleapi调用KML文件 虽然KML为您提供了一种快速简

我有一个SQL Server 2008数据库,其中包含一列类型为geography的数据,用于存储澳大利亚各个地区的形状。我希望能够在谷歌地图上绘制这些形状

这是一个ASP.NET C网站

我已经搜索了如何进行此操作的任何示例,但找不到任何内容


有没有人有一些示例可以说明如何做到这一点,特别是使用SQL Server中的地理数据?

我过去曾使用KML文件在网页上覆盖多边形

我建议读谷歌

创建一个从数据库中读取的函数 创建KML文件 从googleapi调用KML文件 虽然KML为您提供了一种快速简便的方式来覆盖形状,但谷歌确实对显示的项目数量进行了限制

下面的内容应该可以帮助您开始使用KML方法

public ActionResult Kml()
    {
        DataAccess da = new DataAccess();
        string cellColor = "0032FB";

        string kml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
        <kml xmlns=""http://earth.google.com/kml/2.1"">
            <Document>
                <Style id="polygon">
                    <LineStyle>
                        <color>FF" + cellColor + @"</color>
                    </LineStyle>
                    <PolyStyle>
                        <color>44" + cellColor +@"</color>
                        <fill>1</fill>
                        <outline>1</outline>
                    </PolyStyle> 
                </Style>
                <name>some name</name>
                <description>some des</description>

        ";
        DataTable polygons;

        foreach (DataRow polygon in polygons.Rows)
        {
                kml += @"
                    <Placemark>
                        <name>"somename @"</name>
                        <description><![CDATA[<p>some text</p>]]></description>" +
                        @"<styleUrl>#polygon</styleUrl>
                        <Polygon>
                            <extrude>1</extrude>
                            <altitudeMode>clampToSeaFloor</altitudeMode>
                            <outerBoundaryIs>
                                <LinearRing>
                                    <coordinates>" +
                                        polygon["Cell Limit  Longitude West"].ToString() + "," + polygon["Cell Limit Latitude North"].ToString() + " " +
                                        polygon["Cell Limit Longitude East"].ToString() + "," + polygon["Cell Limit Latitude North"].ToString() + " " +
                                        polygon["Cell Limit Longitude East"].ToString() + "," + polygon["Cell Limit Latitude South "].ToString() + " " +
                                        polygon["Cell Limit  Longitude West"].ToString() + "," + polygon["Cell Limit Latitude South "].ToString() + " " +
                                        polygon["Cell Limit  Longitude West"].ToString() + "," + polygon["Cell Limit Latitude North"].ToString() + " " +
                                    @"</coordinates>
                                </LinearRing>
                            </outerBoundaryIs>
                        </Polygon>
                    </Placemark>
                ";
        }

        kml += @"</Document>
        </kml>";
        byte[] data = Encoding.ASCII.GetBytes(kml);

        return File(data, "application/vnd.google-earth.kml+xml", id);
    }
谷歌缓存KML文件,因此添加Math.random可以解决这个问题


你也可以看看。然而,你必须上传你的数据到谷歌。此外,谷歌集团提供的数据。但是您需要SQL,因此此选项可能对您不可用。

来自AdamW的答案是正确的,但是没有解决SqlGeography数据格式中的数据

包括对Microsoft.SqlServer.Types的引用

SqlCommand cmd = new SqlCommand("SELECT STATEMENT",ConnectionString);
connectionString.Open();
SqlDataReader polygon = cmd.ExecuteReader();

While (polygon.read())
{
  string kmlCoordinates = string.Empty;
  SqlGeography geo = (SqlGeography)polygon["GeoColumn"];
  for(int i = 1; i <= geo.STNumPoints(); i++)
  {
       SqlGeography point = geo.STPointN(i);
       kmlCoordinates += point.Long + "," + point.Lat + " ";
  }
{

ConnectionString.Close();
注: 地理点是1索引而不是0索引,并且它也不是foreach友好型的

    public void KmlExport()
    {
        string cellColor = "COLOR";
        string KMLname = "KML NAME";
        string description = "KML DESCRIPTION";
        string kml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                        <kml xmlns=""http://www.opengis.net/kml/2.2"">
                            <Document>
                                <Style id=""polygon"">
                                    <LineStyle>
                                        <color>FF" + cellColor + @"</color>
                                    </LineStyle>
                                    <PolyStyle>
                                        <color>44" + cellColor + @"</color>
                                        <fill>1</fill>
                                        <outline>1</outline>
                                    </PolyStyle> 
                                </Style>
                                <name>" + KMLname + @"</name>
                                <description>" + description + "</description>";

        SqlCommand cmd = new SqlCommand("Select Statement", connectionString);
        cs.Open();
        SqlDataReader polygon = cmd.ExecuteReader();

        while (polygon.Read())
        {
            string kmlCoordinates = string.Empty;
            SqlGeography geo = (SqlGeography)polygon["GEOGRAPHY COLUMN"];

                for (int i = 1; i <= geo.STNumPoints(); i++)
                {
                    SqlGeography point = geo.STPointN(i);
                    kmlCoordinates += point.Long + "," + point.Lat + " ";
                }

                string polyName = polygon["Name Column"].ToString();
                string polyDescription = polygon["Description Column"].ToString();
                kml += @"
                <Placemark>
                    <name>" + polyName + @"</name>
                    <description><![CDATA[<p>" + polyDescription + "</p>]]></description>" +
                        @"<styleUrl>#polygon</styleUrl>
                    <Polygon>
                        <extrude>1</extrude>
                        <altitudeMode>clampToSeaFloor</altitudeMode>
                        <outerBoundaryIs>
                            <LinearRing>
                                <coordinates>" + kmlCoordinates +
                              @"</coordinates>
                            </LinearRing>
                        </outerBoundaryIs>
                    </Polygon>
                </Placemark>";

                kmlCoordinates = string.Empty;
            }

        cs.Close();
        kml += @"</Document></kml>";
        StreamWriter file = new StreamWriter(@"OUTPUTFILE.KML");
        file.WriteLine(kml);
        file.Close();

这是Adam W和Blair M的解决方案的组合。我修改了它,以便在数据库中有多个多边形时生成KML文件

我意识到这有点吹毛求疵——但你应该说明在这些答案中正确使用语句。特别是在这个用例中,我觉得不正确地处理对象可能会导致一些严重的内存泄漏。
    public void KmlExport()
    {
        string cellColor = "COLOR";
        string KMLname = "KML NAME";
        string description = "KML DESCRIPTION";
        string kml = @"<?xml version=""1.0"" encoding=""UTF-8""?>
                        <kml xmlns=""http://www.opengis.net/kml/2.2"">
                            <Document>
                                <Style id=""polygon"">
                                    <LineStyle>
                                        <color>FF" + cellColor + @"</color>
                                    </LineStyle>
                                    <PolyStyle>
                                        <color>44" + cellColor + @"</color>
                                        <fill>1</fill>
                                        <outline>1</outline>
                                    </PolyStyle> 
                                </Style>
                                <name>" + KMLname + @"</name>
                                <description>" + description + "</description>";

        SqlCommand cmd = new SqlCommand("Select Statement", connectionString);
        cs.Open();
        SqlDataReader polygon = cmd.ExecuteReader();

        while (polygon.Read())
        {
            string kmlCoordinates = string.Empty;
            SqlGeography geo = (SqlGeography)polygon["GEOGRAPHY COLUMN"];

                for (int i = 1; i <= geo.STNumPoints(); i++)
                {
                    SqlGeography point = geo.STPointN(i);
                    kmlCoordinates += point.Long + "," + point.Lat + " ";
                }

                string polyName = polygon["Name Column"].ToString();
                string polyDescription = polygon["Description Column"].ToString();
                kml += @"
                <Placemark>
                    <name>" + polyName + @"</name>
                    <description><![CDATA[<p>" + polyDescription + "</p>]]></description>" +
                        @"<styleUrl>#polygon</styleUrl>
                    <Polygon>
                        <extrude>1</extrude>
                        <altitudeMode>clampToSeaFloor</altitudeMode>
                        <outerBoundaryIs>
                            <LinearRing>
                                <coordinates>" + kmlCoordinates +
                              @"</coordinates>
                            </LinearRing>
                        </outerBoundaryIs>
                    </Polygon>
                </Placemark>";

                kmlCoordinates = string.Empty;
            }

        cs.Close();
        kml += @"</Document></kml>";
        StreamWriter file = new StreamWriter(@"OUTPUTFILE.KML");
        file.WriteLine(kml);
        file.Close();