Skip to content

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

[Feat] Add getMapboxRef prop (#372)

Robert Claypool91a06cf2c64e
diff --git a/README.md b/README.md
index 557526b31..b1571455b 100644
--- a/README.md
+++ b/README.md
@@ -188,6 +188,16 @@ version displayed in side panel header
Action called when click Save Map Url in side panel header.
+##### `getMapboxRef(mapbox, index)` (Function, optional)
+
+- Default: `undefined`
+
+Function called when `KeplerGL` adds or removes a `MapContainer` component having an inner Mapbox map.
+
+The `mapbox` argument is an [`InteractiveMap`](https://uber.github.io/react-map-gl/#/Documentation/api-reference/interactive-map) when added or `null` when removed.
+
+The `index` argument is 0 for a single map or 1 for an additional map (since `KeplerGL` supports an optional split map view).
+
##### `actions` (Object, optional)
- Default: `{}`
diff --git a/examples/demo-app/src/app.js b/examples/demo-app/src/app.js
index 29eea71a1..da24f77bd 100644
--- a/examples/demo-app/src/app.js
+++ b/examples/demo-app/src/app.js
@@ -285,6 +285,21 @@ class App extends Component {
this.props.dispatch(setCloudLoginSuccess());
};
+ _getMapboxRef = (mapbox, index) => {
+ if (!mapbox) {
+ // The ref has been unset.
+ // https://reactjs.org/docs/refs-and-the-dom.html#callback-refs
+ // console.log(`Map ${index} has closed`);
+ } else {
+ // We expect an InteractiveMap created by KeplerGl's MapContainer.
+ // https://uber.github.io/react-map-gl/#/Documentation/api-reference/interactive-map
+ const map = mapbox.getMap();
+ map.on('zoomend', e => {
+ // console.log(`Map ${index} zoom level: ${e.target.style.z}`);
+ });
+ }
+ };
+
render() {
const {showBanner, width, height} = this.state;
const {sharing} = this.props.demo;
@@ -336,6 +351,7 @@ class App extends Component {
width={width}
height={height - (showBanner ? BannerHeight : 0)}
onSaveMap={this._isCloudStorageEnabled() && this._toggleCloudModal}
+ getMapboxRef={this._getMapboxRef}
/>
</div>
</GlobalStyle>
diff --git a/src/components/kepler-gl.js b/src/components/kepler-gl.js
index b7fff15c7..64f35e4e6 100644
--- a/src/components/kepler-gl.js
+++ b/src/components/kepler-gl.js
@@ -179,6 +179,7 @@ function KeplerGlFactory(
width,
height,
mapboxApiAccessToken,
+ getMapboxRef,
// redux state
mapStyle,
@@ -260,6 +261,7 @@ function KeplerGlFactory(
index={0}
{...mapFields}
mapLayers={isSplit ? splitMaps[0].layers : null}
+ getMapboxRef={getMapboxRef}
/>
]
: splitMaps.map((settings, index) => (
@@ -268,6 +270,7 @@ function KeplerGlFactory(
index={index}
{...mapFields}
mapLayers={splitMaps[index].layers}
+ getMapboxRef={getMapboxRef}
/>
));
diff --git a/src/components/map-container.js b/src/components/map-container.js
index 8f8084777..d353d604e 100644
--- a/src/components/map-container.js
+++ b/src/components/map-container.js
@@ -86,7 +86,8 @@ export default function MapContainerFactory(MapPopover, MapControl) {
mapLayers: PropTypes.object,
onMapToggleLayer: PropTypes.func,
onMapStyleLoaded: PropTypes.func,
- onMapRender: PropTypes.func
+ onMapRender: PropTypes.func,
+ getMapboxRef: PropTypes.func
};
static defaultProps = {
@@ -148,6 +149,7 @@ export default function MapContainerFactory(MapPopover, MapControl) {
_setMapboxMap = (mapbox) => {
if (!this._map && mapbox) {
+
this._map = mapbox.getMap();
// bind mapboxgl event listener
this._map.on(MAPBOXGL_STYLE_UPDATE, () => {
@@ -172,6 +174,13 @@ export default function MapContainerFactory(MapPopover, MapControl) {
}
});
}
+
+ if (this.props.getMapboxRef) {
+ // The parent component can gain access to our MapboxGlMap by
+ // providing this callback. Note that 'mapbox' will be null when the
+ // ref is unset (e.g. when a split map is closed).
+ this.props.getMapboxRef(mapbox, this.props.index);
+ }
}
_onBeforeRender = ({gl}) => {