Add live realtime data
Use realtime GeoJSON data streams to move a symbol on your map.
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maps-sdk.trimblemaps.com/v4/trimblemaps-4.2.5.css" />
<script src="https://maps-sdk.trimblemaps.com/v4/trimblemaps-4.2.5.js"></script>
<style>
body { margin: 0; padding: 0; }
html,body,#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
TrimbleMaps.setAPIKey('YOUR_API_KEY_HERE');
const map = new TrimbleMaps.Map({
container: 'map',
style: TrimbleMaps.Common.Style.TRANSPORTATION,
zoom: 0,
});
map.on('load', () => {
window.setInterval(() => {
// Make a GET request to get two random numbers
fetch(
'https://www.random.org/decimal-fractions/?num=2&dec=10&col=1&format=plain&rnd=new'
)
.then((r) => r.text())
.then((text) => {
// Takes the two random numbers between 0 and 1 and converts them to degrees
const coordinates = text
.split('\n')
.map((l) => Number(l) * 180 - 90);
const json = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates,
},
};
// Update the drone symbol's location on the map
map.getSource('drone').setData(json);
// Fly the map to the drone's current location
map.flyTo({
center: json.geometry.coordinates,
speed: 0.5,
});
});
}, 2000);
// Set initial location at (0,0).
map.addSource('drone', {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [0, 0],
},
},
});
map.addLayer({
id: 'drone',
type: 'symbol',
source: 'drone',
layout: {
'icon-image': 'rocket_15',
},
});
});
</script>
</body>
</html>
Add live realtime data
<!doctype html>
<html lang="en">
<head>
<meta charset="utf-8" />
<link rel="stylesheet" href="https://maps-sdk.trimblemaps.com/v4/trimblemaps-4.2.5.css" />
<script src="https://maps-sdk.trimblemaps.com/v4/trimblemaps-4.2.5.js"></script>
<style>
body { margin: 0; padding: 0; }
html,body,#map {
height: 100%;
}
</style>
</head>
<body>
<div id="map"></div>
<script>
TrimbleMaps.setAPIKey('YOUR_API_KEY_HERE');
const map = new TrimbleMaps.Map({
container: 'map',
style: TrimbleMaps.Common.Style.TRANSPORTATION,
zoom: 0,
});
map.on('load', () => {
window.setInterval(() => {
// Make a GET request to get two random numbers
fetch(
'https://www.random.org/decimal-fractions/?num=2&dec=10&col=1&format=plain&rnd=new'
)
.then((r) => r.text())
.then((text) => {
// Takes the two random numbers between 0 and 1 and converts them to degrees
const coordinates = text
.split('\n')
.map((l) => Number(l) * 180 - 90);
const json = {
type: 'Feature',
geometry: {
type: 'Point',
coordinates,
},
};
// Update the drone symbol's location on the map
map.getSource('drone').setData(json);
// Fly the map to the drone's current location
map.flyTo({
center: json.geometry.coordinates,
speed: 0.5,
});
});
}, 2000);
// Set initial location at (0,0).
map.addSource('drone', {
type: 'geojson',
data: {
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [0, 0],
},
},
});
map.addLayer({
id: 'drone',
type: 'symbol',
source: 'drone',
layout: {
'icon-image': 'rocket_15',
},
});
});
</script>
</body>
</html>