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
Steps
Create a new pen
- To get started, either complete the Display a map tutorial or .
Get an access token
You need an access token with the correct privileges to access the location services used in this tutorial.
- Go to the Create an API key tutorial and create an API key with the following privilege(s):
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set
esri
to your API key..Config.api Key Use dark colors for code blocks esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/topographic" // basemap styles service });
To learn about other ways to get an access token, go to Types of authentication.
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 esriConfig.apiKey = "YOUR_ACCESS_TOKEN"; const map = new Map({ basemap: "arcgis/navigation" }); const view = new MapView({ container: "viewDiv", map: map, 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.
-
In the
require
statement, add theLocate
widget module.The ArcGIS Maps SDK 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/
for loading the Map module. After the modules are loaded, they are passed as parameters (e.g.Map" 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. To learn more about the API's different modules visit the Overview Guide page.Use dark colors for code blocks <script> require([ "esri/config", "esri/Map", "esri/views/MapView", "esri/widgets/Locate", ], function( esriConfig, Map, MapView, Locate, ) {
-
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 go
to provide your own custom zoom functionality for the widget. In this case, it will zoom the map to a scale ofTo Override 1500
. Add the widget to the top left of the view.Use dark colors for code blocks 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");
-
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 <script> require([ "esri/config", "esri/Map", "esri/views/MapView", "esri/widgets/Locate", "esri/widgets/Track", "esri/Graphic" ], function( esriConfig, Map, MapView, Locate, Track, Graphic ) {
-
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 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");
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: