A RetroSearch Logo

Home - News ( United States | United Kingdom | Italy | Germany ) - Football scores

Search Query:

Showing content from https://developers.arcgis.com/javascript/latest/tutorials/add-a-point-line-and-polygon/ below:

Add a point, line, and polygon | Overview | ArcGIS Maps SDK for JavaScript 4.33

Learn how to display point, line, and polygon graphics in a map.

Graphics are visual elements used to display points, lines, polygons, and text in a map or scene. Graphics are composed of a geometry, symbol, and attributes, and can display a pop-up when clicked. You typically use graphics to display geographic data that is not connected to a database (i.e. a GPS location).

In this tutorial, you will learn how to display points, lines, and polygons on a map as graphics.

Note

For more background information on implementing graphics in your applications, visit Graphics, and Styles and data visualization in the Mapping and location services guide.

Prerequisites ArcGIS Accounts:

You need an ArcGIS Location Platform or ArcGIS Online account.

Steps Create a new pen
  1. To get started, either complete the Display a map tutorial or .
Get an access token

You need an access token with the correct privileges to access the location services used in this tutorial.

  1. Go to the Create an API key tutorial and create an API key with the following privilege(s):
  2. In CodePen, set esriConfig.apiKey to your access token.

    Use dark colors for code blocks

    1
    2
    3
    4
    5
    6
    7
    8
    9
      var esriConfig = {
        apiKey: "YOUR_ACCESS_TOKEN",
      };
    

To learn about other ways to get an access token, go to Types of authentication.

Add modules
  1. In a new <script> at the bottom of the <body>, use $arcgis.import() to add the Map, Graphic and GraphicsLayer modules.

    The ArcGIS Maps SDK for JavaScript is available via CDN and npm, but this tutorial is based on CDN. The $arcgis.import global function accepts a module path or array of module paths, and returns a promise that resolves with the requested modules. This function can only be used when working with the CDN; otherwise, use the standard import syntax. To learn more about the SDK's different modules, visit the References page.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
        <script type="module">
          const [Graphic, Map, GraphicsLayer] = await $arcgis.import([
            "@arcgis/core/Graphic.js",
            "@arcgis/core/Map.js",
            "@arcgis/core/layers/GraphicsLayer.js",
          ]);
    
        </script>
    
Add a graphics layer

A graphics layer is a container for graphics. It is used with a map view to display graphics on a map. You can add more than one graphics layer to a map. Graphics layers are displayed on top of all other layers.

  1. Create and add a GraphicsLayer for displaying graphics on a map.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
        <script type="module">
          const [Graphic, Map, GraphicsLayer] = await $arcgis.import([
            "@arcgis/core/Graphic.js",
            "@arcgis/core/Map.js",
            "@arcgis/core/layers/GraphicsLayer.js",
          ]);
    
          const viewElement = document.querySelector("arcgis-map");
    
          const graphicsLayer = new GraphicsLayer();
    
          viewElement.map = new Map({ basemap: "arcgis/topographic", layers: [graphicsLayer] });
    
        </script>
    
Add a point graphic

A point graphic is created using a point and a marker symbol. A point is defined with longitude (x) and latitude (y) coordinates, and a simple symbol is defined with a color and outline. The Point and SimpleMarkerSymbol classes are used to create the point graphic.

If you would like to display graphics in a map with a spatial reference other than WKID 102100, 3857, or 4326, you must specify the spatial reference when creating a point, line, or polygon geometry. Otherwise, it can be omitted, and the map view's spatial reference will be applied. Learn more about spatial references and coordinate systems in Spatial reference.

  1. Create a point and simpleMarkerSymbol that will be used to create a Graphic.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          const point = {
            //Create a point
            type: "point",
            longitude: -118.80657463861,
            latitude: 34.0005930608889,
          };
          const simpleMarkerSymbol = {
            type: "simple-marker",
            color: [226, 119, 40], // Orange
            outline: {
              color: [255, 255, 255], // White
              width: 1,
            },
          };
    
    
  2. Create a Graphic and set the geometry and symbol properties. The Graphic class will autocast point and simpleMarkerSymbol when it is constructed.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          const point = {
            //Create a point
            type: "point",
            longitude: -118.80657463861,
            latitude: 34.0005930608889,
          };
          const simpleMarkerSymbol = {
            type: "simple-marker",
            color: [226, 119, 40], // Orange
            outline: {
              color: [255, 255, 255], // White
              width: 1,
            },
          };
    
          const pointGraphic = new Graphic({ geometry: point, symbol: simpleMarkerSymbol });
          graphicsLayer.add(pointGraphic);
    
  3. Verify that the point graphic positioned at Point Dume Beach.

Add a line graphic

A line graphic is created using a polyline and a line symbol. A polyline is defined as a sequence of points and a spatial reference. The Polyline and SimpleLineSymbol classes are used to create a line graphic.

  1. Define the polyline and simpleLineSymbol that will be used to create a Graphic.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          const pointGraphic = new Graphic({ geometry: point, symbol: simpleMarkerSymbol });
          graphicsLayer.add(pointGraphic);
    
          // Create a line geometry
          const polyline = {
            type: "polyline",
            paths: [
              [-118.821527826096, 34.0139576938577], //Longitude, latitude
              [-118.814893761649, 34.0080602407843], //Longitude, latitude
              [-118.808878330345, 34.0016642996246], //Longitude, latitude
            ],
          };
          const simpleLineSymbol = {
            type: "simple-line",
            color: [226, 119, 40], // Orange
            width: 2,
          };
    
  2. Create a Graphic and set the geometry and symbol properties. The Graphic class will autocast the polyline and simpleLineSymbol when it is created.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          // Create a line geometry
          const polyline = {
            type: "polyline",
            paths: [
              [-118.821527826096, 34.0139576938577], //Longitude, latitude
              [-118.814893761649, 34.0080602407843], //Longitude, latitude
              [-118.808878330345, 34.0016642996246], //Longitude, latitude
            ],
          };
          const simpleLineSymbol = {
            type: "simple-line",
            color: [226, 119, 40], // Orange
            width: 2,
          };
    
          const polylineGraphic = new Graphic({ geometry: polyline, symbol: simpleLineSymbol });
          graphicsLayer.add(polylineGraphic);
    
  3. Verify that the line graphic positioned along Westward Beach.

Add a polygon graphic

A polygon graphic is created using a polygon and a fill symbol. A polygon is defined as a sequence of points (ring) that describe a closed boundary and a spatial reference. The Polygon and SimpleFillSymbol classes are used to create and display a polygon graphic.

  1. Define the polygon and simpleFillSymbol that will be used to create a Graphic

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          const polylineGraphic = new Graphic({ geometry: polyline, symbol: simpleLineSymbol });
          graphicsLayer.add(polylineGraphic);
    
          // Create a polygon geometry
          const polygon = {
            type: "polygon",
            rings: [
              [-118.818984489994, 34.0137559967283], //Longitude, latitude
              [-118.806796597377, 34.0215816298725], //Longitude, latitude
              [-118.791432890735, 34.0163883241613], //Longitude, latitude
              [-118.79596686535, 34.008564864635], //Longitude, latitude
              [-118.808558110679, 34.0035027131376], //Longitude, latitude
            ],
          };
    
          const simpleFillSymbol = {
            type: "simple-fill",
            color: [227, 139, 79, 0.8], // Orange, opacity 80%
            outline: { color: [255, 255, 255], width: 1 },
          };
    
  2. Create a Graphic and set the geometry and symbol properties. The Graphic class will autocast the polygon and simpleFillSymbol when it is created.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          // Create a polygon geometry
          const polygon = {
            type: "polygon",
            rings: [
              [-118.818984489994, 34.0137559967283], //Longitude, latitude
              [-118.806796597377, 34.0215816298725], //Longitude, latitude
              [-118.791432890735, 34.0163883241613], //Longitude, latitude
              [-118.79596686535, 34.008564864635], //Longitude, latitude
              [-118.808558110679, 34.0035027131376], //Longitude, latitude
            ],
          };
    
          const simpleFillSymbol = {
            type: "simple-fill",
            color: [227, 139, 79, 0.8], // Orange, opacity 80%
            outline: { color: [255, 255, 255], width: 1 },
          };
    
          const polygonGraphic = new Graphic({
            geometry: polygon,
            symbol: simpleFillSymbol,
    
          });
          graphicsLayer.add(polygonGraphic);
    
  3. Verify that the polygon graphic positioned on Mahou Riviera.

Create a pop-up

You can display a pop-up for a graphic when it is clicked. The code that creates the polygon graphic to show a pop-up containing the name and description of the graphic uses the attribute and popupTemplate properties.

  1. Define the popupTemplate and attributes before defining the polygonGraphic.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          const simpleFillSymbol = {
            type: "simple-fill",
            color: [227, 139, 79, 0.8], // Orange, opacity 80%
            outline: { color: [255, 255, 255], width: 1 },
          };
    
          const popupTemplate = { title: "{Name}", content: "{Description}" };
          const attributes = { Name: "Graphic", Description: "I am a polygon" };
    
  2. Update the polygonGraphic to include the popupTemplate and attribute properties.

    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
    108
    109
    110
    111
    112
    113
    114
    115
    116
    117
    118
          const polygonGraphic = new Graphic({
            geometry: polygon,
            symbol: simpleFillSymbol,
    
            attributes: attributes,
            popupTemplate: popupTemplate,
    
          });
          graphicsLayer.add(polygonGraphic);
    
Run the app

In CodePen, run your code to display the map.

The map should display all three graphics. When you click on the polygon, it should show a pop-up.

What's next?

Learn how to use additional API features and ArcGIS services in these tutorials:


RetroSearch is an open source project built by @garambo | Open a GitHub Issue

Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo

HTML: 3.2 | Encoding: UTF-8 | Version: 0.7.4