GeoJSON is an open standard geospatial data interchange format for encoding a variety of geographic data structures (i.e. countries, provinces, cities, etc.) and can include other non-spatial data related to these features. It encodes a variety of geographic data structures that support various geometry types such as Point, LineString, Polygon etc. These geometry types are implemented in Maps using GeoGeometryType enumeration. Using GeoJSON, you can bind geographical feature layers and point layers, and display rich geographical information from various sources.
Map for WinForms allows you to read GeoJSON objects and data using the GeoJSONReader class. This class represents the reader to read GeoJSON data and provides the Read method that can be used to read GeoJSON data from the specified stream.
The following code showcases how to read GeoJSON data using Read method of the GeoJSONReader class. This example uses airports.geojson file from the MapExplorer sample (available at Documents\ComponentOne Samples\WinForms\v6.0\Map\CS\MapExplorer\Resources location on your system) to read GeoJSON data.
C# |
Copy Code
|
---|---|
public partial class Form1 : Form { C1.Win.Map.VectorLayer layer; private VectorLayer _layerAirports; public Form1() { InitializeComponent(); c1Map1.TileLayer.TileSource = new VirtualEarthAerialSource(); //create a vector layer and add it to the map layer = new C1.Win.Map.VectorLayer(); c1Map1.Layers.Add(layer); this.Load += Form1_Load; } private void Form1_Load(object? sender, EventArgs e) { _layerAirports = ReadGeoJsonFromResource("airports.geojson", Color.LightGray, Color.Gray); c1Map1.BeginUpdate(); c1Map1.Layers.Add(_layerAirports); c1Map1.EndUpdate(); c1Map1.ZoomToItems(_layerAirports.Items); } // Create vector private static VectorItem CreateVector(C1.FlexMap.GeoGeometryRecord record) { VectorItem vector = null; switch (record.Geometry?.Type) { case C1.FlexMap.GeoGeometryType.Point: case C1.FlexMap.GeoGeometryType.MultiPoint: C1.Win.Map.VectorPlacemark p = new C1.Win.Map.VectorPlacemark(); p.Marker.Shape = C1.FlexMap.MarkerShape.Circle; p.Marker.Size = new SizeF(6, 6); p.Marker.LabelPosition = C1.FlexMap.LabelPosition.Top; p.Tag = p.Marker.Caption = record.Data["name_en"]?.ToString(); vector = p; break; case C1.FlexMap.GeoGeometryType.Polygon: case C1.FlexMap.GeoGeometryType.MultiPolygon: vector = new C1.Win.Map.VectorPolygon(); break; case C1.FlexMap.GeoGeometryType.LineString: case C1.FlexMap.GeoGeometryType.MultiLineString: vector = new VectorPolyline(); break; default: return null; } vector.Geometry = record.Geometry; return vector; } // Read GeoJson File private C1.Win.Map.VectorLayer ReadGeoJsonFromResource(string name, Color fill, Color stroke) { var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("MapGeoJsonNet6.Resources." + name); //Read GeoJSON data from stream var records = C1.FlexMap.GeoJsonReader.Read(stream); var items = records.Select(record => CreateVector(record)); var vl = new VectorLayer(); foreach (var item in items) { if (item != null) { item.Style.BackColor = fill; item.Style.Stroke.Color = stroke; vl.Items.Add(item); } } vl.LabelVisibility = C1.FlexMap.LabelVisibility.AutoHide; vl.LabelStyle.ForeColor = Color.Yellow; return vl; } |