Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

docs: add better documentation on selecting features #154

Merged
merged 1 commit into from
Dec 29, 2023
Merged
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
61 changes: 61 additions & 0 deletions guides/3.MODES.md
Original file line number Diff line number Diff line change
Expand Up @@ -64,6 +64,22 @@ For example, if you draw a polygon using the `TerraDrawPolygonMode` the `mode` p
> [!NOTE]
> When [Loading Data](#loading-features) into Terra Draw, the `mode` property is used to determine which Mode to add the Feature to.

### Mode Types

Modes can have one of four types. Drawing modes are associated with drawing new geometries onto the map, select modes are aassociated with selecting existing geometries on the map, static mode is a builtin inert mode that just renders the geometries to the map, and a render mode is a 'view only' mode that is just for rendering geometries to the map.

```typescript
export enum ModeTypes {
Drawing = "drawing",
Select = "select",
Static = "static",
Render = "render",
}
```

> [!NOTE]
> Currently, you may only have one `select` mode instantiated in anyone Terra Draw instance.

### Drawing Modes

Terra Draw comes with the following built-in Drawing Modes out of the box:
Expand Down Expand Up @@ -208,6 +224,51 @@ new TerraDrawSelectMode({
});
```

> [!NOTE]
> It is possible to create and use your own selection mode if you so wish. You do not have to use the built in select mode (`TerraDrawSelectMode`).


#### Getting Selected Features

You can get selected features from the selection mode in one of two ways. The first is to listen for the `select` event:

```typescript
draw.on('select', (id: string) => {
const snapshot = draw.getSnapshot()

// Search the snapshot for the selected polygon
const polygon = snapshot.find((feature) => feature.id === id)
})
```

Alternatively, if you need access to the specific mouse/map event alongside the selected geometry you can achieve this in almost all map libraries by creating an event on the map itself object itself like so:

```typescript
// Register an on click event onto the map itself
map.on('click', (event) => {
const snapshot = draw.getSnapshot()

// Search the snapshot for the selected polygon
const polygon = snapshot.find((feature) => feature.properties.selected === true && feature.geometry.type === 'Polygon')

// If there is not a selected polygon, don't do anything
if (!polygon) {
return
}

// Else create or update the popup location to be the cursor position!
if (popup) {
popup.setLngLat([event.lngLat.lng, event.lngLat.lat])
} else {
popup = new maplibregl.Popup({ closeOnClick: false })
.setLngLat([event.lngLat.lng, event.lngLat.lat])
.setHTML('<h1>Example Popup</h1>')
.addTo(map);
}
})
```


### Render Mode

The Render Mode is used to render Features that have been drawn on the map, but are not editable.
Expand Down
Loading