aboutsummaryrefslogtreecommitdiff
path: root/src/map/MapScale.js
blob: c8a724c9048978e2ad124deee2294919cb4e10d9 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
import maplibregl from 'maplibre-gl';
import { useEffect, useMemo } from 'react';
import { useAttributePreference } from '../common/util/preferences';
import { map } from './core/MapView';

const MapScale = () => {
  const distanceUnit = useAttributePreference('distanceUnit');

  const control = useMemo(() => new maplibregl.ScaleControl(), []);

  useEffect(() => {
    map.addControl(control, 'bottom-right');
    return () => map.removeControl(control);
  }, [control]);

  useEffect(() => {
    switch (distanceUnit) {
      case 'mi':
        control.setUnit('imperial');
        break;
      case 'nmi':
        control.setUnit('nautical');
        break;
      case 'km':
      default:
        control.setUnit('metric');
        break;
    }
  }, [control, distanceUnit]);

  return null;
};

export default MapScale;