Display your location

Overview

Learn how to find and track your device location on a map.

Applications can find, track, and display the geolocation for a device with the locate and track components. Both components use the Geolocation API to find the device's location and provide updates. Once your geolocation is found, you can zoom to the location, display a graphic, and follow along as your location changes. The arcgis-locate component finds and zooms to your current location after you click the button, whereas arcgis-track animates the view to your location at an interval. The track component is useful for building applications that provide driving directions and follow routes. Learn more about finding directions in the Find a route and directions tutorial. If you want to find places such as restaurants and gas stations around your current location, try the Find places tutorial.

In this tutorial, you will build an app to find and track your location on a map.

Prerequisites

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):
    • Privileges
      • Location services > Basemaps
  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.

Change the basemap and map position

  1. Update the map component to use the arcgis/navigation basemap. This basemap is optimized for navigation. Set the map to be zoomed out to the world.
    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
      <arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2">
    
        <arcgis-zoom position="top-left"></arcgis-zoom>
    
      </arcgis-map>
    
    Expand

Find your geolocation

The arcgis-locate component uses the browser's Geolocation API to find your device location and zoom the map. Add this component to the map to find and display your current location.

  1. In your arcgis-map component, add the arcgis-locate component in the top left.

    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
      <arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2">
    
        <arcgis-zoom position="top-left"></arcgis-zoom>
    
        <arcgis-locate position="top-left"></arcgis-locate>
    
      </arcgis-map>
    
    Expand
  2. In a new <script> at the bottom of the <body>, get a reference to the arcgis-locate component and update the goToOverride property to provide your own custom zoom functionality for the widget. In this case, it will zoom the map to a scale of 1500.

    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
      <script>
        // Get a reference to the locate component
        const locate = document.querySelector("arcgis-locate");
        // Override the default goTo behavior
        locate.goToOverride = (view, options) => {
          options.target.scale = 1500;
          return view.goTo(options.target);
        }
    
      </script>
    
    Expand
  3. Run the application and click on the locate button to find your location. The map should zoom to a scale of 1500. The blue symbol represents your geolocation. You can remove the graphic by clicking on it and clicking the ... on the pop-up to remove the graphic.

Track your location

The arcgis-track component animates the view to your current location. Tracking is activated by clicking on the component to turn it on or off. By default, it will automatically rotate the view according to your direction of travel. You should only use one geolocation component at a time, so remove the Locate component and add the Track component.

  1. In your arcgis-map component, add the arcgis-track component in the top left.

    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
      <arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2">
    
        <arcgis-zoom position="top-left"></arcgis-zoom>
    
        <arcgis-locate position="top-left"></arcgis-locate>
    
        <arcgis-track position="top-left"></arcgis-track>
    
      </arcgis-map>
    
    Expand
  2. In the <script>, add the Graphic module.

    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
        require([
          "esri/Graphic"
        ], (Graphic) => {
    
        });
    
    Expand
  3. Get a reference to the track component and update the graphic with a simple green symbol.

    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
        require([
          "esri/Graphic"
        ], (Graphic) => {
    
          // Get a reference to the track component
          const track = document.querySelector("arcgis-track");
          // Update the graphic symbol
          track.graphic = new Graphic({
            symbol: {
              type: "simple-marker",
              size: "12px",
              color: "green",
              outline: {
                color: "#efefef",
                width: "1.5px"
              }
            }
          });
    
        });
    
    Expand

Run the App

In CodePen, run your code to display the map.

Click the Track button in the top-left. The green symbol represents your location. Experiment with tracking your current location by moving to different locations. Visit the documentation to learn more about the geolocation tracking interval and timeout settings.

What's next?

Learn how to use additional API features and ArcGIS 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.