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.
NoteAccessing the device location with the Geolocation API is not supported on insecure origins. Therefore, to use the Locate and Track components, the tutorial must run on HTTPS. In most cases you can also use localhost for testing in browsers that supports Window.isSecureContext. Learn more here Locate component.
Prerequisites ArcGIS Accounts:You need an ArcGIS Location Platform or ArcGIS Online account.
Steps Create a new penYou need an access token with the correct privileges to access the location services used in this tutorial.
esriConfig.apiKey
to your access token.
Use dark colors for code blocks
1
2
3
4
5
6
7
8
9
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 positionarcgis/navigation
basemap. This basemap is optimized for navigation. Set the map to be zoomed out to the world.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<arcgis-map basemap="arcgis/navigation" center="-40,28" zoom="2">
<arcgis-zoom position="top-left"></arcgis-zoom>
</arcgis-map>
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 the arcgis-locate
component in the top left.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<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 the goToOverride
property to provide your own custom zoom functionality for the component. In this case, it will zoom the map to a scale of 1500
.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<script type="module">
// 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.
If you have not previously allowed your browser to use your location, the browser will ask for permission before accessing the device location. You must approve this request in order for the Track component to work. If you are using CodePen, the component may not work in Editor view. If this happens, change to Debug view and run the app again.
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 the arcgis-track
component in the top left.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
<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.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const Graphic = await $arcgis.import("@arcgis/core/Graphic.js");
Get a reference to the track component and update the graphic with a simple green symbol.
Expand
Use dark colors for code blocks1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
const Graphic = await $arcgis.import("@arcgis/core/Graphic.js");
// 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",
},
},
});
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.
Learn how to use additional API features and ArcGIS services in these tutorials:
RetroSearch is an open source project built by @garambo | Open a GitHub Issue
Search and Browse the WWW like it's 1997 | Search results from DuckDuckGo
HTML:
3.2
| Encoding:
UTF-8
| Version:
0.7.4