在C#中创建一个geoJson对象

本文关键字:一个 geoJson 对象 创建 | 更新日期: 2023-09-27 17:59:45

am试图通过将ONLY lat和long传递给一个函数来创建GeoJson FeatureCollection对象,该函数在其中实例化Below PoCo。

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对象的任何建议。

在C#中创建一个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";