在C#中创建一个geoJson对象

时间:2016-08-02 21:47:47

标签: c# geojson

我试图通过将只有lat和long传递给它实例化在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对象。

1 个答案:

答案 0 :(得分:3)

以下作业无效 -

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";