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.
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.
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.
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.
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.
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.
In the require statement, add the Track and Graphic modules.
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.
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.