Add live realtime data
This example queries an external data feed for the realtime location of the International Space Station (ISS) and displays it on a map. setInterval
is used to fetch the latest location every second. The ISS is moving at a speed of 28,000 km/h (17,500 mph), so the location updates often.
The longitude and latitude coordinates are converted to GeoJSON, which is used to update GeoJSONSource
. A symbol
layer with a custom icon shows the location of the ISS on the map.
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>Add live realtime data</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no">
<link href="https://api.mapbox.com/mapbox-gl-js/v3.12.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.12.0/mapbox-gl.js"></script>
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
const map = new mapboxgl.Map({
container: 'map',
// Choose from Mapbox's core styles, or make your own style with Mapbox Studio
style: 'mapbox://styles/mapbox/standard',
zoom: 1.5
});
map.on('load', () => {
map.loadImage('/mapbox-gl-js/assets/iss.png', async (error, image) => {
if (error) throw error;
// Add the image to the map style.
map.addImage('iss', image);
// Get the initial location of the International Space Station (ISS).
const geojson = await getLocation();
// Add the ISS location as a source.
map.addSource('iss', {
type: 'geojson',
data: geojson
});
// Add the rocket symbol layer to the map.
map.addLayer({
'id': 'iss',
'type': 'symbol',
'source': 'iss',
'layout': {
'icon-image': 'iss'
}
});
// Update the source from the API every 2 seconds.
const updateSource = setInterval(async () => {
const geojson = await getLocation(updateSource);
map.getSource('iss').setData(geojson);
}, 2000);
async function getLocation(updateSource) {
// Make a GET request to the API and return the location of the ISS.
try {
const response = await fetch(
'https://api.wheretheiss.at/v1/satellites/25544',
{ method: 'GET' }
);
const { latitude, longitude } = await response.json();
// Fly the map to the location.
map.flyTo({
center: [longitude, latitude],
speed: 0.5
});
// Return the location of the ISS as GeoJSON.
return {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'Point',
'coordinates': [longitude, latitude]
}
}
]
};
} catch (err) {
// If the updateSource interval is defined, clear the interval to stop updating the source.
if (updateSource) clearInterval(updateSource);
throw new Error(err);
}
}
});
});
</script>
</body>
</html>
このコードスニペットは、
YOUR_MAPBOX_ACCESS_TOKEN
をあなたのMapboxアカウントのアクセストークンに置き換えるまで、期待通りに動作しません。import React, { useEffect, useRef, useState } from 'react';
import mapboxgl from 'mapbox-gl';
import 'mapbox-gl/dist/mapbox-gl.css';
const MapboxExample = () => {
const mapContainerRef = useRef();
const mapRef = useRef();
const [data, setData] = useState();
useEffect(() => {
// TO MAKE THE MAP APPEAR YOU MUST
// ADD YOUR ACCESS TOKEN FROM
// https://account.mapbox.com
mapboxgl.accessToken = 'YOUR_MAPBOX_ACCESS_TOKEN';
mapRef.current = new mapboxgl.Map({
container: mapContainerRef.current,
style: 'mapbox://styles/mapbox/standard',
zoom: 1.5
});
mapRef.current.on('load', () => {
mapRef.current.loadImage(
'/mapbox-gl-js/assets/iss.png',
async (error, image) => {
if (error) throw error;
// Add the image to the map style.
mapRef.current.addImage('iss', image);
// Get the initial location of the International Space Station (ISS).
const geojson = await getLocation();
// Add the ISS location as a source.
mapRef.current.addSource('iss', {
type: 'geojson',
data: geojson
});
// Add the rocket symbol layer to the mapRef.current.
mapRef.current.addLayer({
id: 'iss',
type: 'symbol',
source: 'iss',
layout: {
'icon-image': 'iss'
}
});
// Update the source from the API every 2 seconds.
const updateSource = setInterval(async () => {
const geojson = await getLocation(updateSource);
mapRef.current.getSource('iss').setData(geojson);
}, 2000);
async function getLocation(updateSource) {
// Make a GET request to the API and return the location of the ISS.
try {
const response = await fetch(
'https://api.wheretheiss.at/v1/satellites/25544',
{ method: 'GET' }
);
const { latitude, longitude } = await response.json();
// Fly the map to the location.
mapRef.current.flyTo({
center: [longitude, latitude],
speed: 0.5
});
// Return the location of the ISS as GeoJSON.
return {
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [longitude, latitude]
}
}
]
};
} catch (err) {
// If the updateSource interval is defined, clear the interval to stop updating the source.
if (updateSource) clearInterval(updateSource);
throw new Error(err);
}
}
}
);
});
return () => {
mapRef.current.remove();
};
}, []);
useEffect(() => {
if (!data) return;
mapRef.current.getSource('iss').setData(data);
mapRef.current.flyTo({
center: data.features[0].geometry.coordinates,
speed: 0.5
});
}, [data]);
return <div ref={mapContainerRef} id="map" style={{ height: '100%' }} />;
};
export default MapboxExample;
このコードスニペットは、
YOUR_MAPBOX_ACCESS_TOKEN
をあなたのMapboxアカウントのアクセストークンに置き換えるまで、期待通りに動作しません。このexampleは役に立ちましたか?