Skip to content

Commit

Permalink
Stop marker event propagation in order to prevent onPress for MapView…
Browse files Browse the repository at this point in the history
…… (#2068)

* Stop marker event propagation in order to prevent onPress for MapViews from being called when a marker is clicked. This makes the behavior of Apple Maps identical to that of the behavior of Google Maps on Android. This fixes react-native-maps/react-native-maps#1132.

* Added a new Marker prop called stopPropagation. This allows the user to control whether or not onPress events from Markers propagate up and trigger MapView onPress events. This is iOS only. The default behavior is disabled (false) to prevent a breaking change from the current behavior.
  • Loading branch information
Super-CodeKing committed Mar 10, 2018
1 parent d72b8e1 commit fd74d11
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 1 deletion.
1 change: 1 addition & 0 deletions docs/marker.md
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
| `draggable` | `<null>` | | This is a non-value based prop. Adding this allows the marker to be draggable (re-positioned).
| `tracksViewChanges` | `Boolean` | true | Sets whether this marker should track view changes. It's recommended to turn it off whenever it's possible to improve custom marker performance. **Note**: iOS Google Maps only.
| `tracksInfoWindowChanges` | `Boolean` | false | Sets whether this marker should track view changes in info window. Enabling it will let marker change content of info window after first render pass, but will lead to decreased performance, so it's recommended to disable it whenever you don't need it. **Note**: iOS Google Maps only.
| `stopPropagation` | `Boolean` | false | Sets whether this marker should propagate `onPress` events. Enabling it will stop the parent `MapView`'s `onPress` from being called. **Note**: iOS only. Android does not propagate `onPress` events. See [#1132](https://github.com/react-community/react-native-maps/issues/1132) for more information.

## Events

Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,7 @@ declare module "react-native-maps" {
rotation?: number;
tracksViewChanges?: boolean
tracksInfoWindowChanges?: boolean
stopPropagation?: boolean
}

export interface MapPolylineProps {
Expand Down
18 changes: 17 additions & 1 deletion lib/components/MapMarker.js
Original file line number Diff line number Diff line change
Expand Up @@ -185,6 +185,14 @@ const propTypes = {

tracksInfoWindowChanges: PropTypes.bool,

/**
* Stops Marker onPress events from propagating to and triggering MapView onPress events.
*
* @platform ios
*/

stopPropagation: PropTypes.bool,

/**
* Callback that is called when the user presses on the marker
*/
Expand Down Expand Up @@ -227,7 +235,7 @@ const propTypes = {
};

const defaultProps = {
onPress() {},
stopPropagation: false,
};

class MapMarker extends React.Component {
Expand Down Expand Up @@ -295,6 +303,14 @@ class MapMarker extends React.Component {
{...this.props}
image={image}
style={[styles.marker, this.props.style]}
onPress={event => {
if (this.props.stopPropagation) {
event.stopPropagation();
}
if (this.props.onPress) {
this.props.onPress(event);
}
}}
/>
);
}
Expand Down

0 comments on commit fd74d11

Please sign in to comment.