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.

    Change line
        
    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.

    Change lineChange lineChange line
                                                                                              
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>ArcGIS API for JavaScript Tutorials: Display your location</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
    
        <script>
          require([
              "esri/config",
              "esri/Map",
              "esri/views/MapView",
              "esri/widgets/Locate",
    
              "esri/widgets/Track",
              "esri/Graphic"
    
            ], function(
                esriConfig,
                Map,
                MapView,
                Locate,
    
                Track,
                Graphic
    
            ) {
    
            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
            });
    
            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");
    
            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");
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>

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 uses AMD modules. The require function is used to load modules so they can be used in the main function. It's important to keep the module references and function parameters in the same order.

    Add line.Add line.
                                                                                              
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>ArcGIS API for JavaScript Tutorials: Display your location</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
    
        <script>
          require([
              "esri/config",
              "esri/Map",
              "esri/views/MapView",
              "esri/widgets/Locate",
    
              "esri/widgets/Track",
              "esri/Graphic"
    
            ], function(
                esriConfig,
                Map,
                MapView,
                Locate,
    
                Track,
                Graphic
    
            ) {
    
            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
            });
    
            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");
    
            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");
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>
  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.

    Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.Add line.
                                                                                              
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>ArcGIS API for JavaScript Tutorials: Display your location</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
    
        <script>
          require([
              "esri/config",
              "esri/Map",
              "esri/views/MapView",
              "esri/widgets/Locate",
    
              "esri/widgets/Track",
              "esri/Graphic"
    
            ], function(
                esriConfig,
                Map,
                MapView,
                Locate,
    
                Track,
                Graphic
    
            ) {
    
            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
            });
    
            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");
    
            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");
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>
  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.

    Add line.Add line.Add line.Add line.
                                                                                              
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>ArcGIS API for JavaScript Tutorials: Display your location</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
    
        <script>
          require([
              "esri/config",
              "esri/Map",
              "esri/views/MapView",
              "esri/widgets/Locate",
    
              "esri/widgets/Track",
              "esri/Graphic"
    
            ], function(
                esriConfig,
                Map,
                MapView,
                Locate,
    
                Track,
                Graphic
    
            ) {
    
            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
            });
    
            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");
    
            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");
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>
  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.

    Change lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange lineChange line
                                                                                              
    <html>
      <head>
        <meta charset="utf-8" />
        <meta name="viewport" content="initial-scale=1, maximum-scale=1, user-scalable=no" />
        <title>ArcGIS API for JavaScript Tutorials: Display your location</title>
    
        <style>
          html,
          body,
          #viewDiv {
            padding: 0;
            margin: 0;
            height: 100%;
            width: 100%;
          }
        </style>
    
        <link rel="stylesheet" href="https://js.arcgis.com/4.18/esri/themes/light/main.css">
        <script src="https://js.arcgis.com/4.18/"></script>
    
    
        <script>
          require([
              "esri/config",
              "esri/Map",
              "esri/views/MapView",
              "esri/widgets/Locate",
    
              "esri/widgets/Track",
              "esri/Graphic"
    
            ], function(
                esriConfig,
                Map,
                MapView,
                Locate,
    
                Track,
                Graphic
    
            ) {
    
            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
            });
    
            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");
    
            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");
    
          });
        </script>
    
      </head>
      <body>
        <div id="viewDiv"></div>
      </body>
    </html>

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: