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 widgets. Both widgets use the HTML5 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 Locate widget finds and zooms to your current location after you click the button, whereas the Track widget animates the view to your location at an interval. The Track widget 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

You need a free ArcGIS developer account to access your dashboard and API keys. The API key must be scoped to access the services used in this tutorial.

Steps

Create a new pen

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

Set the API key

To access ArcGIS services, you need an API key.

  1. Go to your dashboard to get an API key.

  2. In CodePen, set the apiKey to your key, so it can be used to access basemap layer and location services.

    Use dark colors for code blocks
        
    Change line
    1
    2
    3
    4
    esriConfig.apiKey = "YOUR_API_KEY";
    const map = new Map({
      basemap: "arcgis-topographic" // Basemap layer service
    });

Change the basemap and map position

  1. In the main function, update the existing code 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
                                                                                              
    Change lineChange lineChange 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
            esriConfig.apiKey = "YOUR_API_KEY";
    
            const map = new Map({
              basemap: "arcgis-navigation"
            });
    
            const view = new MapView({
              container: "viewDiv",
              map: map,
              center: [-40, 28],
              zoom: 2
            });
    
    Expand

Find your geolocation

The Locate widget uses HTML5 to find your device location and zoom the map. Add this widget to the map to find and display your current location.

  1. In the require statement, add the Locate widget module.

    The ArcGIS API for JavaScript is available as AMD modules and ES modules, but this tutorial is based on AMD. The AMD require function uses references to determine which modules will be loaded – for example, you can specify "esri/Map" for loading the Map module. After the modules are loaded, they are passed as parameters (e.g. Map) to the callback function where they can be used in your application. It is important to keep the module references and callback parameters in the same order. For more information on the different types of modules, visit the Introduction to Tooling guide topic.

    Expand
    Use dark colors for code blocks
                                                                                              
    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
        <script>
          require([
              "esri/config",
              "esri/Map",
              "esri/views/MapView",
              "esri/widgets/Locate",
    
            ], function(
                esriConfig,
                Map,
                MapView,
                Locate,
    
            ) {
    
    Expand
  2. At the end of the code in the main function, create the Locate widget and set useHeadingEnabled to false so it does not change the rotation of the map. Use the goToOverride to provide your own custom zoom functionality for the widget. In this case, it will zoom the map to a scale of 1500. Add the widget to the top left of the view.

    Expand
    Use dark colors for code blocks
                                                                                              
    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
            const locate = new Locate({
              view: view,
              useHeadingEnabled: false,
              goToOverride: function(view, options) {
                options.target.scale = 1500;
                return view.goTo(options.target);
              }
            });
            view.ui.add(locate, "top-left");
    
    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 Track widget animates the view to your current location. Tracking is activated by toggling the widget on and off. By default it will automatically rotate the view according to your direction of travel. You generally only use one geolocation widget, so remove the Locate widget and add the Track widget.

  1. In the require statement, add the Track and Graphic modules.

    Expand
    Use dark colors for code blocks
                                                                                              
    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
        <script>
          require([
              "esri/config",
              "esri/Map",
              "esri/views/MapView",
              "esri/widgets/Locate",
    
              "esri/widgets/Track",
              "esri/Graphic"
    
            ], function(
                esriConfig,
                Map,
                MapView,
                Locate,
    
                Track,
                Graphic
    
            ) {
    
    Expand
  2. In the main function, replace the Locate widget code with the Track widget and set the graphic with a simple green symbol, and set the useHeadingEnabled to false so the map view doesn't rotate. Add the widget to the top left of the view.

    Expand
    Use dark colors for code blocks
                                                                                              
    Change lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange 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
            const track = new Track({
              view: view,
              graphic: new Graphic({
                symbol: {
                  type: "simple-marker",
                  size: "12px",
                  color: "green",
                  outline: {
                    color: "#efefef",
                    width: "1.5px"
                  }
                }
              }),
              useHeadingEnabled: false
            });
            view.ui.add(track, "top-left");
    
    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.