Maps for WPF | ComponentOne
In This Topic
    GeoJSON Map
    In This Topic

    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. Using GeoJSON, you can bind geographical feature layers and point layers, and display rich geographical information from various sources.

    Maps for WPF 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\WPF\v6.0\Map\CS\MapExplorer\Resources and Documents\ComponentOne Samples\WPF\v4.6.2\C1.WPF.Maps\CS\MapsSamples\Resources location on your system) to read GeoJSON data.

    C#
    Copy Code
    VectorLayer _layerAirports;
    public MainWindow()
    {
        InitializeComponent();
        this.Loaded += MainWindow_Loaded;
    }
    
    private void MainWindow_Loaded(object sender, RoutedEventArgs e)
    {
        _layerAirports = ReadGeoJsonFromResource("airports.geojson", Brushes.LightGray, Brushes.Gray);
    
        c1Map1.Layers.Add(_layerAirports);
    }
    
    // Read GeoJson File
    VectorLayer ReadGeoJsonFromResource(string name, Brush fill, Brush stroke)
    {
        var stream = Assembly.GetExecutingAssembly().GetManifestResourceStream("WpfMapGeoJsonNet6.Resources." + name);
    
        var items = GeoJsonReader.Read(stream);
    
        var vl = new VectorLayer();
        vl.BeginUpdate();
        foreach (var item in items)
        {
            if (item != null)
            {
                item.Fill = fill;
                item.Stroke = stroke;
    
                if (item is VectorPlacemark)
                {
                    var lbl = ((System.Collections.IDictionary)item.Tag)["name_en"] as string;
                    if (!string.IsNullOrEmpty(lbl))
                    {
                        var pm = (VectorPlacemark)item;
                        pm.Geometry = new EllipseGeometry() { RadiusX = 3, RadiusY = 3 };
                        pm.ToolTip = pm.Label = lbl;
                        pm.LabelPosition = LabelPosition.Top;
                        vl.Children.Add(item);
                    }
                }
                else
                    vl.Children.Add(item);
            }
        }
        vl.LabelVisibility = LabelVisibility.AutoHide;
        vl.Foreground = Brushes.Yellow;
        vl.EndUpdate();
        return vl;
    }