Skip to content

Commit

Permalink
add onClick prop handler
Browse files Browse the repository at this point in the history
adds a property `onClick` that allows adding a handler
to be called with the coordinates when the map is clicked.

refs #49
  • Loading branch information
forrert committed Oct 22, 2016
1 parent 12033a1 commit e9bcc7a
Show file tree
Hide file tree
Showing 2 changed files with 33 additions and 13 deletions.
16 changes: 11 additions & 5 deletions example/examples/click.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -56,10 +56,15 @@ export default class ClickExample extends Component {

@autobind
_onClickFeatures(features) {
const placeNames = features
.filter(feature => feature.layer['source-layer'] === 'place_label')
.map(feature => feature.properties.name);
windowAlert(placeNames);
const placeNames = features
.filter(feature => feature.layer['source-layer'] === 'place_label')
.map(feature => feature.properties.name);
windowAlert(placeNames);
}

@autobind
_onClick(coordinates, pos) {
windowAlert(`${coordinates}\n${JSON.stringify(pos)}`);
}

render() {
Expand All @@ -70,7 +75,8 @@ export default class ClickExample extends Component {
return (
<MapGL { ...viewport }
onChangeViewport={ this._onChangeViewport }
onClickFeatures={ this._onClickFeatures }/>
onClickFeatures={ this._onClickFeatures }
onClick={ this._onClick }/>
);
}
}
Expand Down
30 changes: 22 additions & 8 deletions src/map.react.js
Original file line number Diff line number Diff line change
Expand Up @@ -118,6 +118,13 @@ const PROP_TYPES = {
*/
attributionControl: PropTypes.bool,

/**
* Called when the map is clicked. The handler is called with the clicked
* coordinates (https://www.mapbox.com/mapbox-gl-js/api/#LngLat) and the
* screen coordinates (https://www.mapbox.com/mapbox-gl-js/api/#PointLike).
*/
onClick: PropTypes.func,

/**
* Called when a feature is clicked on. Uses Mapbox's
* queryRenderedFeatures API to find features under the pointer:
Expand Down Expand Up @@ -597,21 +604,28 @@ export default class MapGL extends Component {
}

@autobind _onMouseClick(opt) {
if (!this.props.onClickFeatures) {
if (!this.props.onClickFeatures && !this.props.onClick) {
return;
}

const map = this._getMap();
const pos = opt.pos;

// Radius enables point features, like marker symbols, to be clicked.
const size = this.props.clickRadius;
const bbox = [[pos.x - size, pos.y - size], [pos.x + size, pos.y + size]];
const features = map.queryRenderedFeatures(bbox, this._queryParams);
if (!features.length && this.props.ignoreEmptyFeatures) {
return;
if (this.props.onClick) {
const latLong = map.unproject(opt.pos);
this.props.onClick(latLong, pos);
}

if (this.props.onClickFeatures) {
// Radius enables point features, like marker symbols, to be clicked.
const size = this.props.clickRadius;
const bbox = [[pos.x - size, pos.y - size], [pos.x + size, pos.y + size]];
const features = map.queryRenderedFeatures(bbox, this._queryParams);
if (!features.length && this.props.ignoreEmptyFeatures) {
return;
}
this.props.onClickFeatures(features);
}
this.props.onClickFeatures(features);
}

@autobind _onZoom({pos, scale}) {
Expand Down

0 comments on commit e9bcc7a

Please sign in to comment.