Skip to content

Kepler.gl is a powerful open source geospatial analysis tool for large-scale data sets.

feat: zoom and compass control (#3438)

Igor Dykhta259667648e5e
diff --git a/src/components/src/index.ts b/src/components/src/index.ts
index c4a9201f9..c664ea233 100644
--- a/src/components/src/index.ts
+++ b/src/components/src/index.ts
@@ -153,6 +153,7 @@ export {default as LayerSelectorPanelFactory} from './map/layer-selector-panel';
export {default as LazyTippy} from './map/lazy-tippy';
export {default as LocalePanelFactory} from './map/locale-panel';
export {default as MapControlFactory} from './map/map-control';
+export {default as MapNavigationControlFactory} from './map/map-navigation-control';
export {default as MapControlPanelFactory} from './map/map-control-panel';
export {default as MapControlToolbarFactory} from './map/map-control-toolbar';
export {default as MapControlTooltipFactory} from './map/map-control-tooltip';
@@ -378,6 +379,7 @@ export type {FeatureActionPanelProps} from './editor/feature-action-panel';
export type {PlaybackControlsProps} from './common/animation-control/playback-controls';
export type {MapContainerProps} from './map-container';
export type {MapControlProps} from './map/map-control';
+export type {MapNavigationControlProps} from './map/map-navigation-control';
export type {MapDrawPanelProps} from './map/map-draw-panel';
export type {MapLegendPanelFactoryDeps, MapLegendPanelProps} from './map/map-legend-panel';
export type {DatasetInfoProps} from './side-panel/common/dataset-info';
diff --git a/src/components/src/map-container.tsx b/src/components/src/map-container.tsx
index 09b7b3e43..394a17d91 100644
--- a/src/components/src/map-container.tsx
+++ b/src/components/src/map-container.tsx
@@ -1342,6 +1342,7 @@ export default function MapContainerFactory(
<>
<MapControl
mapState={mapState}
+ mapStateActions={mapStateActions}
datasets={datasets}
availableLocales={LOCALE_CODES_ARRAY}
dragRotate={mapState.dragRotate}
diff --git a/src/components/src/map/map-control.tsx b/src/components/src/map/map-control.tsx
index 1802a30e4..a2385271d 100644
--- a/src/components/src/map/map-control.tsx
+++ b/src/components/src/map/map-control.tsx
@@ -12,6 +12,7 @@ import LayerSelectorPanelFactory from './layer-selector-panel';
import MapLegendPanelFactory from './map-legend-panel';
import MapDrawPanelFactory from './map-draw-panel';
import LocalePanelFactory from './locale-panel';
+import MapNavigationControlFactory from './map-navigation-control';
import {Layer} from '@kepler.gl/layers';
import {Editor, LayerVisConfig, MapControls, MapState} from '@kepler.gl/types';
import {Datasets} from '@kepler.gl/table';
@@ -77,6 +78,7 @@ export type MapControlProps = {
// optional
mapState?: MapState;
+ mapStateActions?: typeof MapStateActions;
readOnly?: boolean;
scale?: number;
mapLayers?: {[key: string]: boolean};
@@ -92,7 +94,8 @@ MapControlFactory.deps = [
MapLegendPanelFactory,
MapDrawPanelFactory,
LocalePanelFactory,
- AnnotationControlFactory
+ AnnotationControlFactory,
+ MapNavigationControlFactory
];
function MapControlFactory(
@@ -102,7 +105,8 @@ function MapControlFactory(
MapLegendPanel: ReturnType<typeof MapLegendPanelFactory>,
MapDrawPanel: ReturnType<typeof MapDrawPanelFactory>,
LocalePanel: ReturnType<typeof LocalePanelFactory>,
- AnnotationControl: ReturnType<typeof AnnotationControlFactory>
+ AnnotationControl: ReturnType<typeof AnnotationControlFactory>,
+ MapNavigationControl: ReturnType<typeof MapNavigationControlFactory>
) {
const DEFAULT_ACTIONS = [
SplitMapButton,
@@ -122,12 +126,15 @@ function MapControlFactory(
top = 0,
mapIndex = 0,
logoComponent = LegendLogo,
+ mapState,
+ mapStateActions,
...restProps
}) => {
const actionComponentProps = {
isSplit,
mapIndex,
logoComponent,
+ mapState,
...restProps
};
return (
@@ -135,6 +142,13 @@ function MapControlFactory(
{actionComponents.map((ActionComponent, index) => (
<ActionComponent key={index} className="map-control-action" {...actionComponentProps} />
))}
+ {mapState && mapStateActions ? (
+ <MapNavigationControl
+ mapState={mapState}
+ mapIndex={mapIndex}
+ mapStateActions={mapStateActions}
+ />
+ ) : null}
</StyledMapControl>
);
};
diff --git a/src/components/src/map/map-navigation-control.tsx b/src/components/src/map/map-navigation-control.tsx
new file mode 100644
index 000000000..5bb7e145d
--- /dev/null
+++ b/src/components/src/map/map-navigation-control.tsx
@@ -0,0 +1,163 @@
+// SPDX-License-Identifier: MIT
+// Copyright contributors to the kepler.gl project
+
+import React, {useCallback, useContext, useMemo} from 'react';
+import styled, {useTheme} from 'styled-components';
+import {MapViewStateContext, MapViewStateContextType} from '../map-view-state-context';
+import {MapControlButton} from '../common/styled-components';
+import {MapState} from '@kepler.gl/types';
+import {MapStateActions} from '@kepler.gl/actions';
+import {getApplicationConfig} from '@kepler.gl/utils';
+
+export type MapNavigationControlProps = {
+ mapState: MapState;
+ mapIndex: number;
+ mapStateActions: typeof MapStateActions;
+};
+
+const StyledMapNavigationControl = styled.div`
+ display: flex;
+ gap: 8px;
+ flex-direction: column;
+ align-items: flex-end;
+ margin-top: 12px;
+`;
+
+const NavButtonGroup = styled.div`
+ display: flex;
+ flex-direction: column;
+ gap: 0;
+ overflow: hidden;
+ box-shadow: 0 6px 12px 0 rgba(0, 0, 0, 0.16);
+`;
+
+interface NavButtonProps {
+ $isFirst?: boolean;
+}
+
+const NavButton = styled(MapControlButton)<NavButtonProps>`
+ box-shadow: none;
+ border-top: ${props => (props.$isFirst ? 'none' : `1px solid ${props.theme.panelBorderColor}`)};
+`;
+
+MapNavigationControlFactory.deps = [];
+
+export default function MapNavigationControlFactory() {
+ const MapNavigationControl: React.FC<MapNavigationControlProps> = ({
+ mapState,
+ mapIndex,
+ mapStateActions
+ }) => {
+ const {getInternalViewState} = useContext<MapViewStateContextType>(MapViewStateContext);
+ const viewState = getInternalViewState(mapIndex);
+ const theme = useTheme();
+
+ const isEnabled = getApplicationConfig().enableMapNavigationControl;
+
+ const handleZoomIn = useCallback(() => {
+ const currentZoom = viewState?.zoom ?? mapState.zoom;
+ let nextZoom = currentZoom + 1;
+ if (mapState.maxZoom != null) {
+ nextZoom = Math.min(mapState.maxZoom, nextZoom);
+ }
+ mapStateActions.updateMap({zoom: nextZoom}, mapIndex);
+ }, [viewState, mapState, mapStateActions, mapIndex]);
+
+ const handleZoomOut = useCallback(() => {
+ const currentZoom = viewState?.zoom ?? mapState.zoom;
+ let nextZoom = currentZoom - 1;
+ if (mapState.minZoom != null) {
+ nextZoom = Math.max(mapState.minZoom, nextZoom);
+ }
+ mapStateActions.updateMap({zoom: nextZoom}, mapIndex);
+ }, [viewState, mapState, mapStateActions, mapIndex]);
+
+ const handleCompassClick = useCallback(() => {
+ const currentBearing = viewState?.bearing ?? mapState.bearing;
+ const currentPitch = viewState?.pitch ?? mapState.pitch;
+ if (currentBearing === 0 && currentPitch === 0) {
+ return;
+ }
+ if (currentBearing !== 0) {
+ mapStateActions.updateMap({bearing: 0}, mapIndex);
+ } else {
+ mapStateActions.updateMap({bearing: 0, pitch: 0}, mapIndex);
+ }
+ }, [viewState, mapState, mapStateActions, mapIndex]);
+
+ const rotation = useMemo(() => {
+ const bearing = -(viewState?.bearing ?? mapState.bearing ?? 0);
+ const pitch = viewState?.pitch ?? mapState.pitch ?? 0;
+ return {bearing, pitch};
+ }, [viewState, mapState]);
+
+ if (!isEnabled) {
+ return null;
+ }
+
+ return (
+ <StyledMapNavigationControl className="map-navigation-control">
+ <NavButtonGroup>
+ <NavButton
+ $isFirst
+ onClick={handleZoomIn}
+ title="Zoom in"
+ className="map-navigation-control__zoom-in"
+ >
+ <svg
+ fill="none"
+ width="18"
+ height="18"
+ viewBox="0 0 24 24"
+ strokeWidth="2"
+ stroke="currentColor"
+ >
+ <path d="M12 4.5v15m7.5-7.5h-15" />
+ </svg>
+ </NavButton>
+ <NavButton
+ onClick={handleZoomOut}
+ title="Zoom out"
+ className="map-navigation-control__zoom-out"
+ >
+ <svg
+ fill="none"
+ width="18"
+ height="18"
+ viewBox="0 0 24 24"
+ strokeWidth="2"
+ stroke="currentColor"
+ >
+ <path d="M19.5 12h-15" />
+ </svg>
+ </NavButton>
+ </NavButtonGroup>
+ {mapState.dragRotate ? (
+ <MapControlButton
+ onClick={handleCompassClick}
+ title="Reset bearing/pitch"
+ className="map-navigation-control__compass"
+ >
+ <div style={{transform: `rotateX(${rotation.pitch}deg)`}}>
+ <svg fill="none" width="18" height="18" viewBox="0 0 16 16">
+ <g transform={`rotate(${rotation.bearing},8,8)`}>
+ <path
+ d="M5 8.00006L7.99987 0L10.9997 8.00006H5Z"
+ fill={theme.notificationColors?.error || '#F05C44'}
+ />
+ <path
+ d="M11.0002 7.99994L8.00038 16L5.00051 7.99994L11.0002 7.99994Z"
+ fill="currentColor"
+ />
+ </g>
+ </svg>
+ </div>
+ </MapControlButton>
+ ) : null}
+ </StyledMapNavigationControl>
+ );
+ };
+
+ MapNavigationControl.displayName = 'MapNavigationControl';
+ return React.memo(MapNavigationControl);
+}
diff --git a/src/utils/src/application-config.ts b/src/utils/src/application-config.ts
index 6b8ba618f..f728e438f 100644
--- a/src/utils/src/application-config.ts
+++ b/src/utils/src/application-config.ts
@@ -99,6 +99,9 @@ export type KeplerApplicationConfig = {
/** Whether to enable the annotations feature. Enabled by default. */
enableAnnotations?: boolean;
+
+ /** Whether to show the map navigation control (zoom buttons and compass). Enabled by default. */
+ enableMapNavigationControl?: boolean;
};
const DEFAULT_APPLICATION_CONFIG: Required<KeplerApplicationConfig> = {
@@ -164,7 +167,9 @@ const DEFAULT_APPLICATION_CONFIG: Required<KeplerApplicationConfig> = {
maxPitch: 60,
- enableAnnotations: true
+ enableAnnotations: true,
+
+ enableMapNavigationControl: true
};
const applicationConfig: Required<KeplerApplicationConfig> = DEFAULT_APPLICATION_CONFIG;