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 components. Both components use the 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 arcgis-locate
component finds and zooms to your current location after you click the button, whereas arcgis-track
animates the view to your location at an interval. The track component 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 access token.Config.api Key Use dark colors for code blocks var esriConfig = { apiKey: "YOUR_ACCESS_TOKEN" };
To learn about other ways to get an access token, go to Types of authentication.
Change the basemap and map position
- Update the map component to use the
arcgis/navigation
basemap. This basemap is optimized for navigation. Set the map to be zoomed out to the world.Use dark colors for code blocks <arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"> <arcgis-zoom position="top-left"></arcgis-zoom> </arcgis-map>
Find your geolocation
The arcgis-locate
component uses the browser's Geolocation API to find your device location and zoom the map. Add this component to the map to find and display your current location.
-
In your
arcgis-map
component, add thearcgis-locate
component in the top left.Use dark colors for code blocks <arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"> <arcgis-zoom position="top-left"></arcgis-zoom> <arcgis-locate position="top-left"></arcgis-locate> </arcgis-map>
-
In a new
<script
at the bottom of the> <body
, get a reference to the> arcgis-locate
component and update thego
property to provide your own custom zoom functionality for the widget. In this case, it will zoom the map to a scale ofTo Override 1500
.Use dark colors for code blocks <script> // Get a reference to the locate component const locate = document.querySelector("arcgis-locate"); // Override the default goTo behavior locate.goToOverride = (view, options) => { options.target.scale = 1500; return view.goTo(options.target); } </script>
-
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 arcgis-track
component animates the view to your current location. Tracking is activated by clicking on the component to turn it on or off. By default, it will automatically rotate the view according to your direction of travel. You should only use one geolocation component at a time, so remove the Locate component and add the Track component.
-
In your
arcgis-map
component, add thearcgis-track
component in the top left.Use dark colors for code blocks <arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"> <arcgis-zoom position="top-left"></arcgis-zoom> <arcgis-locate position="top-left"></arcgis-locate> <arcgis-track position="top-left"></arcgis-track> </arcgis-map>
-
In the
<script
, add the> Graphic
module.Use dark colors for code blocks require([ "esri/Graphic" ], (Graphic) => { });
-
Get a reference to the track component and update the graphic with a simple green symbol.
Use dark colors for code blocks require([ "esri/Graphic" ], (Graphic) => { // Get a reference to the track component const track = document.querySelector("arcgis-track"); // Update the graphic symbol track.graphic = new Graphic({ symbol: { type: "simple-marker", size: "12px", color: "green", outline: { color: "#efefef", width: "1.5px" } } }); });
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: