Skip to content

Commit

Permalink
udpate docs and examples
Browse files Browse the repository at this point in the history
  • Loading branch information
Xintong Xia committed Apr 27, 2020
1 parent ea01a55 commit 7d13e05
Show file tree
Hide file tree
Showing 8 changed files with 2,043 additions and 1,427 deletions.
83 changes: 46 additions & 37 deletions docs/api-reference/react-map-gl-draw/react-map-gl-draw.md
Expand Up @@ -3,20 +3,18 @@
`react-map-gl-draw` is a react based drawing library tailored for [`react-map-gl`](https://github.com/uber/react-map-gl).

## Options
- `mode` (Object, Optional)
- `EditorModes.READ_ONLY` - Not interactive. This is the default mode.
- `EditorModes.SELECT` - Lets you select, delete, and drag features.
- `EditorModes.EDITTING` - Lets you select, delete, and drag vertices; and drag features.
- `EditorModes.DRAW_PATH` - Lets you draw a GeoJson `LineString` feature.
- `EditorModes.DRAW_POLYGON` - Lets you draw a GeoJson `Polygon` feature.
- `EditorModes.DRAW_POINT` - Lets you draw a GeoJson `Point` feature.
- `EditorModes.DRAW_RECTANGLE` - Lets you draw a `Rectangle` (represented as GeoJson `Polygon` feature).

- `features` (Feature[], Optional) - List of features in GeoJson format. `react-map-gl-draw` respect the user' input `features`. If not provided, will manage `features` internally, and user can access and manipulate the `features` by calling `getFeatures`, `addFeatures`, and `deleteFeatures`.
- `selectedFeatureIndex` (String, Optional) - Index of the selected feature. If not provided, will manage it internally.
- `mode` (Object, Optional) - A mode instance. default to null. Available modes are
- `EditingMode`: Lets you select and drag vertices; and drag features.
- `DrawLineStringMode`: Lets you draw a GeoJson `LineString` feature.
- `DrawPolygonMode`: Lets you draw a GeoJson `Polygon` feature.
- `DrawPointMode`: Lets you draw a GeoJson `Point` feature.
- `DrawRectangleMode`: Lets you draw a `Rectangle` (represented as GeoJson `Polygon` feature) with two clicks - start drawing on first click, and finish drawing on second click.

- `features` (Feature[], Optional) - List of features in GeoJson format. If `features` are provided from users, then `react-map-gl-draw` respect the users' input, and therefore ignore any internal `features`. But if `features` are not provided, then `react-map-gl-draw` manages `features` internally, and users can access and manipulate the features by calling `getFeatures`, `addFeatures`, and `deleteFeatures`.
- `selectedFeatureIndex` (String, Optional) - Index of the selected feature.
- `clickRadius` (Number, Optional) - Radius to detect features around a hovered or clicked point. Default value is `0`

- `onSelect` (Function, Optional) - callback when clicking a position under `SELECT` and `EDITTING` mode. Receives an object containing the following parameters
- `onSelect` (Function, Optional) - callback when clicking a position under `SELECT` and `EDITING` mode. Receives an object containing the following parameters
- `selectedFeature`: selected feature. `null` if clicked an empty space.
- `selectedFeatureIndex`: selected feature index.`null` if clicked an empty space.
- `editHandleIndex`: selected editHandle index. `null` if clicked an empty space.
Expand Down Expand Up @@ -56,7 +54,7 @@ Returns is a map of [style objects](https://reactjs.org/docs/dom-elements.html#s
- `index`: index of the feature.
- `state`: one of `SELECTED`, `HOVERED`, `INACTIVE`, `UNCOMMITTED`, `CLOSING`.

- `editHandleStyle` (Object|Function, Optional) : Object - Either a [style objects](https://reactjs.org/docs/dom-elements.html#style) or a function to style an `editHandle`, function parameters are
- `editHandleStyle` (Object|Function, Optional) : Object - Either a [style objects](https://reactjs.org/docs/dom-elements.html#style) or a function to style an `editHandle, function parameters are
- `feature`: feature to style.
- `index`: index of the editHandle vertex in the feature.
- `state`: one of `SELECTED`, `HOVERED`, `INACTIVE`, `UNCOMMITTED`, `CLOSING`.
Expand Down Expand Up @@ -107,64 +105,75 @@ As shown in the above image, for the feature currently being edited,
## Code Example
```js
import React, { Component } from 'react';
import { render } from 'react-dom';
import MapGL from 'react-map-gl';
import { Editor, EditorModes } from 'react-map-gl-draw';
import {
Editor,
EditingMode,
DrawLineStringMode,
DrawPolygonMode,
} from 'react-map-gl-draw';

const MODES = [
{ id: EditorModes.EDITING, text: 'Select and Edit Feature'},
{ id: EditorModes.DRAW_POINT, text: 'Draw Point'},
{ id: EditorModes.DRAW_PATH, text: 'Draw Polyline'},
{ id: EditorModes.DRAW_POLYGON, text: 'Draw Polygon'},
{ id: EditorModes.DRAW_RECTANGLE, text: 'Draw Rectangle'}
{ id: 'drawPolyline', text: 'Draw Polyline', handler: DrawLineStringMode },
{ id: 'drawPolygon', text: 'Draw Polygon', handler: DrawPolygonMode },
{ id: 'editing', text: 'Edit Feature', handler: EditingMode },
];

const DEFAULT_VIEWPORT = {
width: 800,
height: 600,
longitude: -122.45,
latitude: 37.78,
zoom: 14
zoom: 14,
};

class App extends Component {
state = {
// map
viewport: DEFAULT_VIEWPORT,
// editor
selectedMode: EditorModes.READ_ONLY
};
export default class App extends Component {
constructor(props) {
super(props);
this.state = {
viewport: DEFAULT_VIEWPORT,
modeId: null,
modeHandler: null,
};
}

_switchMode = evt => {
const selectedMode = evt.target.id;
this.setState({
selectedMode: selectedMode === this.state.selectedMode ? null : selectedMode
});
const modeId = evt.target.value === this.state.modeId ? null : evt.target.value;
const mode = MODES.find(m => m.id === modeId);
const modeHandler = mode ? new mode.handler() : null;
this.setState({modeId, modeHandler});
};

_renderToolbar = () => {
return (
<div style={{position: absolute, top: 0, right: 0, maxWidth: '320px'}}>
<div style={{position: 'absolute', top: 0, right: 0, maxWidth: '320px'}}>
<select onChange={this._switchMode}>
<option value="">--Please choose a mode--</option>
{MODES.map(mode => <option value={mode.id}>{mode.text}</option>)}
<option value="">--Please choose a draw mode--</option>
{MODES.map(mode => <option key={mode.id} value={mode.id}>{mode.text}</option>)}
</select>
</div>
);
};

_updateViewport = (viewport) => {
this.setState({ viewport });
};

render() {
const { viewport, selectedMode } = this.state;
const { viewport, modeHandler } = this.state;
return (
<MapGL
{...viewport}
width="100%"
height="100%"
mapStyle={'mapbox://styles/mapbox/light-v9'}
onViewportChange={this.setState({ viewport })}
onViewportChange={this._updateViewport}
>
<Editor
// to make the lines/vertices easier to interact with
clickRadius={12}
mode={selectedMode}
mode={modeHandler}
/>
{this._renderToolbar()}
</MapGL>
Expand Down
2 changes: 1 addition & 1 deletion examples/react-map-gl-draw/package.json
Expand Up @@ -11,7 +11,7 @@
"react": "^16.3.0",
"react-dom": "^16.3.0",
"react-map-gl": "^4.0.0",
"react-map-gl-draw": "^0.18.0-alpha.2",
"react-map-gl-draw": "^0.18.1",
"@math.gl/core": "^3.1.3"
},
"devDependencies": {
Expand Down

0 comments on commit 7d13e05

Please sign in to comment.