在 C# 中创建一个 geoJson 对象

Create a geoJson object in C#

我正在尝试通过仅将纬度和经度传递给在 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 对象的任何建议。

以下分配无效 -

features = new 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";