Add a feature layer as GeoJSON

Learn how to add features from feature layers to a map.

A feature layer is a dataset in a hosted feature service. Each feature layer contains features with a single geometry type (point, line, or polygon), and a set of attributes. You can access and display features by making query requests to the feature service and displaying them in a map.

In this tutorial, you access and display three different hosted feature layers. The data is accessed and displayed as GeoJSON.

The feature layers are:

Prerequisites

You need an ArcGIS account to access the developer dashboard and create an API key.

Steps

Create a new pen

  1. To get started, either complete the Display a map tutorial or .

Set the API key

To access location services, you need an API key or OAuth 2.0 access token.

  1. Go to your dashboard to get an API key. The API key must be scoped to access the services used in this tutorial.

  2. In CodePen, update apiKey to use your key.

        
    Change line
    1
    2
    3
    4
    const apiKey = "YOUR_API_KEY";
    const basemapId = "ArcGIS:Streets";
    const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
    olms(map, basemapURL);
    

Add references to ArcGIS REST JS

This tutorial uses ArcGIS REST JS for accessing the feature service. It also uses the ol-popup library to display pop-ups.

  1. In the <head> element, add references to the ArcGIS REST JS and ol-popup libraries.

                                                                                                                                              
    Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>

Add a point feature layer

The trailheads feature layer contains feature with a point geometry. Use a Vector layer to access the feature layer by URL and display the points. By default, they will display as white circles with blue outlines.

  1. Create a VectorSource. Pass a new GeoJSON as the format parameter. For the url parameter, pass a direct link to a FeatureServer endpoint.

    The URL contains an f=geojson parameter to request data in GeoJSON format. For more information about querying a Feature Service, see the Query a feature layer (SQL) and Query a feature layer (spatial) tutorials.

                                                                                                                                              
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
    
          olms(map, basemapURL).then(function (map) {
    
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              pointLayerName +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
    
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            });
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>
  2. Create a Vector layer using the Vector source you defined. Add it to the map using map.addLayer.

                                                                                                                                              
    Add line.Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            });
    
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            });
            map.addLayer(pointLayer);
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>

Add a line feature layer

The Trails feature layer contains features with a line geometry. Use a Vector layer to access the feature layer by URL and display the lines. By default, the features are displayed as blue lines.

  1. Create a VectorSource with format and url parameters.

                                                                                                                                              
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            });
            map.addLayer(pointLayer);
    
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              lineLayerName +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
    
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            });
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>
  2. Create a Vector layer using the Vector source you defined. Add it to the map using map.addLayer.

                                                                                                                                              
    Add line.Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            });
    
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            });
            map.addLayer(lineLayer);
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>

Add a polygon feature layer

The Parks and Open Space feature layer contains features with a polygon geometry. Use a Vector layer to access the feature layer by URL and display the polygons. By default, polygon features are displayed as translucent white fill with blue outlines.

If you added the layer with map.addLayer, it would be added above other layers and obscure them. Instead, use map.getLayers to access the layers collection, then use Collection.insertAt to place it below the other two layers.

  1. Create a VectorSource with format and url parameters.

                                                                                                                                              
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            });
            map.addLayer(lineLayer);
    
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              polygonLayerName +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
    
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            });
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>
  2. Create a Vector layer using the Vector source you defined. Add it to the map using insertAt at index 1.

                                                                                                                                              
    Add line.Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            });
    
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            });
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>
  3. In CodePen, click Run at the top right. Your map should display the locations of trailheads, trails, and open spaces on your map.

Add a pop-up

You can use a Popup to show data attributes for a feature when you click on them. A popup is a type of Overlay so you add it to the map with map.addOverlay. To get a feature, you use the click event and map.getFeaturesAtPixel to get the features at a click point. You also use the point to position the Popup at the correct location.

For a full list of possible attributes to show, see the feature layer pages:

  1. Create a Popup and save it to a popup variable. Add it to the map with map.addOverlay.

                                                                                                                                              
    Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            });
            map.getLayers().insertAt(1, polygonLayer);
    
            const popup = new Popup();
            map.addOverlay(popup);
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
        </script>
    
      </body>
    </html>
  2. Add a click event handler. Inside, check if a feature from the trailheads layer was clicked. If so, move the pop-up, and show the trailhead name and park name (TRL_NAME and PARK_NAME). Otherwise, check the trails layer; show the trail name, length and elevation gain (TRL_NAME, LENGTH_MI, ELEV_GAIN) if it was clicked. Otherwise, check the parks layer; show the park name and managing agency (PARK_NAME and MNG_AGENCY) if it was clicked. Otherwise, hide the pop-up with popup.hide;

                                                                                                                                              
    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
    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
    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>OpenLayers Tutorials: Add a feature layer</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>
    
        <link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/css/ol.css" type="text/css" />
        <script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.7.0/build/ol.js"></script>
        <script src="https://cdn.jsdelivr.net/npm/ol-mapbox-style@6.1.4/dist/olms.js" type="text/javascript"></script>
    
        <script src="https://unpkg.com/@esri/arcgis-rest-request@3.0.0/dist/umd/request.umd.js"></script>
        <script src="https://unpkg.com/ol-popup@4.0.0"></script>
        <link rel="stylesheet" href="https://unpkg.com/ol-popup@4.0.0/src/ol-popup.css" />
    
      </head>
      <body>
        <div id="map"></div>
    
        <script>
          const apiKey = "YOUR_API_KEY";
          const map = new ol.Map({
            target: "map"
            new ol.View({
              center: ol.proj.fromLonLat([-118.805, 34.027]),
              zoom: 12
          const basemapId = "ArcGIS:Streets";
          const basemapURL = "https://basemaps-api.arcgis.com/arcgis/rest/services/styles/" + basemapId + "?type=style&token=" + apiKey;
          olms(map, basemapURL).then(function (map) {
            const pointLayerName = "Trailheads";
            const pointLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const pointSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: pointLayerURL
            const pointLayer = new ol.layer.Vector({
              source: pointSource
            const lineLayerName = "Trails";
            const lineLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const lineSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: lineLayerURL
            const lineLayer = new ol.layer.Vector({
              source: lineSource
            const polygonLayerName = "Parks_and_Open_Space";
            const polygonLayerURL =
              "https://services3.arcgis.com/GVgbJbqm8hXASVYi/arcgis/rest/services/" +
              "/FeatureServer/0/query?where=1%3D1&outFields=*&returnGeometry=true&f=geojson";
            const polygonSource = new ol.source.Vector({
              format: new ol.format.GeoJSON(),
              url: polygonLayerURL
            const polygonLayer = new ol.layer.Vector({
              source: polygonSource
            map.getLayers().insertAt(1, polygonLayer);
            const popup = new Popup();
            map.addOverlay(popup);
    
            map.on("click", (event) => {
              let feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === pointLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('PARK_NAME')}`);
                return;
              }
    
              feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === lineLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('TRL_NAME')}</h4>${feature.get('LENGTH_MI')} miles, ${feature.get('ELEV_GAIN')} feet elevation gain.`);
                return;
              }
    
              feature = map.getFeaturesAtPixel(event.pixel, {
                layerFilter: l => l === polygonLayer
              })[0];
              if (feature) {
                popup.show(event.coordinate, `<h4>${feature.get('PARK_NAME')}</h4>${feature.get('MNG_AGENCY')}`);
                return;
              }
              popup.hide();
            });
        </script>
    
      </body>
    </html>

Run the app

In CodePen, run your code to display the map.

You should now be able to click on features to see information in a pop-up.

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.