|
| 1 | +# Mapbox Collision Boxes |
| 2 | + |
| 3 | +A utility library for [Mapbox GL JS (`mapbox-gl`)](https://docs.mapbox.com/mapbox-gl-js/guides/), that calculates collision boxes of symbols on a Mapbox map in the screen coordinate. |
| 4 | + |
| 5 | +## Getting started |
| 6 | + |
| 7 | +### Prerequisites |
| 8 | + |
| 9 | +This library is supposed to work with `mapbox-gl` version 2 or higher. |
| 10 | + |
| 11 | +### How to install |
| 12 | + |
| 13 | +Please add this repository to your dependencies. |
| 14 | + |
| 15 | +```sh |
| 16 | +npm install https://github.com/codemonger-io/mapbox-collision-boxes#v0.1.0 |
| 17 | +``` |
| 18 | + |
| 19 | +### Usage |
| 20 | + |
| 21 | +The following snippet is an example to collect features hidden by a clicked symbol on the screen. |
| 22 | + |
| 23 | +```ts |
| 24 | +import mapboxgl from 'mapbox-gl'; |
| 25 | +import { boxesIntersect, collectCollisionBoxesAndFeatures } from 'mapbox-collision-boxes'; |
| 26 | + |
| 27 | +const map = new mapboxgl.Map( |
| 28 | + // ... initialize map |
| 29 | +); |
| 30 | +// ... other configurations |
| 31 | +const layerId = 'cats-and-dogs'; // suppose you have a custom layer |
| 32 | +const map.on('click', layerId, async event => { |
| 33 | + const clickedFeatureId = event.features[0].id; |
| 34 | + const collisionBoxes = await collectCollisionBoxesAndFeatures(map, layerId); |
| 35 | + const clickedBox = collisionBoxes.find(box => box.feature.id === clickedFeatureId); |
| 36 | + const hiddenBoxes = collisionBoxes.filter(box => box !== clickedBox && boxesIntersect(box.box, clickedBox.box)); |
| 37 | + const hiddenFeatures = hiddenBoxes.map(box => box.feature); |
| 38 | + // ... process features |
| 39 | +}); |
| 40 | +``` |
| 41 | + |
| 42 | +You can find a complete project in the [`example`](./example) folder. |
| 43 | + |
| 44 | +## Motivation |
| 45 | + |
| 46 | +I have been developing an app that shows custom symbols on a map using [symbol layers](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#symbol) of `mapbox-gl`. |
| 47 | +When symbols overlap on the screen, `mapbox-gl` shows only the first one and hides other overlapping ones. |
| 48 | +As far as I know, there is no `mapbox-gl` API to get symbols hidden by a specific symbol on the screen\*. |
| 49 | +This is not convenient for my app because it wants to list all the symbols including hidden ones at a clicked point. |
| 50 | +Although there is an [option](https://docs.mapbox.com/mapbox-gl-js/style-spec/layers/#layout-symbol-icon-allow-overlap) that makes `mapbox-gl` skip collision detection and show every single symbol on the screen, this will make the map too busy if there are many overlapping symbols. |
| 51 | + |
| 52 | +So I decided to **develop a library that can aggregate symbols overlapping with a specific symbol on a Mapbox map**. |
| 53 | + |
| 54 | +Please refer to my [blog post](https://codemonger.io/blog/0009-mapbox-collision-boxes/) for more details. |
| 55 | + |
| 56 | +## Tips |
| 57 | + |
| 58 | +### Viewport padding |
| 59 | + |
| 60 | +Collision boxes collected by `collectCollisionBoxesAndFeatures` include constant offsets. |
| 61 | +They have the actual screen position + `100`\* along both the x- and y-axes. |
| 62 | +Since the offsets do not matter to hit tests among collision boxes, this library leaves them to avoid unnecessary calculation. |
| 63 | +If you want to project collision boxes to the actuall screen, you have to subtract `100` from their x- and y-axis values. |
| 64 | + |
| 65 | +\* This constant is defined as `viewportPadding` at https://github.com/mapbox/mapbox-gl-js/blob/e29e113ff5e1f4c073f84b8cbe546006d2fb604f/src/symbol/collision_index.js#L50 which is not exported from `mapbox-gl`. |
0 commit comments