Skip to content

Commit ccefcf0

Browse files
hysanrborn
authored andcommitted
Stop marker event propagation in order to prevent onPress for MapView… (react-native-maps#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#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.
1 parent 5c694d6 commit ccefcf0

File tree

3 files changed

+19
-1
lines changed

3 files changed

+19
-1
lines changed

docs/marker.md

+1
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@
1919
| `draggable` | `<null>` | | This is a non-value based prop. Adding this allows the marker to be draggable (re-positioned).
2020
| `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.
2121
| `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.
22+
| `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.
2223

2324
## Events
2425

index.d.ts

+1
Original file line numberDiff line numberDiff line change
@@ -146,6 +146,7 @@ declare module "react-native-maps" {
146146
rotation?: number;
147147
tracksViewChanges?: boolean
148148
tracksInfoWindowChanges?: boolean
149+
stopPropagation?: boolean
149150
}
150151

151152
export interface MapPolylineProps {

lib/components/MapMarker.js

+17-1
Original file line numberDiff line numberDiff line change
@@ -185,6 +185,14 @@ const propTypes = {
185185

186186
tracksInfoWindowChanges: PropTypes.bool,
187187

188+
/**
189+
* Stops Marker onPress events from propagating to and triggering MapView onPress events.
190+
*
191+
* @platform ios
192+
*/
193+
194+
stopPropagation: PropTypes.bool,
195+
188196
/**
189197
* Callback that is called when the user presses on the marker
190198
*/
@@ -227,7 +235,7 @@ const propTypes = {
227235
};
228236

229237
const defaultProps = {
230-
onPress() {},
238+
stopPropagation: false,
231239
};
232240

233241
class MapMarker extends React.Component {
@@ -295,6 +303,14 @@ class MapMarker extends React.Component {
295303
{...this.props}
296304
image={image}
297305
style={[styles.marker, this.props.style]}
306+
onPress={event => {
307+
if (this.props.stopPropagation) {
308+
event.stopPropagation();
309+
}
310+
if (this.props.onPress) {
311+
this.props.onPress(event);
312+
}
313+
}}
298314
/>
299315
);
300316
}

0 commit comments

Comments
 (0)