我尝试通过仅将纬度和经度传递给在 PoCo 下面实例化的函数来创建 GeoJson FeatureCollection 对象。
namespace PoCo
{
public class LocalGeometry
{
public string type { get; set; }
public List<double> coordinates { get; set; }
}
public class Properties
{
public string name { get; set; }
public string address { get; set; }
public string id { get; set; }
}
public class LocalFeature
{
public string type { get; set; }
public LocalGeometry geometry { get; set; }
public Properties properties { get; set; }
}
public class geoJson
{
public string type { get; set; }
public List<LocalFeature> features { get; set; }
}
}
这就是创建对象的方式
var CorOrd = new LocalGeometry();
CorOrd.coordinates.Add(Lat);
CorOrd.coordinates.Add(Lang);
CorOrd.type = "Point";
var geoJson = new geoJson
{
type = "FeatureCollection",
features = new LocalFeature
{
type = "Feature",
geometry = CorOrd
}
};
但我收到错误
CS0029 Cannot implicitly convert type 'PoCo' to 'System.Collections.Generic.List<PoCo.Local>'.
关于如何在此处创建 GeoJson 对象的任何建议。
最佳答案
以下分配无效 -
功能 = 新的 LocalFeature
它应该是一个LocalFeature列表 -
features = new List<LocalFeature>
{
new LocalFeature { type = "Feature", geometry = CorOrd}
}
此外,您需要在添加之前实例化一个列表。否则,它将抛出NullReferenceException。
ar CorOrd = new LocalGeometry();
CorOrd.coordinates = new List<double>(); // <=====
CorOrd.coordinates.Add(Lat);
CorOrd.coordinates.Add(Lang);
CorOrd.type = "Point";
关于c# - 在 C# 中创建 geoJson 对象,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38731047/