我有以下kml文件:
<?xml version="1.0" encoding="UTF-8"?>
<kml xmlns="http://earth.google.com/kml/2.x">
<Placemark>
<name>My Home</name>
<description>Here is the place where I live</description>
<Point>
<coordinates>-122.0822035425683,37.42228990140251,0</coordinates>
</Point>
</Placemark>
我想在上面的文件中解析地标的经度和纬度。
答案 0 :(得分:0)
我感觉到你的痛苦......我花了很长时间来实现这个目的。以下是从.kml文件中解析纬度和经度值的快速示例:
从存储纬度和经度值的地方开始:
public class Location
{
public float Latitude { get; set; }
public float Longitude { get; set; }
}
然后从.kml文件中获取流(在我的情况下,我一直使用相同的.kml文件,并将其放在我的项目中,其构建操作设置为&#39; Embedded Resource&#39;:< / p>
Stream stream = GetType().Assembly.GetManifestResourceStream("MyNamespace.Filename.kml");
List<Location> locations = ParseLocations(stream);
以下是ParseLocations的实现:
public static List<Location> ParseLocations(Stream stream)
{
List<Location> locationList = new List<Location>();
var doc = XDocument.Load(stream);
XNamespace ns = "http://earth.google.com/kml/2.1";
var result = doc.Root.Descendants(ns + "Placemark");
foreach (XElement xmlTempleInfo in result)
{
var point = xmlTempleInfo.Element(ns + "Point");
string[] coordinates = point.Element(ns + "coordinates").Value.Split(",".ToCharArray());
locationList.Add(new Location()
{
Latitude = float.Parse(coordinates[1])
Longitude = float.Parse(coordinates[0]),
});
}
return locationList;
}