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

16

我有一个SQL Server 2008数据库,其中包含一个存储澳大利亚各个区域形状的地理类型列。我想要在Google Maps上绘制这些形状。

这是用于ASP.NET C#网站的。

我已经搜索了如何实现此操作的示例,但没有找到任何相关内容?

是否有人有使用来自SQL Server的地理数据进行此操作的示例?

3个回答

11

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循环。


1
我意识到这有点苛刻,但你应该在回答中说明USING语句的正确使用方法,特别是在这种用例中,我觉得不正确地处理对象可能会导致一些重大的内存泄漏问题。 - Wjdavis5

7
我过去曾经使用KML文件在网页上叠加多边形。
我建议阅读Google的KML教程
  • 创建一个从数据库中读取数据的函数
  • 创建KML文件
  • 从Google API调用KML文件
尽管KML为您提供了一种快速简便的方式来叠加形状,但Google对显示项目数量有限制。
以下内容应该能够帮助您开始使用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);
    }

JavaScript

var url = 'http://www.example.com/AppName/GMap/file.kml &rand=' + Math.random();

layer_paperCharts = new google.maps.KmlLayer(url);

if (loadedonce) {
  layer_paperCharts.set('preserveViewport', true);
} else {
  loadedonce = true;
}

layer_paperCharts.setMap(map);

谷歌缓存KML文件,因此添加Math.random()将解决此问题。
您也可以查看Fusion Tables。但是,您必须将数据上传到谷歌。此外,谷歌会对呈现的数据进行分组。但是,如果您需要SQL,则可能无法使用此选项。

7
    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文件。


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接