Populate Data in Blazor Maps Component
15 Dec 202214 minutes to read
This section explains how to populate data inputs and provide it to the Maps component.
Geometry types
GeoJSON data contains geometry objects with properties such as geometry types and coordinates. The geometry types are the values present in the geometry objects of the GeoJSON data that specify the type of shape to be rendered, as well as the coordinates that help to draw the shape’s boundary line. The supportive geometry types are:
Shapes | Supported |
---|---|
Polygon | Yes |
MultiPolygon | Yes |
LineString | Yes |
MultiLineString | Yes |
Point | Yes |
MultiPoint | Yes |
GeometryCollection | Yes |
Shape data
The shape data collection describes geographical shape information that is available in GeoJSON format. The Map shapes are rendered with this data. The custom shapes such as seat selection in bus, seat selection in a cricket stadium and more useful information can be also added as ShapeData in the MapsLayer.
Data source
The DataSource property is used to represent statistical data in the Maps component, and it accepts a collection of values as input. For example, a list of objects as input can be provided to the data source. This data source will be used to color the map, display data labels, and display tooltip, among other things.
The data source is populated with list of objects relative to shape data. In the following example, PopulationDetails can be used as data source in Maps.
@code{
public class PopulationDetail
{
public string Code { get; set; }
public double Value { get; set; }
public string Name { get; set; }
public double Population { get; set; }
public double Density { get; set; }
};
private List<PopulationDetail> PopulationDetails = new List<PopulationDetail> {
new PopulationDetail {
Code = "AF",
Value= 53,
Name= "Afghanistan",
Population= 29863010,
Density= 119
},
new PopulationDetail {
Code= "AL",
Value= 117,
Name= "Albania",
Population= 3195000,
Density= 111
},
new PopulationDetail {
Code= "DZ",
Value= 15,
Name= "Algeria",
Population= 34895000,
Density= 15
}
};
}
Data binding
The following properties in the MapsLayer are used for binding data in the Maps component. Both the properties are related to each other.
- ShapePropertyPath
- ShapeDataPath
ShapePropertyPath
The ShapePropertyPath property is used to refer the field name in the ShapeData property of shape layers to identify the shape. When the values of ShapeDataPath property from the DataSource property and ShapePropertyPath property from the ShapeData property match, then the associated object from the data source is bound to the corresponding shape.
@using Syncfusion.Blazor.Maps
<SfMaps>
<MapsLayers>
<MapsLayer ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}'
ShapePropertyPath='new string[] {"name"}' TValue="string">
</MapsLayer>
</MapsLayers>
</SfMaps>
NOTE
world-map.json
file contains following data and its field name value is used to map the corresponding shape with the provided data source.
[
{
"type": "Feature",
"properties": {
"admin": "Afghanistan",
"name": "Afghanistan",
"continent": "Asia"
},
"geometry": { "type": "Polygon", "coordinates": [[[61.21081709172573, ...
},
...
]
ShapeDataPath
The ShapeDataPath property is similar to the ShapePropertyPath property, but it refers to the field name in the DataSource property. For example, following population data contains field Name, Population and Density. Here the Name field is set to the ShapeDataPath to map the corresponding value of field name in shape data.
@using Syncfusion.Blazor.Maps
<SfMaps>
<MapsLayers>
<MapsLayer ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}'
DataSource="PopulationDetails" ShapePropertyPath='new string[] {"name"}'
ShapeDataPath="Name" TValue="PopulationDetail">
</MapsLayer>
</MapsLayers>
</SfMaps>
@code{
public class PopulationDetail
{
public string Name { get; set; }
public double Population { get; set; }
public double Density { get; set; }
};
private List<PopulationDetail> PopulationDetails = new List<PopulationDetail> {
new PopulationDetail {
Name= "Afghanistan",
Population= 29863010,
Density= 119
},
...
};
}
In the above example, both name fields contain the same value as Afghanistan, this value is matched in both shape data and data source, so that the details associated with Afghanistan will be mapped to the corresponding shape and used to color the corresponding shape, display data labels, display tooltips, and more.
@using Syncfusion.Blazor.Maps
<SfMaps>
<MapsLayers>
<MapsLayer ShapeData='new {dataOptions= "https://cdn.syncfusion.com/maps/map-data/world-map.json"}'
DataSource="PopulationDetails"
ShapeDataPath="Name"
ShapePropertyPath='new string[] {"name"}' TValue="PopulationDetail">
@* It display data label for bounded items *@
<MapsDataLabelSettings Visible="true" LabelPath="Name"></MapsDataLabelSettings>
</MapsLayer>
</MapsLayers>
</SfMaps>
@code{
public class PopulationDetail
{
public string Code { get; set; }
public double Value { get; set; }
public string Name { get; set; }
public double Population { get; set; }
public double Density { get; set; }
};
private List<PopulationDetail> PopulationDetails = new List<PopulationDetail> {
new PopulationDetail {
Code = "AF",
Value= 53,
Name= "Afghanistan",
Population= 29863010,
Density= 119
},
new PopulationDetail {
Code= "AL",
Value= 117,
Name= "Albania",
Population= 3195000,
Density= 111
},
new PopulationDetail {
Code= "DZ",
Value= 15,
Name= "Algeria",
Population= 34895000,
Density= 15
}
};
}
Fetching data from JSON file
To retrieve data from a JSON file, you can create a Blazor WebAssembly App or a Blazor Server App. The data from the JSON file is then read, convert it to a C# object, and assign it to the DataSource property.
Fetching data from JSON file using Blazor WebAssembly App
The Http.GetFromJsonAsync is used in the OnInitializedAsync lifecycle method to load JSON file data. As this will be executed asynchronously, check whether populationDensity is available, render the Maps component, or display the loading statement.
@inject HttpClient Http;
@using Syncfusion.Blazor.Maps
@if (PopulationDensity == null)
{
<p><em>Loading Maps component...</em></p>
}
else
{
<SfMaps>
<MapsLayers>
<MapsLayer ShapeData='new {dataOptions ="https://cdn.syncfusion.com/maps/map-data/world-map.json"}'
DataSource="PopulationDensity"
ShapeDataPath="Name"
ShapePropertyPath='new string[] {"name"}' TValue="PopulationData">
<MapsDataLabelSettings Visible="true" LabelPath="Name"></MapsDataLabelSettings>
</MapsLayer>
</MapsLayers>
</SfMaps>
}
@code{
PopulationData[] PopulationDensity;
protected override async Task OnInitializedAsync()
{
PopulationDensity = await Http.GetFromJsonAsync<PopulationData[]>("sample-data/PopulationDensity.json");
}
public class PopulationData
{
public string Code { get; set; }
public double Value { get; set; }
public string Name { get; set; }
public double Population { get; set; }
public float Density { get; set; }
}
}
Here, the PopulationDensity.json
file contains following data.
[
{
"code": "AF",
"value": 53,
"name": "Afghanistan",
"population": 29863010,
"density": 119
},
{
"code": "AL",
"value": 117,
"name": "Albania",
"population": 3195000,
"density": 111
},
{
"code": "DZ",
"value": 15,
"name": "Algeria",
"population": 34895000,
"density": 15
}
]
Fetching data from JSON file using Blazor Server App
The Http.GetAsync is used in the OnInitializedAsync lifecycle method to get the JSON file data and read the JSON file as a string. Then, the GeoJSON data in the string can be converted as a deserialized object list and set in the DataSource property of the Maps component. As this will be executed asynchronously, check whether populationDensity is available, render the Maps component, or display the loading statement.
@inject HttpClient Http;
@using Syncfusion.Blazor.Maps
@using System.Net.Http.Json
@using System.Text.Json;
@using Newtonsoft.Json;
@inject NavigationManager NavigationManager
@if (PopulationDensity == null)
{
<p><em>Loading Maps component...</em></p>
}
else
{
<SfMaps>
<MapsLayers>
<MapsLayer ShapeData='new {dataOptions ="https://cdn.syncfusion.com/maps/map-data/world-map.json"}'
DataSource="PopulationDensity"
ShapeDataPath="Name"
ShapePropertyPath='new string[] {"name"}' TValue="PopulationData">
<MapsDataLabelSettings Visible="true" LabelPath="Name"></MapsDataLabelSettings>
</MapsLayer>
</MapsLayers>
</SfMaps>
}
@code{
public List<PopulationData> PopulationDensity;
public string result;
protected override async Task OnInitializedAsync()
{
string path = NavigationManager.Uri + "sample-data/PopulationDensity.json";
HttpClient httpClient = new HttpClient();
HttpResponseMessage response = await Http.GetAsync(path);
result = response.Content.ReadAsStringAsync().Result;
PopulationDensity = JsonConvert.DeserializeObject<List<PopulationData>>(result);
}
public class PopulationData
{
public string Code { get; set; }
public double Value { get; set; }
public string Name { get; set; }
public double Population { get; set; }
public float Density { get; set; }
}
}