2012-04-03 113 views
14

我有一個SQL Server 2008數據庫,其中包含一個存儲各種澳大利亞地區形狀的地理類型列。我希望能夠在Google地圖上繪製這些形狀。從谷歌地圖在SQL Server地理數據類型上顯示多邊形

這是一個ASP.NET C#網站。

我已經搜索瞭如何做到這一點的任何樣本,但找不到任何東西?

有沒有人有一些如何做到這一點的示例,特別是使用SQL Server的地理數據?

回答

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要麼。

+0

我意識到這有點挑剔 - 但你應該在這些答案中說明正確使用USING語句。特別是在這個用例中,我覺得沒有妥善處理你的對象可能會導致一些顯着的內存泄漏。 – Wjdavis5 2015-12-29 16:29:17

7

我過去曾經使用KML文件在網頁上疊加多邊形。

我建議閱讀網上搜尋KML tutorials

  • 創建一個函數從數據庫中讀取
  • 創建KML從谷歌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 [email protected]"</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。但是,您必須將您的數據上傳到Google。 Google也對提供的數據進行分組。但是你需要SQL,所以這個選項可能不適用於你。

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