Skip to content

Commit 3d498ce

Browse files
committed
Experimentation with newer versions of Mapbox & MapLibre GL
1 parent 4d54e58 commit 3d498ce

File tree

6 files changed

+390
-164
lines changed

6 files changed

+390
-164
lines changed

gatsby-config.js

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -94,8 +94,8 @@ module.exports = {
9494
"child-src": "'self' blob:",
9595
"connect-src":
9696
process.env.NODE_ENV === "development"
97-
? "'self' https://admg.nasa-impact.net https://www.google-analytics.com https://*.tiles.mapbox.com https://api.mapbox.com https://events.mapbox.com http://localhost:* ws://localhost:*"
98-
: "'self' https://admg.nasa-impact.net https://www.google-analytics.com https://*.tiles.mapbox.com https://api.mapbox.com https://events.mapbox.com https://admgstaging.nasa-impact.net/api/unpublished_drafts",
97+
? "'self' https://admg.nasa-impact.net https://www.google-analytics.com https://*.tiles.mapbox.com https://api.mapbox.com https://events.mapbox.com http://localhost:* ws://localhost:* https://demotiles.maplibre.org https://*.cartocdn.com"
98+
: "'self' https://admg.nasa-impact.net https://www.google-analytics.com https://*.tiles.mapbox.com https://api.mapbox.com https://events.mapbox.com https://admgstaging.nasa-impact.net/api/unpublished_drafts https://demotiles.maplibre.org https://*.cartocdn.com",
9999
},
100100
},
101101
},

package.json

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -52,13 +52,15 @@
5252
"gatsby-transformer-json": "^5.13.1",
5353
"gatsby-transformer-sharp": "^5.13.1",
5454
"lodash.debounce": "^4.0.8",
55-
"mapbox-gl": "1.13.1",
55+
"mapbox-gl": "3.10.0",
56+
"maplibre-gl": "^5.2.0",
5657
"nuka-carousel": "^4.8.4",
5758
"prop-types": "^15.8.1",
5859
"react": "^18.3.1",
5960
"react-datepicker": "^4.7.0",
6061
"react-dom": "^18.3.1",
6162
"react-helmet": "^6.1.0",
63+
"react-map-gl": "^8.0.1",
6264
"react-tooltip": "^5.26.3",
6365
"react-typography": "^0.16.20",
6466
"sharp": "^0.33.4",

src/components/deployments/map.js

Lines changed: 1 addition & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -121,19 +121,7 @@ export const DeploymentMap = ({
121121
selectedPlatforms={selectedPlatforms}
122122
selectedDeployment={selectedDeployment}
123123
mapStyleID={MAP_STYLE_ID}
124-
>
125-
<MapLegend
126-
platforms={platforms}
127-
platformsWithData={platformsWithData}
128-
activeDeploymentPlatforms={activeDeploymentPlatforms}
129-
selectedPlatforms={selectedPlatforms}
130-
setSelectedPlatforms={setSelectedPlatforms}
131-
/>
132-
<MapViewControl
133-
onClick={() => setEnable3DView(!enable3DView)}
134-
is3DViewEnabled={enable3DView}
135-
/>
136-
</GlobeMap>
124+
/>
137125
) : (
138126
<MapboxMap
139127
geojson={geojson}

src/components/map/globe-map.js

Lines changed: 30 additions & 74 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
1-
import React, { useEffect, useState, useMemo } from "react"
1+
import React, { useEffect, useState } from "react"
22
import { centroid } from "@turf/centroid"
3-
import DeckGL from "@deck.gl/react"
3+
import { Map, useControl, useMap } from "react-map-gl/mapbox"
4+
import { MapboxOverlay } from "@deck.gl/mapbox"
45
import {
56
COORDINATE_SYSTEM,
67
_GlobeView as GlobeView,
@@ -22,6 +23,7 @@ import {
2223
import "./deck-gl.css"
2324
import { colors } from "../../theme"
2425
import { MOVING_PLATFORMS } from "../../utils/constants"
26+
import "maplibre-gl/dist/maplibre-gl.css"
2527

2628
const INITIAL_VIEW_STATE = {
2729
longitude: -98,
@@ -30,6 +32,20 @@ const INITIAL_VIEW_STATE = {
3032
}
3133
const MAPBOX_TOKEN = process.env.GATSBY_MAPBOX_TOKEN
3234

35+
function DeckGLOverlay(props) {
36+
const overlay = useControl(() => new MapboxOverlay(props))
37+
const { current: map } = useMap()
38+
39+
useEffect(() => {
40+
if (map) {
41+
map.flyTo({ center: [-90, 20], curve: 0.1, speed: 0.002 })
42+
}
43+
}, [map])
44+
45+
overlay.setProps(props)
46+
return null
47+
}
48+
3349
export function GlobeMap({
3450
geojson,
3551
deployments,
@@ -77,54 +93,13 @@ export function GlobeMap({
7793
longitude: lon,
7894
latitude: lat,
7995
zoom: 1,
80-
transitionInterpolator: new FlyToInterpolator({ speed: 1.5 }),
8196
transitionDuration: "auto",
8297
})
8398
}, [geojson])
8499

85-
const backgroundLayers = useMemo(
86-
() => [
87-
new SimpleMeshLayer({
88-
id: "earth-sphere",
89-
data: [0],
90-
mesh: new SphereGeometry({
91-
radius: 6.3e6,
92-
nlat: 18,
93-
nlong: 36,
94-
}),
95-
coordinateSystem: COORDINATE_SYSTEM.CARTESIAN,
96-
getPosition: [0, 0, 0],
97-
getColor: [42, 98, 163, 125],
98-
}),
99-
new TileLayer({
100-
id: "TileLayer",
101-
data: `https://api.mapbox.com/styles/v1/${mapStyleID}/tiles/256/{z}/{x}/{y}@2x?access_token=${MAPBOX_TOKEN}`,
102-
maxZoom: 22,
103-
minZoom: 3,
104-
extent: [-180, -90, 180, 90],
105-
tileSize: 256,
106-
renderSubLayers: props => {
107-
// eslint-disable-next-line react/prop-types
108-
const { boundingBox } = props.tile
109-
return new BitmapLayer(props, {
110-
data: null,
111-
// eslint-disable-next-line react/prop-types
112-
image: props.data,
113-
bounds: [
114-
boundingBox[0][0],
115-
boundingBox[0][1],
116-
boundingBox[1][0],
117-
boundingBox[1][1],
118-
],
119-
})
120-
},
121-
}),
122-
],
123-
[]
124-
)
125-
126100
const flights = new GeoJsonLayer({
127101
id: "flights",
102+
pickable: true,
128103
data: {
129104
...geojson,
130105
features: geojson.features
@@ -138,7 +113,7 @@ export function GlobeMap({
138113
),
139114
},
140115
lineWidthMinPixels: 0.5,
141-
getLineWidth: 1,
116+
getLineWidth: 1.5,
142117
getLineColor: f =>
143118
getLineColorAsRGB(
144119
movingPlatforms
@@ -168,37 +143,18 @@ export function GlobeMap({
168143
if (iconMapping && geojson) {
169144
return (
170145
<MapContainer>
171-
<DeckGL
172-
views={
173-
new GlobeView({
174-
controller: { keyboard: false, inertia: true },
175-
})
176-
}
177-
controller={true}
146+
<Map
147+
reuseMaps
148+
projection="globe"
149+
id="map"
150+
// mapStyle="https://basemaps.cartocdn.com/gl/dark-matter-gl-style/style.json"
151+
mapStyle={`mapbox://styles/${mapStyleID}`}
152+
mapboxAccessToken={MAPBOX_TOKEN}
178153
initialViewState={initialViewState}
179-
layers={[backgroundLayers, flights, staticLocations]}
180-
widgets={[
181-
new ZoomWidget({ placement: "bottom-right" }),
182-
new FullscreenWidget({ placement: "bottom-right" }),
183-
]}
184-
></DeckGL>
154+
>
155+
<DeckGLOverlay layers={[flights, staticLocations]} />
156+
</Map>
185157
{children}
186-
<Attribution>
187-
<a
188-
href="https://www.mapbox.com/about/maps/"
189-
target="_blank"
190-
rel="noreferrer"
191-
>
192-
© Mapbox
193-
</a>
194-
<a
195-
href="https://www.openstreetmap.org/about/"
196-
target="_blank"
197-
rel="noreferrer"
198-
>
199-
© OpenStreetMap
200-
</a>
201-
</Attribution>
202158
</MapContainer>
203159
)
204160
}

src/components/map/index.js

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -12,6 +12,7 @@ export default function Map({ height, basemap, children, showControls }) {
1212
mapbox.accessToken = process.env.GATSBY_MAPBOX_TOKEN
1313
const m = new mapbox.Map({
1414
container: containerRef.current,
15+
projection: "mercator",
1516
style: basemap || "mapbox://styles/covid-nasa/cm7etyf7t003s01qpfq8kec7y",
1617
zoom: 1,
1718
center: [0, 0],

0 commit comments

Comments
 (0)