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

时间:2012-04-03 04:58:47

标签: c# google-maps

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

这适用于ASP.NET C#网站。

我搜索了有关如何执行此操作的任何示例但找不到任何内容?

有没有人有一些如何执行此操作的示例,特别是使用SQL Server中的地理数据?

3 个答案:

答案 0 :(得分: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索引,并且它也不是预先友好的。

答案 1 :(得分:7)

我过去曾使用KML文件在网页上叠加多边形。

我建议阅读谷歌KML tutorials

  • 创建一个从数据库中读取的函数
  • 创建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);

Google缓存KML文件,因此添加Math.random()将解决这个问题。

您还可以查看Fusion Tables。但是,您必须将数据上传到Google。谷歌也将呈现的数据分组。但是你想要SQL,所以你可能无法使用这个选项。

答案 2 :(得分: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文件。