You signed in with another tab or window. Reload to refresh your session.You signed out in another tab or window. Reload to refresh your session.You switched accounts on another tab or window. Reload to refresh your session.Dismiss alert
displayControlsDefault | boolean | Sets default value for the control keys in the control option - default `true`
28
-
controls | Object | Lets you hide or show individual controls. See `displayControlsDefault` for default. Available options are: marker, line, shape, square and trash.
29
-
styles | Object | Add a style with any of the following properties: <li>`gl-draw-polygon`</li><li>`gl-draw-polygon-stroke`</li><li> `gl-draw-line`</li><li> `gl-draw-point`</li><li> `gl-draw-selected-line`</li><li> `gl-draw-selected-polygon`</li><li> `gl-draw-selected-polygon-stroke`</li><li> `gl-draw-selected-point`</li><li> `gl-draw-selected-point-mid`</li>The property should be an object with either the `layout` and/or `paint` properties as specified in the [Mapbox GL Style Reference](https://www.mapbox.com/mapbox-gl-style-spec/). It will overwrite the corresponding default styles found in [`src/theme/`](https://github.com/mapbox/mapbox-gl-draw/tree/master/src/theme).
30
-
31
-
Custom Style Example:
32
-
33
-
```js
34
-
var Draw =mapboxgl.Draw({
35
-
styles: {
36
-
'gl-draw-polygon': {
37
-
'paint': {
38
-
'fill-color':'#00ffff',
39
-
'fill-outline-color':'#00ffff',
40
-
'fill-opacity':0.25
41
-
}
42
-
},
43
-
'gl-draw-polygon-stroke': {
44
-
'paint': {
45
-
'line-color':'#0000ff',
46
-
'line-width':4
47
-
}
48
-
}
49
-
}
50
-
});
51
-
```
52
-
53
-
54
-
`mapboxgl.Draw()` returns an instance of the `Draw` class which has the following public API methods for getting and setting data:
27
+
controls | Object | Lets you hide or show individual controls. See `displayControlsDefault` for default. Available options are: point, line, polygon and trash.
28
+
style | Object | An array of style objects. By default draw provides a style for you. To override this see [Styling Draw](#styling-draw) further down.
`mapboxgl.Draw()` returns an instance of `Draw` which has the following API for working with your data:
59
33
60
-
This method takes any valid GeoJSON and adds it to Draw. The object will be turned into a GeoJSON feature and will be assigned a unique `drawId` that can be used to identify it. This method return the new feature's `drawId`. If an id is provided with the feature that ID will be used.
34
+
###`.add(Object: GeoJSON) -> String || [String]`
61
35
62
-
The second argument is an optional options object to add information to the geometry when creating the new element. Currently the only used option is `permanent`, which, if set to true, will cause the element to ignore click events, `Draw.select(:id:)` and `Draw.selectAll()`.
36
+
This method takes either a Feature or a FeatureCollection and adds it to Draw. It returns an id for interacting with the added feature. When a FeatureCollection is provided an array of ids is returned. If there is no ID on the feature, a random ID will be genearted.
63
37
64
-
Draw does not enforce unique IDs to be passed to `.add`, but it does enforce unique ids inside of it. This means that if you provide an id for a feature that is not unqiue, Draw will override the exhisting feature with your new feature. You can think of this like PUT in http verbs.
38
+
Currently the only supoorted GeoJSON feature types are `Point`, `LineString` and `Polygon`.
65
39
66
-
If a FeatureCollection is provided to `.add` Draw will break it up into many features as if you looped through the features in the collection and added them one at a time. This is good for bulk adding, though it is no faster than looping yourself.
40
+
Adding a feature with the same id as another feature in Draw forces an update.
67
41
68
42
Example:
69
43
70
44
```js
71
45
var feature = { type:'Point', coordinates: [0, 0] };
This method takes the `drawId` of a feature and returns its GeoJSON object.
63
+
This method takes an ID returns a GeoJSON feature.
91
64
92
65
Example:
93
66
@@ -98,10 +71,9 @@ console.log(Draw.get(id));
98
71
```
99
72
100
73
---
101
-
####`.getAll() -> Object`
102
-
103
-
This method returns all features added to Draw in a single GeoJSON FeatureCollection. The each feature's unique id will be found on the `id` attribute of the feature.
74
+
###`.getAll() -> Object`
104
75
76
+
This method returns all features added to Draw in a single GeoJSON FeatureCollection.
This method takes an id removes the coorisponding feature from Draw.
145
120
146
-
---
121
+
In `direct_select` mode, deleting the active feature will stop the mode and revert to the `simple_select` mode.
147
122
148
-
####`.select(String: drawId) -> Draw`
123
+
Example:
149
124
150
-
This method takes the `drawId` of a feature and selects it. It returns `this` to allow for method chaining.
125
+
```js
126
+
var feature = { type:'Point', coordinates: [0, 0] };
127
+
var id =draw.add(feature)
128
+
Draw
129
+
.delete(id)
130
+
.getAll();
131
+
// => { type: 'FeatureCollection', features: [] }
132
+
```
151
133
152
134
---
153
135
154
-
####`.selectAll() -> Draw`
136
+
###`.deleteAll() -> Draw`
155
137
156
-
This method selects all features. It returns `this` to allow for method chaining.
138
+
This method removes all geometries in Draw.
139
+
140
+
Example:
141
+
142
+
```js
143
+
Draw.add({ type:'Point', coordinates: [0, 0] });
144
+
Draw
145
+
.deleteAll()
146
+
.getAll();
147
+
// => { type: 'FeatureCollection', features: [] }
148
+
```
157
149
158
150
---
159
151
160
-
####`.deselect(String: drawId) -> Draw`
152
+
###`.trash() -> Draw`
161
153
162
-
This method takes the `drawId` of a feature and deselects it if selected. It returns `this` to allow for method chaining.
154
+
This envokes the current modes trash event. For the `simple_select` mode this deletes all active features. For the `direct_select` mode this deletes the active vertices. For the drawing modes, these cancel the current process.
155
+
156
+
This is different from `delete` or `deleteAlll` in that it follows rules described by the current mode.
This method deselects all features. It returns `this` to allow for method chaining.
162
+
`changeMode` triggers the mode switching process inside Draw. `mode` must be one of the below strings. Each mode takes its own arguments. They are descibed in detail below.
169
163
170
-
---
164
+
#### Mode: `simple_select`
171
165
172
-
####`.destroy(String: drawId) -> Draw`
166
+
Lets you select, delete and drag features.
173
167
174
-
This method takes the `drawId` of feature and removes it from draw. It returns `this`to allow for method chaining.
168
+
For `simple_select` options is an array of ids. It is optional. If provided, these features will be active at the start of the mode. In this mode, features can have their active state changed by the user. To control what is active, react to changes as described in the events section below.
175
169
176
-
Example:
170
+
#### Mode: `direct_select`
177
171
178
-
```js
179
-
var feature = { type:'Point', coordinates: [0, 0] };
180
-
var id =draw.add(feature)
181
-
Draw
182
-
.destroy(id)
183
-
.getAll();
184
-
// => { type: 'FeatureCollection', features: [] }
185
-
```
172
+
Lets you select, delete and drag vertices.
186
173
187
-
---
174
+
For `direct_select`options is a single featureId. It is required. This feature will be active for the duration of the mode.
Draw fires off a number of events. All of these events are name spaced `draw.` and are emitted via map object.
205
187
206
-
####`.clear() -> Draw`
188
+
###draw.modechange
207
189
208
-
This method removes all geometries in Draw.
190
+
This event is fired just after the current mode is stopped and just before the next mode is started. A render will not happen until after all event handlers have been triggered. This means you can force a mode redirect by calling `changeMode` inside of a `draw.modechange` handler.
This is fired every time a feature is deleted. The payload is a list GeoJSON features just before they were deleted.
223
206
224
-
This method initiates drawing the specified type. The types are defined in constants in draw under `Draw.types.*`. The argument should be one of
207
+
### draw.changed
225
208
226
-
-`Draw.types.POINT`
227
-
-`Draw.types.LINE`
228
-
-`Draw.types.POLYGON`
229
-
-`Draw.types.SQUARE`
209
+
This is fired when feature coordinates are changed. It is fired just after the changes have been sent to be rendered. The payload is the GeoJSON of all changed features. Deleted features will not show up here.
230
210
231
-
Example:
211
+
### draw.mode.default.selected.start
232
212
233
-
```js
234
-
Draw.startDrawing(Draw.types.LINE);
235
-
```
213
+
This is fired every time a feature is selected in the default mode. The payload is an array of feature ids being selected. This is **NOT** fired when the mode starts as this information is in the `draw.modechange` event.
236
214
237
-
## Events
215
+
### draw.mode.default.selected.end
216
+
217
+
This is fired every time a feature is unselected in the default mode. The payload is an array of feature ids being unselected. This is **NOT** fired when the mode stops, as this can be assumed via the `draw.modechange` event.
218
+
219
+
## Styling Draw
238
220
239
-
Draw fires off a number of events on draw and select actions. All of these events are name spaced `draw` inside of the mapboxgl event emitter.
221
+
Draw is styled by the [Mapbox GL Style Spec](https://www.mapbox.com/mapbox-gl-style-spec/) with a preset list of properties.
240
222
241
-
#### draw.set
223
+
The `GL Style Spec` requires each layer to have a source. **DO NOT PROVIDE THIS** for styling draw.
242
224
243
-
This is fired every time a feature is committed via escape or the double click. The payload is an object with the `mapbox-gl-draw` feature id and the GeoJSON representation of the feature.
225
+
Draw moves features between sources for performance gains, because of this it is recommeneded that you **DO NOT** provide a source for a style despite the fact the `GL Style Spec` requires a source. **Draw will provide the source for you automatically**.
244
226
245
-
#### draw.delete
227
+
If you need to style gl-draw for debugging sources the source names are `mapbox-gl-draw-hot` and `mapbox-gl-draw-cold`.
246
228
247
-
This is fired every time a feature is deleted inside of `mapbox-gl-draw`. The payload is an object with the `mapbox-gl-draw` feature id of the feature that was deleted and the GeoJSON representation of the feature just before it was deleted.
229
+
property | values | function
230
+
--- | --- | ---
231
+
meta | feature, midpoint, vertex, too-small | `midpoint` and `vertex` are used on points added to the map to communicate polygon and line handles. `feature` is used for all features added by the user. `too-small` is used to indicate a point that represents the center of a collection of features that are too small at the current zoom level to be seen.
232
+
active | true, false | A feature is active when it is 'selected' in the current mode. `true` and `false` are strings.
233
+
mode | simple_select, direct_select, draw_point, draw_line_string, draw_polygon | Indicates which mode Draw is currently in.
234
+
hover | true, false | `true` and `false` are strings. `hover` is true when the mouse is over the feature.
248
235
249
-
#### draw.select.start
236
+
Draw also provides a few more properties, but they should not be used for styling. For details on them, see `Using Draw with map.queryRenderFeatures`.
250
237
251
-
Fired every time a feature is selected. The payload is an object with the `mapbox-gl-draw` feature id and the GeoJSON representation of the feature.
238
+
### Example Custom Style
252
239
253
-
#### draw.select.end
240
+
With this style all Point features are blue and have a black halo when active. No other features are rendered, even if they are present.
254
241
255
-
Fired every time a feature is deselected. The payload is an object with the `mapbox-gl-draw` feature id.
242
+
```js
243
+
mapbox.Draw({
244
+
style: [
245
+
{
246
+
'id':'highlight-active-points',
247
+
'type':'circle',
248
+
'filter': ['all',
249
+
['==', '$type', 'Point'],
250
+
['==', 'meta', 'feature'],
251
+
['==', 'active', 'true']],
252
+
'paint': {
253
+
'circle-radius':7,
254
+
'circle-color':'#000000'
255
+
},
256
+
'interactive':true
257
+
},
258
+
{
259
+
'id':'points-are-blue',
260
+
'type':'circle',
261
+
'filter': ['all',
262
+
['==', '$type', 'Point'],
263
+
['==', 'meta', 'feature'],
264
+
['==', 'active', 'true']],
265
+
'paint': {
266
+
'circle-radius':5,
267
+
'circle-color':'#000088'
268
+
},
269
+
'interactive':true
270
+
}
271
+
]
272
+
});
273
+
```
256
274
257
-
## Private functions
275
+
## Using Draw with `map.queryRenderFeatures`
258
276
259
-
Draw uses underscore prefixed variables to indicate that the following function is private. These functions are apt to change or be removed. If there is functionality in these functions that should be part of the public api, please open PR explaining the use case.
277
+
property | values | function
278
+
--- | --- | ---
279
+
id | string | only available when `meta` is `feature`
280
+
parent | string | only avaible when `meta` is not `feature`
281
+
coord_path | string | a `.` seporated path to one [lon, lat] entity in the parents coordinates
282
+
lon | number | the longitude value of a handle. Only available when `meta` is `midpoint`.
283
+
lat | number | the latitude value of a handle. Only available when `meta` is `midpoint`.
0 commit comments