Populate Data in Angular Maps

16 May 20228 minutes to read

In this section you can learn how to populate shape data for providing Data input to Map control and usage of DataSource property.

Shape Data

The Shape Data collection describing geographical shape information can be obtained from GEOJSON format shapes.

In this example, USA shape is used as shape data by utilizing the “United States of America.json” file in the following folder structure obtained from downloaded Maps_GeoJSON folder.

..\ Maps_GeoJSON\All Countries with States

You can assign the complete contents in “United States of America.json” file to new JSON object. For better understanding, a JS file “usa.js” is already created to store JSON data in JSON object “usMap”.

[usa.js]

  • JAVASCRIPT
  • var usMap = //Paste all the content copied from the JSON file//

    Data Binding

    The Maps control supports data binding with the dataSource property in the shape layers.

    Properties

    The following properties in shape layers is be used for binding data in Maps control,

    • dataSource
    • shapeDataPath
    • shapePropertyPath

    Data Source

    The dataSource property accepts the collection values as input. For example, you can provide the list of objects as input.

    Shape Data Path

    The shapeDataPath property is used to refer the data ID in DataSource. For example, population MapData contains data ids ‘Name’ and ‘Population’. The shapeDataPath and the shapePropertyPath properties are related to each other (refer to shapePropertyPath for more details).

    Shape Property Path

    The shapePropertyPath property is similar to the shapeDataPath that refers to the column name in the data property of shape layers to identify the shape. When the values of the shapeDataPath property in the dataSource property and the value of shapePropertyPath in the data property match, then the associated object from the dataSource is bound to the corresponding shape.

    The datasource is populated with JSON data relative to shape data and stored in JSON object. The USA population as datasource is used for better understanding.

    The “populationData.js” file is used to store JSON data in JSON object “populationData”.

    Refer both shape data and datasource as illustrated in the following code example.

    [populationData.js]

  • TS
  • var populationData = [
            { name: "California", population: "38332521" },
            { name: "Texas", population: "26448193" },
            { name: "New York", population: "19651127" },
            { name: "Florida", population: "19552860" },
            { name: "Illinois", population: "12882135" },
            { name: "Pennsylvania", population: "12773801" },
            { name: "Ohio", population: "11570808" },
            { name: "Georgia", population: "9992167" },
            { name: "Michigan", population: "9895622" },
            { name: "North Carolina", population: "9848060" },
            { name: "New Jersey", population: "8899339" },
            { name: "Virginia", population: "8260405" },
            { name: "Washington", population: "6971406" },
            { name: "Massachusetts", population: "6692824" },
            { name: "Arizona", population: "6626624" },
            { name: "Indiana", population: "6570902" },
            { name: "Tennessee", population: "6495978" },
            { name: "Missouri", population: "6044171" },
            { name: "Maryland", population: "5928814" },
            { name: "Wisconsin", population: "5742713" },
            { name: "Minnesota", population: "5420380" },
            { name: "Colorado", population: "5268367" },
            { name: "Alabama", population: "4833722" },
            { name: "South Carolina", population: "4774839" },
            { name: "Louisiana", population: "4625470" },
            { name: "Kentucky", population: "4395295" },
            { name: "Oregon", population: "3930065" },
            { name: "Oklahoma", population: "3850568" },
            { name: "Puerto Rico", population: "3615086" },
            { name: "Connecticut", population: "3596080" },
            { name: "Iowa", population: "3090416" },
            { name: "Mississippi", population: "2991207" },
            { name: "Arkansas", population: "2959373" },
            { name: "Utah", population: "2900872" },
            { name: "Kansas", population: "2893957" },
            { name: "Nevada", population: "2790136" },
            { name: "New Mexico", population: "2085287" },
            { name: "Nebraska", population: "1868516" },
            { name: "West Virginia", population: "1854304" },
            { name: "Idaho", population: "1612136" },
            { name: "Hawaii", population: "1404054" },
            { name: "Maine", population: "1328302" },
            { name: "New Hampshire", population: "1323459" },
            { name: "Rhode Island", population: "1051511" },
            { name: "Montana", population: "1015165" },
            { name: "Delaware", population: "925749" },
            { name: "South Dakota", population: "844877" },
            { name: "Alaska", population: "735132" },
            { name: "North Dakota", population: "723393" },
            { name: "District of Columbia", population: "646449" },
            { name: "Vermont", population: "626630" },
            { name: "Wyoming", population: "582658" }
    ];
    
        <!-- Shape data file-->
        <script src="usa.js" type="text/javascript"></script>
        
        <!-- Data source file-->
        <script src="populationData.js" type="text/javascript"></script>

    The JSON object “populationData” is used as dataSource in the following code example.

  • TS
  • this.mapLayers = [{
        shapeData: usMap,
        shapeDataPath: "name",
        shapeIdTableField: "name",
        dataSource: populationData
    }];
  • HTML
  • <ej-map id="mapcontrol" [layers]="mapLayers">
    </ej-map>