从SQL Server地理数据类型在谷歌地图上显示多边形
本文关键字:谷歌地图 显示 多边形 数据类型 SQL Server | 更新日期: 2023-09-27 18:30:00
我有一个SQL Server 2008数据库,其中有一列类型为geography的列,存储了澳大利亚各个地区的形状。我想能够在谷歌地图上画出这些形状。
这是针对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教程
- 创建一个从数据库中读取的函数
- 创建KML文件
- 从Google API调用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);
}
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。但是你必须上传你的数据到谷歌。此外,谷歌还对所提供的数据进行了分组。但是您需要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文件。