-
Notifications
You must be signed in to change notification settings - Fork 2.1k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
- Loading branch information
1 parent
70d55e1
commit 94ade66
Showing
10 changed files
with
269 additions
and
51 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,63 @@ | ||
# MapboxOverlay | ||
|
||
`MapboxOverlay` is an implementation of [Mapbox GL JS](https://www.npmjs.com/package/mapbox-gl)'s [IControl](https://docs.mapbox.com/mapbox-gl-js/api/markers/#icontrol) API. When adding a `MapboxOverlay` instance to an mapbox map, a deck.gl canvas overlaid on top of the base map, and synchronized with the map's camera. | ||
|
||
## Example | ||
|
||
```js | ||
import {MapboxOverlay} from '@deck.gl/mapbox'; | ||
import {ScatterplotLayer} from '@deck.gl/layers'; | ||
|
||
const map = new mapboxgl.Map({...}); | ||
|
||
const overlay = new MapboxOverlay({ | ||
layers: [ | ||
new ScatterplotLayer({ | ||
id: 'my-scatterplot', | ||
data: [ | ||
{position: [-74.5, 40], size: 100} | ||
], | ||
getPosition: d => d.position, | ||
getRadius: d => d.size, | ||
getColor: [255, 0, 0] | ||
}) | ||
] | ||
}); | ||
|
||
map.addControl(overlay); | ||
``` | ||
|
||
## Constructor | ||
|
||
```js | ||
import {MapboxOverlay} from '@deck.gl/mapbox'; | ||
new MapboxOverlay(props); | ||
``` | ||
|
||
`MapboxOverlay` accepts the same props as the [Deck](/docs/api-reference/core/deck.md) class, with the following exceptions: | ||
|
||
- `views` - multi-view is not supported. There is only one `MapView` that synchronizes with the base map. | ||
- `viewState` - managed internally. | ||
- `controller` - always disabled (to use Mapbox's interaction handlers). | ||
|
||
## Methods | ||
|
||
##### setProps | ||
|
||
Updates (partial) props of the underlying `Deck` instance. See [Deck.setProps](/docs/api-reference/core/deck.md#setprops). | ||
|
||
##### pickObject | ||
|
||
See [Deck.pickObject](/docs/api-reference/core/deck.md#pickobject). | ||
|
||
##### pickObjects | ||
|
||
See [Deck.pickObjects](/docs/api-reference/core/deck.md#pickobjects). | ||
|
||
##### pickMultipleObjects | ||
|
||
See [Deck.pickMultipleObjects](/docs/api-reference/core/deck.md#pickmultipleobjects). | ||
|
||
##### finalize | ||
|
||
Removes the control and deletes all resources. |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1,2 @@ | ||
export {default as MapboxLayer} from './mapbox-layer'; | ||
export {default as MapboxOverlay} from './mapbox-overlay'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,175 @@ | ||
import {Deck} from '@deck.gl/core'; | ||
import {getViewState} from './deck-utils'; | ||
|
||
import type {Map, IControl, MapMouseEvent} from 'mapbox-gl'; | ||
|
||
/** | ||
* Implements Mapbox [IControl](https://docs.mapbox.com/mapbox-gl-js/api/markers/#icontrol) interface | ||
* Renders deck.gl layers over the base map and automatically synchronizes with the map's camera | ||
*/ | ||
export default class MapboxOverlay implements IControl { | ||
private _props: any; | ||
private _deck: Deck; | ||
private _map?: Map; | ||
private _container?: HTMLDivElement; | ||
|
||
constructor(props) { | ||
this._props = {...props}; | ||
} | ||
|
||
/** Update (partial) props of the underlying Deck instance. */ | ||
setProps(props: any): void { | ||
Object.assign(this._props, props); | ||
|
||
if ('viewState' in this._props) { | ||
delete this._props.viewState; | ||
} | ||
|
||
if (this._deck) { | ||
this._deck.setProps(this._props); | ||
} | ||
} | ||
|
||
/** Called when the control is added to a map */ | ||
onAdd(map: Map) { | ||
this._map = map; | ||
|
||
/* global document */ | ||
const container = document.createElement('div'); | ||
Object.assign(container.style, { | ||
position: 'absolute', | ||
left: 0, | ||
top: 0, | ||
pointerEvents: 'none' | ||
}); | ||
this._container = container; | ||
|
||
this._deck = new Deck({ | ||
...this._props, | ||
parent: container, | ||
viewState: getViewState(map) | ||
}); | ||
|
||
map.on('resize', this._updateContainerSize); | ||
map.on('render', this._updateViewState); | ||
map.on('mousemove', this._handleMouseEvent); | ||
map.on('mouseout', this._handleMouseEvent); | ||
map.on('click', this._handleMouseEvent); | ||
map.on('dblclick', this._handleMouseEvent); | ||
|
||
this._updateContainerSize(); | ||
return container; | ||
} | ||
|
||
/** Called when the control is removed from a map */ | ||
onRemove() { | ||
const map = this._map; | ||
|
||
if (map) { | ||
map.off('resize', this._updateContainerSize); | ||
map.off('render', this._updateViewState); | ||
map.off('mousemove', this._handleMouseEvent); | ||
map.off('mouseout', this._handleMouseEvent); | ||
map.off('click', this._handleMouseEvent); | ||
map.off('dblclick', this._handleMouseEvent); | ||
} | ||
|
||
this._deck?.finalize(); | ||
this._map = undefined; | ||
this._container = undefined; | ||
} | ||
|
||
getDefaultPosition() { | ||
return 'top-left'; | ||
} | ||
|
||
/** Forwards the Deck.pickObject method */ | ||
pickObject(params) { | ||
return this._deck && this._deck.pickObject(params); | ||
} | ||
|
||
/** Forwards the Deck.pickMultipleObjects method */ | ||
pickMultipleObjects(params) { | ||
return this._deck && this._deck.pickMultipleObjects(params); | ||
} | ||
|
||
/** Forwards the Deck.pickObjects method */ | ||
pickObjects(params) { | ||
return this._deck && this._deck.pickObjects(params); | ||
} | ||
|
||
/** Remove from map and releases all resources */ | ||
finalize() { | ||
if (this._map) { | ||
this._map.removeControl(this); | ||
} | ||
} | ||
|
||
_updateContainerSize = () => { | ||
if (this._map && this._container) { | ||
const {clientWidth, clientHeight} = this._map.getContainer(); | ||
Object.assign(this._container.style, { | ||
width: `${clientWidth}px`, | ||
height: `${clientHeight}px` | ||
}); | ||
} | ||
}; | ||
|
||
_updateViewState = () => { | ||
const deck = this._deck; | ||
if (deck) { | ||
deck.setProps({viewState: getViewState(this._map)}); | ||
// Redraw immediately if view state has changed | ||
deck.redraw(false); | ||
} | ||
}; | ||
|
||
_handleMouseEvent = (event: MapMouseEvent) => { | ||
const deck = this._deck; | ||
if (!deck) { | ||
return; | ||
} | ||
|
||
const mockEvent: { | ||
type: string; | ||
offsetCenter: {x: number; y: number}; | ||
srcEvent: MapMouseEvent; | ||
tapCount?: number; | ||
} = { | ||
type: event.type, | ||
offsetCenter: event.point, | ||
srcEvent: event | ||
}; | ||
|
||
switch (event.type) { | ||
case 'click': | ||
// Hack: because we do not listen to pointer down, perform picking now | ||
deck._lastPointerDownInfo = deck.pickObject({ | ||
...mockEvent.offsetCenter, | ||
radius: deck.props.pickingRadius | ||
}); | ||
mockEvent.tapCount = 1; | ||
deck._onEvent(mockEvent); | ||
break; | ||
|
||
case 'dblclick': | ||
mockEvent.type = 'click'; | ||
mockEvent.tapCount = 2; | ||
deck._onEvent(mockEvent); | ||
break; | ||
|
||
case 'mousemove': | ||
mockEvent.type = 'pointermove'; | ||
deck._onPointerMove(mockEvent); | ||
break; | ||
|
||
case 'mouseout': | ||
mockEvent.type = 'pointerleave'; | ||
deck._onPointerMove(mockEvent); | ||
break; | ||
|
||
default: | ||
return; | ||
} | ||
}; | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Oops, something went wrong.