Skip to content
Permalink
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>
<html>
<head>
<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="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.css" type="text/css" />
<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/2/atlas.min.js"></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 https://azure.com/maps
authOptions: {
authType: 'subscriptionKey',
subscriptionKey: '<Your Azure Maps Key>'
}
});
//Wait until the map resources are ready.
map.events.add('ready', function () {
//Create a data source and add it to the map.
datasource = new atlas.source.DataSource();
map.sources.add(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 atlas.data.Feature(new atlas.data.Point(userPosition), geoPosition));
datasource.add(userShape);
} else {
userShape.setCoordinates(userPosition);
userShape.setProperties(geoPosition);
}
//Center the map on the users position.
map.setCamera({
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) {
case error.PERMISSION_DENIED:
alert('User denied the request for Geolocation.');
break;
case error.POSITION_UNAVAILABLE:
alert('Position information is unavailable.');
break;
case error.TIMEOUT:
alert('The request to get user position timed out.');
break;
case error.UNKNOWN_ERROR:
alert('An unknown error occurred.');
break;
}
});
}
}
function stopTracking() {
//Cancel the geolocation updates.
navigator.geolocation.clearWatch(watchId);
//Clear all data from the map.
datasource.clear();
userShape = null;
watchId = null;
}
</script>
</head>
<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()" />
</div>
<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.
</fieldset>
</body>
</html>
You can’t perform that action at this time.