2016-08-02 18 views
2

Ich versuche, ein GeoJson FeatureCollection-Objekt zu erstellen, indem NUR lat und long an eine Funktion übergeben wird, in der es Unten PoCo instanziiert.Erstellen eines geoJson-Objekts in C#

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

} 

Dies ist, wie Objekt

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

Schaffung ist aber sind immer Fehler

CS0029 Cannot implicitly convert type 'PoCo' to 'System.Collections.Generic.List<PoCo.Local>'.

Irgendwelche Vorschläge, wie ich ein GeoJSON Objekt hier schaffen kann.

+0

@Win Vielen Dank, es ist ein großer Gewinn! –

Antwort

3

folgende Zuordnung ist nicht gültig -

features = new LocalFeature

Es sollte LocalFeature eine Liste sein -

features = new List<LocalFeature> 
{ 
    new LocalFeature { type = "Feature", geometry = CorOrd} 
} 

Darüber hinaus müssen Sie eine Liste instanziiert vor hinzufügen. Andernfalls wird NullReferenceException geworfen.

ar CorOrd = new LocalGeometry(); 
CorOrd.coordinates = new List<double>(); // <===== 
CorOrd.coordinates.Add(Lat); 
CorOrd.coordinates.Add(Lang); 
CorOrd.type = "Point"; 
+1

Sie können auch Eigenschaftsinitialisierungen verwenden, um sie mit 'new LocalGeometry {coordinates = new List {Lat, Long}, type =" Point "}' zu initialisieren – mythz