Skip to content

Commit 0d60329

Browse files
committed
Support Mapbox terrain (#1483)
1 parent b7fa030 commit 0d60329

File tree

2 files changed

+19
-28
lines changed

2 files changed

+19
-28
lines changed

src/components/interactive-map.js

+5-10
Original file line numberDiff line numberDiff line change
@@ -2,9 +2,8 @@ import * as React from 'react';
22
import {useContext, useRef, useMemo, useEffect, useImperativeHandle, forwardRef} from 'react';
33
import * as PropTypes from 'prop-types';
44

5-
import StaticMap from './static-map';
5+
import StaticMap, {getViewport} from './static-map';
66
import {MAPBOX_LIMITS} from '../utils/map-state';
7-
import WebMercatorViewport from 'viewport-mercator-project';
87

98
import TransitionManager from '../utils/transition-manager';
109
import MapContext, {MapContextProvider} from './map-context';
@@ -145,15 +144,9 @@ function normalizeEvent(event) {
145144
}
146145
const pos = [x, y];
147146

148-
const viewport = new WebMercatorViewport(
149-
Object.assign({}, this.props, this.props.viewState, {
150-
width: this.width,
151-
height: this.height
152-
})
153-
);
154-
155147
event.point = pos;
156-
event.lngLat = viewport.unproject(pos);
148+
149+
event.lngLat = this.viewport.unproject(pos);
157150

158151
return event;
159152
}
@@ -351,6 +344,8 @@ const InteractiveMap = forwardRef((props, ref) => {
351344
[parentContext, eventCanvasRef.current]
352345
);
353346
context.onViewportChange = handleViewportChange;
347+
context.viewport = parentContext.viewport || getViewport(thisRef);
348+
thisRef.viewport = context.viewport;
354349

355350
const handleInteractionStateChange = interactionState => {
356351
const {isDragging = false} = interactionState;

src/components/static-map.js

+14-18
Original file line numberDiff line numberDiff line change
@@ -38,6 +38,15 @@ const NO_TOKEN_WARNING = 'A valid API access token is required to use Mapbox dat
3838

3939
function noop() {}
4040

41+
export function getViewport({props, width, height}) {
42+
return new WebMercatorViewport({
43+
...props,
44+
...props.viewState,
45+
width,
46+
height
47+
});
48+
}
49+
4150
const UNAUTHORIZED_ERROR_CODE = 401;
4251

4352
const CONTAINER_STYLE = {
@@ -112,7 +121,7 @@ function getRefHandles(mapboxRef) {
112121

113122
const StaticMap = forwardRef((props, ref) => {
114123
const [accessTokenValid, setTokenState] = useState(true);
115-
const [size, setSize] = useState([0, 0]);
124+
const [size, setSize] = useState({width: 0, height: 0});
116125
const mapboxRef = useRef(null);
117126
const mapDivRef = useRef(null);
118127
const containerRef = useRef(null);
@@ -127,9 +136,8 @@ const StaticMap = forwardRef((props, ref) => {
127136
// Initialize
128137
const mapbox = new Mapbox({
129138
...props,
139+
...size,
130140
mapboxgl, // Handle to mapbox-gl library
131-
width: size[0],
132-
height: size[1],
133141
container: mapDivRef.current,
134142
onError: evt => {
135143
const statusCode = (evt.error && evt.error.status) || evt.status;
@@ -150,7 +158,7 @@ const StaticMap = forwardRef((props, ref) => {
150158
const resizeObserver = new ResizeObserver(entries => {
151159
if (entries[0].contentRect) {
152160
const {width, height} = entries[0].contentRect;
153-
setSize([width, height]);
161+
setSize({width, height});
154162
props.onResize({width, height});
155163
}
156164
});
@@ -166,12 +174,7 @@ const StaticMap = forwardRef((props, ref) => {
166174

167175
useIsomorphicLayoutEffect(() => {
168176
if (mapboxRef.current) {
169-
mapboxRef.current.setProps(
170-
Object.assign({}, props, {
171-
width: size[0],
172-
height: size[1]
173-
})
174-
);
177+
mapboxRef.current.setProps({...props, ...size});
175178
}
176179
});
177180

@@ -191,14 +194,7 @@ const StaticMap = forwardRef((props, ref) => {
191194
<MapContextProvider
192195
value={{
193196
...context,
194-
viewport:
195-
context.viewport ||
196-
new WebMercatorViewport({
197-
...props,
198-
...props.viewState,
199-
width: size[0],
200-
height: size[1]
201-
}),
197+
viewport: context.viewport || getViewport({map, props, ...size}),
202198
map,
203199
container: context.container || containerRef.current
204200
}}

0 commit comments

Comments
 (0)