Style a feature layer

Learn how to use style .

Style a feature layer using API key authentication

A is a dataset in a hosted in . Each feature layer contains with a single type (point, line, or polygon), and a set of . Layers in OpenLayers can contain style functions, which use attribute values to change the appearance of features. This allows you to create complex, data-driven visualizations by relating visual variables to data attributes.

In this tutorial, you apply different styles to enhance the visualization of the Trailheads, Trails and Parks and Open Spaces feature layers.

Prerequisites

Steps

Get the starter app

Select a type of authentication below and follow the steps to create a new application.

You can choose one of the following to create a new CodePen:

  • Option 1: Complete the Display a map tutorial; or,
  • Option 2: Start from the Display a map tutorial .

Set up authentication

Create in your for the type of authentication you selected.

Create a new with the correct privileges to access the resources used in this tutorial.

  1. Go to the Create an API key tutorial and create an with the following :
    • Privileges
      • Location services > Basemaps
    • Item access
      • Note: If you are using your own custom data layer for this tutorial, you need to grant the access to the layer item. Learn more in Item access privileges.
  2. Copy the API key access token to your clipboard when prompted.

Set developer credentials

Use the API key or OAuth so your application can access .

  1. Update the accessToken variable to use your API key.

    Expand
    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
        /* Use for API key authentication */
        const accessToken = "YOUR_ACCESS_TOKEN";
    
    Expand

Style trailheads with a hiker image and labels

A Style in OpenLayers has several possible components: an image, text, stroke, fill and so on. To display a hiker icon for the trailheads layer, you use an Icon style as the image component. Specify which image file as the src, and the size as the scale parameter.

To display the trailhead name, you use a Text style. Use a left value for textAlign with an offsetX of 10 to display the labels to the right of the icons. Use Fill and Stroke styles as the fill and stroke properties to give the labels white text with a teal outline. The font property is a CSS font definition. Since the label for each trailhead is different, use a function to return a Style for a given feature. Set the text property using feature's TRL_NAME attribute.

  1. Inside olms load handler, create a trailheadStyle function that takes a feature and returns a Style containing an Icon style. Use http://static.arcgis.com/images/Symbols/NPS/npsPictograph_0231b.png for the src and set a scale of 25%.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
        const basemapId = "arcgis/outdoor";
        const basemapURL = `https://basemapstyles-api.arcgis.com/arcgis/rest/services/styles/v2/styles/${basemapId}?token=${accessToken}`;
    
        olms.apply(map, basemapURL).then(function (map) {
    
          const trailheadStyle = function (feature) {
            return new ol.style.Style({
              image: new ol.style.Icon({
                src: "http://static.arcgis.com/images/Symbols/NPS/npsPictograph_0231b.png",
                scale: 0.25
              }),
    
    
    Expand
  2. Add a Text style to display labels with white text, teal outline, in an italic sans-serif font. Set the text property using the feature's TRL_NAME attribute.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
          const trailheadStyle = function (feature) {
            return new ol.style.Style({
              image: new ol.style.Icon({
                src: "http://static.arcgis.com/images/Symbols/NPS/npsPictograph_0231b.png",
                scale: 0.25
              }),
    
              text: new ol.style.Text({
                text: feature.get("TRL_NAME"),
                font: "italic 12px sans-serif",
                offsetX: 10,
                textAlign: "left",
                fill: new ol.style.Fill({
                  color: "#FFFFFF"
                }),
    
                stroke: new ol.style.Stroke({
                  color: "#5E8D74",
                  width: 3
                })
              })
    
            });
          };
    
    Expand

Add the Trailheads feature layer to the map

Use a Vector layer with a Vector source to display the trailheads.

  1. Add a Vector layer with a Vector source to load and display the trailheads feature layer. Set declutter to be true to prevent label overlap. Pass the trailheadsStyle function as the style property. Use map.addLayer to add this layer to the map.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
                stroke: new ol.style.Stroke({
                  color: "#5E8D74",
                  width: 3
                })
              })
    
            });
          };
    
          const trailheadsLayerName = "Trailheads";
          const trailheadsLayerURL =
            "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
            trailheadsLayerName +
            "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=pgeojson";
    
          const trailheadsLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: trailheadsLayerURL,
    
            }),
    
            style: trailheadStyle,
            declutter: true
          });
    
          map.addLayer(trailheadsLayer);
    
    Expand
  2. Add the for the feature layer source.

    • Go to the Trailheads (Santa Monica Mountains) .
    • Scroll down to the Credits (Attribution) section and copy its value.
    • Create an attribution property and paste the attribution value from the item.
      Expand
      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
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
      163
      164
      165
      166
      167
      168
      169
      170
      171
      172
      173
      174
      175
      176
      177
      178
      179
      180
      181
      182
      183
      184
      185
      186
      187
      188
      189
      190
      191
      192
      193
      194
      195
      196
      197
      198
      199
      200
      201
      202
                  stroke: new ol.style.Stroke({
                    color: "#5E8D74",
                    width: 3
                  })
                })
      
              });
            };
      
            const trailheadsLayerName = "Trailheads";
            const trailheadsLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              trailheadsLayerName +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=pgeojson";
      
            const trailheadsLayer = new ol.layer.Vector({
              source: new ol.source.Vector({
                format: new ol.format.GeoJSON(),
                url: trailheadsLayerURL,
      
                // Attribution text retrieved from https://arcgis.com/home/item.html?id=883cedb8c9fe4524b64d47666ed234a7
                attributions: ['Los Angeles GeoHub |']
      
              }),
      
              style: trailheadStyle,
              declutter: true
            });
      
            map.addLayer(trailheadsLayer);
      
      Expand
  3. At the top right, click Run to test your map. You should see hiker icons and trailhead labels.

Style trail width by elevation gain

To visualize the elevation gain of a trail you can use the width of a Stroke style. To do this, create another function which returns a Style. The width of the style's stroke property will be a calculation converting feet of elevation gain to pixels of width.

  1. Create a trailStyle function that takes a feature and returns a Style with a Stroke style. Set the color to pink, and calculate the width property from the feature's ELEV_GAIN attribute.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
            style: trailheadStyle,
            declutter: true
          });
    
          map.addLayer(trailheadsLayer);
    
          const trailStyle = function (feature) {
            return new ol.style.Style({
              stroke: new ol.style.Stroke({
                color: "#BA55D3",
                width: 3 + (4 * feature.get("ELEV_GAIN")) / 2300
              })
            });
          };
    
    Expand
  2. Add the Trails as a Vector layer with a GeoJSON Vector source. Pass your trailStyle function as the style property. Use insertAt to add this layer below the trailheads layer.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
          const trailStyle = function (feature) {
            return new ol.style.Style({
              stroke: new ol.style.Stroke({
                color: "#BA55D3",
                width: 3 + (4 * feature.get("ELEV_GAIN")) / 2300
              })
            });
          };
    
          const trailsLayerName = "Trails";
          const trailsLayerURL =
            "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
            trailsLayerName +
            "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=pgeojson";
          const trailsLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: trailsLayerURL,
    
            }),
    
            style: trailStyle
          });
          map.getLayers().insertAt(1, trailsLayer);
    
    Expand
  3. Add the for the feature layer source.

    • Go to the Trails .
    • Scroll down to the Credits (Attribution) section and copy its value.
    • Create an attribution property and paste the attribution value from the item.
      Expand
      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
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
      163
      164
      165
      166
      167
      168
      169
      170
      171
      172
      173
      174
      175
      176
      177
      178
      179
      180
      181
      182
      183
      184
      185
      186
      187
      188
      189
      190
      191
      192
      193
      194
      195
      196
      197
      198
      199
      200
      201
      202
            const trailStyle = function (feature) {
              return new ol.style.Style({
                stroke: new ol.style.Stroke({
                  color: "#BA55D3",
                  width: 3 + (4 * feature.get("ELEV_GAIN")) / 2300
                })
              });
            };
      
            const trailsLayerName = "Trails";
            const trailsLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              trailsLayerName +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=pgeojson";
            const trailsLayer = new ol.layer.Vector({
              source: new ol.source.Vector({
                format: new ol.format.GeoJSON(),
                url: trailsLayerURL,
      
                // Attribution text retrieved from https://arcgis.com/home/item.html?id=69e12682738e467eb509d8b54dc73cbd
                attributions: ['Los Angeles GeoHub |']
      
              }),
      
              style: trailStyle
            });
            map.getLayers().insertAt(1, trailsLayer);
      
      Expand
  4. At the top right, click Run to test your map. You should see the hiking trails in pink with thicker lines for those with more elevation gain.

Add a filtered bike-only trails layer

To display bike-only trails, you can add another style function to display a dashed line for trails that allow biking. You can display a dashed line by passing an array of alternating stroke and gap segments length as the lineDash property in a Stroke style.

The same source from the trailsLayer can be used.

  1. Create a bikeTrailsStyle function. If the has a Yes value for USE_BIKE, return a white dashed Stroke style. Otherwise, do not return a Style, so the will not be displayed.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
            style: trailStyle
          });
          map.getLayers().insertAt(1, trailsLayer);
    
          const bikeTrailsStyle = function (feature) {
            if (feature.get("USE_BIKE") === "Yes") {
              return new ol.style.Style({
    
                stroke: new ol.style.Stroke({
                  lineDash: [1, 4],
                  color: "white",
                  width: 2
                })
              });
            }
          };
    
    Expand
  2. Add a bikeTrailsLayer using the source from trailsLayer, with the bikeTrailsStyle function. Insert the layer above the trailsLayer.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
                stroke: new ol.style.Stroke({
                  lineDash: [1, 4],
                  color: "white",
                  width: 2
                })
              });
            }
          };
    
          const bikeTrailsLayer = new ol.layer.Vector({
            source: trailsLayer.getSource(),
            style: bikeTrailsStyle
          });
          map.getLayers().insertAt(2, bikeTrailsLayer);
    
    Expand
  3. At the top right, click Run to test your map. You should now see the bike-accessible trails with a dashed line.

Style a polygon layer

You can use color to communicate the category of a , such as the type of a park or open space. Use a style function to returns a Fill style, in which you derive the color property from the feature's TYPE property.

  1. Create a parksStyle function which returns a Fill style. Look up the feature's TYPE attribute in a table to give a different color for Natural Areas, Regionla Open Space, Local Park and Regional Recreation Park. If the type is not one of these, make the color transparent.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
          const bikeTrailsLayer = new ol.layer.Vector({
            source: trailsLayer.getSource(),
            style: bikeTrailsStyle
          });
          map.getLayers().insertAt(2, bikeTrailsLayer);
    
          const parksStyle = function (feature) {
            const type = feature.get("TYPE");
            const colorTable = {
              "Natural Areas": "#9E559C",
              "Regional Open Space": "#A7C636",
              "Local Park": "#149ECE",
              "Regional Recreation Park": "#ED5151"
            };
    
            return new ol.style.Style({
              fill: new ol.style.Fill({
                color: colorTable[feature.get("TYPE")] || "transparent"
              })
            });
          };
    
    Expand
  2. Add the for the feature layer source.

    • Go to the Parks and Open Space .
    • Scroll down to the Credits (Attribution) section and copy its value.
    • Create an attribution property and paste the attribution value from the item.
      Expand
      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
      108
      109
      110
      111
      112
      113
      114
      115
      116
      117
      118
      119
      120
      121
      122
      123
      124
      125
      126
      127
      128
      129
      130
      131
      132
      133
      134
      135
      136
      137
      138
      139
      140
      141
      142
      143
      144
      145
      146
      147
      148
      149
      150
      151
      152
      153
      154
      155
      156
      157
      158
      159
      160
      161
      162
      163
      164
      165
      166
      167
      168
      169
      170
      171
      172
      173
      174
      175
      176
      177
      178
      179
      180
      181
      182
      183
      184
      185
      186
      187
      188
      189
      190
      191
      192
      193
      194
      195
      196
      197
      198
      199
      200
      201
      202
            const bikeTrailsLayer = new ol.layer.Vector({
              source: trailsLayer.getSource(),
              style: bikeTrailsStyle
            });
            map.getLayers().insertAt(2, bikeTrailsLayer);
      
            const parksStyle = function (feature) {
              const type = feature.get("TYPE");
              const colorTable = {
                "Natural Areas": "#9E559C",
                "Regional Open Space": "#A7C636",
                "Local Park": "#149ECE",
                "Regional Recreation Park": "#ED5151"
              };
      
              return new ol.style.Style({
                fill: new ol.style.Fill({
                  color: colorTable[feature.get("TYPE")] || "transparent"
                })
              });
            };
      
      Expand
  3. Create a Vector layer with a Vector source for the parks . Pass your parksStyle function as the style property, with a low opacity. Insert the parks layer below the trails layers.

    Expand
    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
    119
    120
    121
    122
    123
    124
    125
    126
    127
    128
    129
    130
    131
    132
    133
    134
    135
    136
    137
    138
    139
    140
    141
    142
    143
    144
    145
    146
    147
    148
    149
    150
    151
    152
    153
    154
    155
    156
    157
    158
    159
    160
    161
    162
    163
    164
    165
    166
    167
    168
    169
    170
    171
    172
    173
    174
    175
    176
    177
    178
    179
    180
    181
    182
    183
    184
    185
    186
    187
    188
    189
    190
    191
    192
    193
    194
    195
    196
    197
    198
    199
    200
    201
    202
            return new ol.style.Style({
              fill: new ol.style.Fill({
                color: colorTable[feature.get("TYPE")] || "transparent"
              })
            });
          };
    
          const parksLayerName = "Parks_and_Open_Space";
          const parksLayerURL =
            "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
            parksLayerName +
            "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=pgeojson";
          const parksLayer = new ol.layer.Vector({
            source: new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: parksLayerURL,
    
            }),
            opacity: 0.2,
            style: parksStyle
          });
          map.getLayers().insertAt(1, parksLayer);
    
    Expand

Run the app

Run the app.

You should now see parks of different colors, beneath the trails and trailhead layers.

What's next?

Learn how to use additional ArcGIS location services in these tutorials:

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

You can no longer sign into this site. Go to your ArcGIS portal or the ArcGIS Location Platform dashboard to perform management tasks.

Your ArcGIS portal

Create, manage, and access API keys and OAuth 2.0 developer credentials, hosted layers, and data services.

Your ArcGIS Location Platform dashboard

Manage billing, monitor service usage, and access additional resources.

Learn more about these changes in the What's new in Esri Developers June 2024 blog post.

Close