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
- To get started, either complete the Display a map tutorial or .
Set the API key
To access ArcGIS services, you need an API key.
Go to your dashboard to get an API key.
In CodePen, set the
api
to your key, so it can be used to access basemap layer and location services.Key Use dark colors for code blocks Change line
Change the basemap and map position
In the main
function
, update the existing code to use thearcgis-navigation
basemap. This basemap is optimized for navigation. Set the map to be zoomed out to the world.Use dark colors for code blocks Change line Change line Change line
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.
In the
require
statement, add theLocate
widget module.Use dark colors for code blocks Add line. Add line. At the end of the code in the main
function
, create theLocate
widget and setuse
to false so it does not change the rotation of the map. Use theHeading Enabled g
to provide your own custom zoom functionality for the widget. In this case, it will zoom the map to a scale ofo To Override 1500
. Add the widget to the top left of the view.Use dark colors for code blocks Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. Add line. 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 theTrack
andGraphic
modules.Use dark colors for code blocks Add line. Add line. Add line. Add line. In the main
function
, replace theLocate
widget code with theTrack
widget and set the graphic with a simple green symbol, and set theuse
to false so the map view doesn't rotate. Add the widget to the top left of the view.Heading Enabled Use dark colors for code blocks Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line Change line
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: