Add live realtime data
Use realtime location data streams to move a symbol
on your map with setInterval
.
Referencing images in a style's sprite
This example uses the Mapbox Streets style. The icon-image
used in this example comes from the Mapbox Streets style's sprite.
To view all available images in a style's sprite or add additional images, open the style in Mapbox Studio and click the Images tab. To add a new image to the style at runtime see the Add an icon to the map example.
<!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.8.0/mapbox-gl.css" rel="stylesheet">
<script src="https://api.mapbox.com/mapbox-gl-js/v3.8.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/streets-v12',
zoom: 1.5
});
map.on('load', async () => {
// 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': {
// This icon is a part of the Mapbox Streets style.
// To view all images available in a Mapbox style, open
// the style in Mapbox Studio and click the "Images" tab.
// To add a new image to the style at runtime see
// https://docs.mapbox.com/mapbox-gl-js/example/add-image/
'icon-image': 'rocket'
}
});
// 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/streets-v12',
zoom: 1.5
});
mapRef.current.on('load', async () => {
const geojson = await getLocation();
mapRef.current.addSource('iss', {
type: 'geojson',
data: geojson
});
mapRef.current.addLayer({
id: 'iss',
type: 'symbol',
source: 'iss',
layout: {
'icon-image': 'rocket'
}
});
const updateSource = setInterval(async () => {
await getLocation(updateSource);
}, 2000);
async function getLocation(updateSource) {
try {
const response = await fetch(
'https://api.wheretheiss.at/v1/satellites/25544',
{ method: 'GET' }
);
const { latitude, longitude } = await response.json();
setData({
type: 'FeatureCollection',
features: [
{
type: 'Feature',
geometry: {
type: 'Point',
coordinates: [longitude, latitude]
}
}
]
});
} catch (err) {
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は役に立ちましたか?