Skip to content
Branch: master
Find file Copy path
Find file Copy path
1 contributor

Users who have contributed to this file

111 lines (95 sloc) 5 KB
<!DOCTYPE html>
<title>Continuous User Position Tracking - Azure Maps Web SDK Samples</title>
<meta charset="utf-8" />
<meta http-equiv="x-ua-compatible" content="IE=Edge" />
<meta name="viewport" content="width=device-width, initial-scale=1, shrink-to-fit=no" />
<meta name="description" content="This sample shows how to continuously get the users position information from the browser by using the HTML5 geolocation API and update it on a map." />
<meta name="keywords" content="map, gis, API, SDK, geolocation, user, location, position, tracking, gps, gps tracking" />
<meta name="author" content="Microsoft Azure Maps" />
<!-- Add references to the Azure Maps Map control JavaScript and CSS files. -->
<link rel="stylesheet" href="" type="text/css" />
<script src=""></script>
<script type='text/javascript'>
var map, watchId, datasource, userShape;
function GetMap() {
//Initialize a map instance.
map = new atlas.Map('myMap', {
view: 'Auto',
//Add your Azure Maps subscription key to the map SDK. Get an Azure Maps key at
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
//Wait until the map resources are ready.'ready', function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
//Add a layer for rendering the users location as a symbol.
map.layers.add(new atlas.layer.SymbolLayer(datasource));
function startTracking() {
if (!watchId) {
//Watch the users position.
watchId = navigator.geolocation.watchPosition(function (geoPosition) {
//Get the coordinate information from the geoPosition.
var userPosition = [geoPosition.coords.longitude, geoPosition.coords.latitude];
//TIP: altitude? in meters, speed? in meters/second and heading? in degrees are also potential properties of geoPosition.coords
if (!userShape) {
//Create a shape to show the users position and add it to the data source.
userShape = new atlas.Shape(new, geoPosition));
} else {
//Center the map on the users position.
center: userPosition,
zoom: 15
}, function (error) {
//If an error occurs when trying to access the users position information, display an error message.
switch (error.code) {
alert('User denied the request for Geolocation.');
alert('Position information is unavailable.');
case error.TIMEOUT:
alert('The request to get user position timed out.');
case error.UNKNOWN_ERROR:
alert('An unknown error occurred.');
function stopTracking() {
//Cancel the geolocation updates.
//Clear all data from the map.
userShape = null;
watchId = null;
<body onload="GetMap()">
<div id="myMap" style="position:relative;width:100%;min-width:290px;height:600px;"></div>
<div style="position:absolute;top:15px;left:15px;background-color:white;padding:10px;border-radius:10px;">
<input type="button" value="Start Continuous Tracking" onclick="startTracking()" />
<input type="button" value="Stop Continuous Tracking" onclick="stopTracking()" />
<fieldset style="width:calc(100% - 30px);min-width:290px;margin-top:10px;">
<legend>Continuous User Position Tracking</legend>
This sample shows how to continuously get the users position information from the browser by using the HTML5 geolocation API and update it on a map.
You can’t perform that action at this time.