Skip To Content ArcGIS for Developers Sign In Dashboard

Overview

You will learn: how to build an app 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 Get a route and directions and Driving directions tutorials. 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.

NOTE: Accessing the device location with the Geolocation API is not supported on insecure origins. Therefore, to use the Locate and Track widgets, the tutorial must run on HTTPS. In most cases you can also use localhost for testing in browsers that supports Window.isSecureContext (currently Chrome and Firefox). Learn more here.

Steps

Create a starter app

  1. Open the JavaScript Starter App on CodePen.

  2. In CodePen, click Fork and save the pen as ArcGIS JavaScript Tutorials: Display and track your location.

Change the basemap and map position

  1. In the main function, update the existing code to use the streets-navigation-vector basemap. This basemap is optimized for navigation. Set the map to be zoomed out to the world.
          var map = new Map({
            //*** UPDATE ***//
            basemap: "streets-navigation-vector"
          });
    
          var view = new MapView({
            container: "viewDiv",
            map: map,
            //*** UPDATE ***//
            center: [-40, 28],
            zoom: 2
          });
    

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.

        require([
          "esri/Map",
          "esri/views/MapView",
          "esri/widgets/Locate"
        ], function(Map, MapView, Locate) {
    
  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.

          var locate = new Locate({
            view: view,
            useHeadingEnabled: false,
            goToOverride: function(view, options) {
              options.target.scale = 1500;  // Override the default map scale
              return view.goTo(options.target);
            }
          });
    
          view.ui.add(locate, "top-left");
    
  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.

        require([
          "esri/Map",
          "esri/views/MapView",
          "esri/widgets/Locate",
          "esri/widgets/Track",
          "esri/Graphic"
        ], function(Map, MapView, Locate, Track, Graphic) {
    
  2. In the main function, comment out the Locate widget code and create 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.

          //*** UPDATE ***//
          // var locate = new Locate({
          //   view: view,
          //   useHeadingEnabled: false,
          //   goToOverride: function(view, options) {
          //     options.target.scale = 1500;  // Override the default map scale
          //     return view.goTo(options.target);
          //   }
          // });
    
          // view.ui.add(locate, "top-left");
    
          //*** ADD ***//
          var track = new Track({
            view: view,
            graphic: new Graphic({
              symbol: {
                type: "simple-marker",
                size: "12px",
                color: "green",
                outline: {
                  color: "#efefef",
                  width: "1.5px"
                }
              }
            }),
            useHeadingEnabled: false  // Don't change orientation of the map
          });
    
          view.ui.add(track, "top-left");
    
  3. Run the app again. 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.

Congratulations, you're done!

Your app should look something like this.

Challenge

Add a compass and rotate the view

Add the Compass widget to the view. Set the Track widget useHeadingEnabled property to true and try the application again to see how the view (and compass) change direction when you start tracking.

      require([
        "esri/Map",
        "esri/views/MapView",
        "esri/widgets/Locate",
        "esri/widgets/Track",
        "esri/widgets/Graphic",
        //*** ADD ***//
        "esri/widgets/Compass"
      ], function(Map, MapView, Locate, Track, Graphic, Compass) {
        //*** ADD ***//
        var compass = new Compass({
          view: view
        });

        // adds the compass to the top left corner of the MapView
        view.ui.add(compass, "top-left");