Skip to content

Create and style features (3 of 4)

Use the MapLibre ArcGIS plugin to query and display features (cities) with a population greater than 250,000.

Display a clustered feature layer of U.S. cities on a map.

Prerequisites

Steps

Get the service URL

To display the feature layer in an app, reference its service URL.

  1. In your portal, go to the item page of your feature service.

  2. Scroll down to the URL section and copy the value. You will need this in a later step. It should look something like this:
    https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer

Create a new app

Create a new CodePen app with a map div that is the full width and height of the browser window.

  1. Go to CodePen and create a new pen for your application.

  2. In CodePen > HTML, add HTML and CSS to create a page with a div element called map.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>MapLibre GL JS</title>
        <style>
          html,
          body,
          #map {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
            font-family: Arial, Helvetica, sans-serif;
            font-size: 14px;
            color: #323232;
          }
    
        </style>
    
      </head>
    
      <body>
        <div id="map"></div>
    
      </body>
    
    </html>

Add script references

Reference the MapLibre GL JS library and the MapLibre ArcGIS plugin.

  1. In the HTML <head>, add the following <link> and <script> references.

    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
      <!-- Load MapLibre GL JS from CDN -->
      <script src=https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.js></script>
      <link href=https://unpkg.com/maplibre-gl@5.9.0/dist/maplibre-gl.css rel="stylesheet" />
      <!-- Load MapLibre ArcGIS from CDN -->
      <script src="https://unpkg.com/@esri/maplibre-arcgis@1.0.0/dist/umd/maplibre-arcgis.min.js"></script>
    

Set the access token

  1. Add a <script> element in the HTML <body> and create an accessToken variable to store the access token. Set YOUR_ACCESS_TOKEN with the access token you previously copied.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
      <script>
    
        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";
    
      </script>
    
    Expand

Create a map

Create a map to display an ArcGIS basemap style centered on the United States of America. Use the MapLibre ArcGIS plugin to access the basemap style.

  1. Create a Map that centers on the United States of America.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
        const map = new maplibregl.Map({
          container: "map", // the id of the div element
          zoom: 3,
          center: [-100, 40]
        });
    
    Expand
  2. Use the plugin to apply a BasemapStyle that references the arcgis/navigation style.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
        const basemapStyle = maplibreArcGIS.BasemapStyle.applyStyle(map, {
          style: 'arcgis/navigation',
          token: accessToken
        });
    
    Expand

Add a feature layer

Use the MapLibre ArcGIS plugin to add a feature layer to the map that displays the GeoJSON features. You can use a query (such as a where clause) to return a set of features, for example only cities with populations greater than 250,000. This helps reduce data transfer and helps your application load faster.

  1. Add a map.on('load', ...) to register an event handler that runs only once when the map has finished loading.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
        map.once("load", async () => {
    
          map.addSource(featureLayer.sourceId, {
            ...featureLayer.source,
            cluster: true,
            clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart
            clusterMaxZoom: 14, // display all trailheads individually from zoom 14 up
          });
    
        });
    
    Expand
  2. Inside the load event handler, use the plugin to create a new FeatureLayer object that references the feature service URL you copied earlier. Configure the query so it only returns cities with a population greater than 250,000.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
        map.once("load", async () => {
    
          // Get GeoJSON features from a feature service where `POPULATION > 250000`
          const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl(
            "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0",
            {
              query: {
                where: 'POPULATION>250000'
              },
              token: accessToken
            });
    
        });
    
    Expand
  3. Add the feature layer source to the map. Add cluster, clusterRadius and clusterMaxZoom attributes to the definition of the trailheads source to enable feature clustering.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
        map.once("load", async () => {
    
          // Get GeoJSON features from a feature service where `POPULATION > 250000`
          const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl(
            "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0",
            {
              query: {
                where: 'POPULATION>250000'
              },
              token: accessToken
            });
    
          map.addSource(featureLayer.sourceId, {
            ...featureLayer.source,
            cluster: true,
            clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart
            clusterMaxZoom: 14, // display all trailheads individually from zoom 14 up
          });
    
        });
    
    Expand

Add a circle layer

Add a circle layer to visualize clustered features from the GeoJSON source.

  1. Call map.addLayer() to create a layer with type: "circle" that references the usaMajorCities source. Use the paint property to style the layer based on whether a feature is part of a cluster.

    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
        map.once("load", async () => {
    
          // Get GeoJSON features from a feature service where `POPULATION > 250000`
          const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl(
            "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0",
            {
              query: {
                where: 'POPULATION>250000'
              },
              token: accessToken
            });
    
          map.addSource(featureLayer.sourceId, {
            ...featureLayer.source,
            cluster: true,
            clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart
            clusterMaxZoom: 14, // display all trailheads individually from zoom 14 up
          });
    
          map.addLayer({
            ...featureLayer.layer,
            id: "city-clusters",
            type: "circle",
    
            paint: {
              "circle-color": "hsla(0,0%,0%,0.75)",
              "circle-stroke-width": 1.5,
              "circle-stroke-color": "white",
    
              "circle-radius": ["case", ["get", "cluster"], 10, 5] // 10 pixels for clusters, 5 pixels otherwise
    
            }
          });
    
        });
    
    Expand

Cluster points

Add a symbol layer to display cluster counts on top of circle clusters.

  1. Create a symbol layer named cities-cluster-count to display the number of features in each cluster. This layer references the usaMajorCities source and uses the point_count property for the label.
    Expand
    Use dark colors for code blocks
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    13
    14
    15
    16
    17
    18
    19
    20
    21
    22
    23
    24
    25
    26
    27
    28
    29
    30
    31
    32
    33
    34
    35
    36
    37
    38
    39
    40
    41
    42
    43
    44
    45
    46
    47
    48
    49
    50
    51
    52
    53
    54
    55
    56
    57
    58
    59
    60
    61
    62
    63
    64
    65
    66
    67
    68
    69
    70
    71
    72
    73
    74
    75
    76
    77
    78
    79
    80
    81
    82
    83
    84
    85
    86
    87
    88
    89
    90
    91
    92
    93
    94
    95
    96
    97
    98
    99
    100
    101
    102
    103
    104
    105
    106
    107
        map.once("load", async () => {
    
          // Get GeoJSON features from a feature service where `POPULATION > 250000`
          const featureLayer = await maplibreArcGIS.FeatureLayer.fromUrl(
            "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/USA_Major_Cities/FeatureServer/0",
            {
              query: {
                where: 'POPULATION>250000'
              },
              token: accessToken
            });
    
          map.addSource(featureLayer.sourceId, {
            ...featureLayer.source,
            cluster: true,
            clusterRadius: 20, // cluster two trailheads if less than 20 pixels apart
            clusterMaxZoom: 14, // display all trailheads individually from zoom 14 up
          });
    
          map.addLayer({
            ...featureLayer.layer,
            id: "city-clusters",
            type: "circle",
    
            paint: {
              "circle-color": "hsla(0,0%,0%,0.75)",
              "circle-stroke-width": 1.5,
              "circle-stroke-color": "white",
    
              "circle-radius": ["case", ["get", "cluster"], 10, 5] // 10 pixels for clusters, 5 pixels otherwise
    
            }
          });
    
          map.addLayer({
            ...featureLayer.layer,
            id: "cities-cluster-count",
            type: "symbol",
            layout: {
              "text-font": ["Arial Bold"],
              "text-field": ["get", "point_count"],
              "text-offset": [0, 0.1] // move the label vertically downwards slightly to improve centering
            },
            paint: {
              "text-color": "white"
            }
          });
    
        });
    
    Expand

Run the app

Run the app.

The map should display the styled feature layer from the GeoJSON data hosted in your portal.

Your browser is no longer supported. Please upgrade your browser for the best experience. See our browser deprecation post for more details.