我有一个SQL Server 2008数据库,其中包含一个存储各种澳大利亚地区形状的地理类型列。我希望能够在Google地图上绘制这些形状。从谷歌地图在SQL Server地理数据类型上显示多边形
这是一个ASP.NET C#网站。
我已经搜索了如何做到这一点的任何样本,但找不到任何东西?
有没有人有一些如何做到这一点的示例,特别是使用SQL Server的地理数据?
我有一个SQL Server 2008数据库,其中包含一个存储各种澳大利亚地区形状的地理类型列。我希望能够在Google地图上绘制这些形状。从谷歌地图在SQL Server地理数据类型上显示多边形
这是一个ASP.NET C#网站。
我已经搜索了如何做到这一点的任何样本,但找不到任何东西?
有没有人有一些如何做到这一点的示例,特别是使用SQL Server的地理数据?
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要么。
我过去曾经使用KML文件在网页上叠加多边形。
我建议阅读网上搜寻KML tutorials
虽然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,所以这个选项可能不适用于你。
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文件。
我意识到这有点挑剔 - 但你应该在这些答案中说明正确使用USING语句。特别是在这个用例中,我觉得没有妥善处理你的对象可能会导致一些显着的内存泄漏。 – Wjdavis5 2015-12-29 16:29:17