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
- Go to the Create an API key tutorial and create an
API key with the followingprivilege(s) :
- Privileges
- Location services > Basemaps
- Privileges
- In CodePen, set the
apiKeyproperty on the globalesriConfigvariable to your access token.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/navigationbasemap. This basemap is optimized for navigation. Set the map to be zoomed out to the world.28 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display your location</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"><arcgis-zoom slot="top-left"></arcgis-zoom></arcgis-map>4 collapsed lines</body></html>
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-mapcomponent, add thearcgis-locatecomponent in the top left.28 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display your location</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-locate slot="top-left"></arcgis-locate></arcgis-map>4 collapsed lines</body></html> -
In a new
<script>at the bottom of the<body>, get a reference to thearcgis-locatecomponent and update thegoToOverrideproperty to provide your own custom zoom functionality for the component. In this case, it will zoom the map to a scale of1500.36 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display your location</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-locate slot="top-left"></arcgis-locate></arcgis-map><script type="module">// Get a reference to the locate componentconst locate = document.querySelector("arcgis-locate");// Override the default goTo behaviorlocate.goToOverride = (view, options) => {options.target.scale = 1500;return view.goTo(options.target);};</script>4 collapsed lines</body></html> -
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-mapcomponent, add thearcgis-trackcomponent in the top left.28 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display your location</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-locate slot="top-left"></arcgis-locate><arcgis-track slot="top-left"></arcgis-track></arcgis-map>15 collapsed lines<script type="module">// Get a reference to the locate componentconst locate = document.querySelector("arcgis-locate");// Override the default goTo behaviorlocate.goToOverride = (view, options) => {options.target.scale = 1500;return view.goTo(options.target);};</script></body></html> -
In the
<script>, add theGraphicmodule.47 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display your location</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-locate slot="top-left"></arcgis-locate><arcgis-track slot="top-left"></arcgis-track></arcgis-map><script type="module">// Get a reference to the locate componentconst locate = document.querySelector("arcgis-locate");// Override the default goTo behaviorlocate.goToOverride = (view, options) => {options.target.scale = 1500;return view.goTo(options.target);};const Graphic = await $arcgis.import("@arcgis/core/Graphic.js");6 collapsed lines</script></body></html> -
Get a reference to the track component and update the graphic with a simple green symbol.
47 collapsed lines<html><head><meta charset="utf-8" /><meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" /><title>ArcGIS Maps SDK for JavaScript Tutorials: Display your location</title><style>html,body {height: 100%;margin: 0;}</style><script>var esriConfig = {apiKey: "YOUR_ACCESS_TOKEN",};</script><!-- Load the ArcGIS Maps SDK for JavaScript from CDN --><script type="module" src="https://js.arcgis.com/5.0/"></script></head><body><arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2"><arcgis-zoom slot="top-left"></arcgis-zoom><arcgis-locate slot="top-left"></arcgis-locate><arcgis-track slot="top-left"></arcgis-track></arcgis-map><script type="module">// Get a reference to the locate componentconst locate = document.querySelector("arcgis-locate");// Override the default goTo behaviorlocate.goToOverride = (view, options) => {options.target.scale = 1500;return view.goTo(options.target);};const Graphic = await $arcgis.import("@arcgis/core/Graphic.js");// Get a reference to the track componentconst track = document.querySelector("arcgis-track");// Update the graphic symboltrack.graphic = new Graphic({symbol: {type: "simple-marker",size: "12px",color: "green",outline: {color: "#efefef",width: "1.5px",},},});6 collapsed lines</script></body></html>
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 SDK features and ArcGIS services in these tutorials: