我在使用谷歌地图和 ashx 处理程序在地图上生成点时遇到问题。这真的很奇怪,因为我以前经常使用它们,但没有问题。
处理程序工作正常,但地图上不显示点。
所以生成的文件是:
<?xml version="1.0" encoding="utf-8"?>
<kml xmlns="http://www.opengis.net/kml/2.2">
<Document>
<Placemark>
<name>Bla</name>
<description><div class="ExternalClassEA84F87DED7745D594D3D4236376E796">dfdfg</div></description>
<Point>
<coordinates>19.8106635766601,41.3390789318785</coordinates>
</Point>
</Placemark>
</Document>
</kml>
注意:如果我在谷歌网站上传这个 kml 文件并使用那个 kml url 它工作正常:/
我用来创建通用处理程序的代码是:
System.Threading.Thread.CurrentThread.CurrentCulture = System.Globalization.CultureInfo.InvariantCulture;
context.Response.ContentType = "application/vnd.google-earth.kml+xml";
context.Response.AddHeader("Content-Disposition", "attachment; filename=Pikat.kml");
context.Response.Expires = -1;
XmlTextWriter kml = new XmlTextWriter(context.Response.OutputStream, System.Text.Encoding.UTF8);
kml.Formatting = Formatting.Indented;
kml.Indentation = 3;
kml.WriteStartDocument();
kml.WriteStartElement("kml", "http://www.opengis.net/kml/2.2");
kml.WriteStartElement("Document");
String place = String.Empty;
place = context.Request.QueryString["place"];
if (!String.IsNullOrEmpty(place))
{
#region Places Of Interests
SPListItemCollection places = this.GetPlacesOfInterest(context);
kml = GeneratePlacemarkElementForPlaceOfInterests(kml, places, context);
#endregion
}
private XmlTextWriter GeneratePlacemarkElementForPlaceOfInterests(XmlTextWriter kml, SPListItemCollection places, HttpContext context)
{
int i = 0;
if (places != null)
{
foreach (SPListItem item in places)
{
kml.WriteStartElement("Placemark");
kml.WriteAttributeString("id", String.Format("{0}", i + 1));
// kml.WriteElementString("id", String.Format("{0}", Cases.IndexOf(item) + 1));
kml.WriteElementString("name", item["Title"].ToString());
string HtmlDesc = String.Format(@"<Table width='300px'>
<tr><td><b>Titulli: </b></td><td>{0}</td></tr>
<tr><td><b>Kategoria: </b></td><td>{1}</td></tr>
<tr><td><b>Adresa:</b> </td><td>{2}</td></tr>
<tr><td><b>Pershkrimi:</b> </td><td>{3}</td></tr>
<tr><td><b>Orari:</b> </td><td>{4}</td></tr>
<tr><td><b>Me shume:</b> </td><td>{5}</td></tr>
</table><br/>",
item["Title"] == null ? "--" : item["Title"].ToString(),
item["Category"] == null ? "--" : item["Category"].ToString(),
item["Address"] == null ? "--" : item["Address"].ToString(),
item["Description"] == null ? "--" : item["Description"].ToString(),
item["HoursOfOperation"] == null ? "--" : item["HoursOfOperation"].ToString(),
item["SiteUrl"] == null ? "--" : item["SiteUrl"].ToString());
kml.WriteElementString("description", HtmlDesc);
// show the Geometry
kml.WriteStartElement("Point");
string x = item["Longitude"].ToString(); string y = item["Latitude"].ToString();
kml.WriteElementString("coordinates", String.Format("{0},{1}", x, y));
kml.WriteEndElement(); // </Point>
kml.WriteEndElement(); // </Placemark>
i++;
}
}
return kml;