Skip to content

Commit

Permalink
Add onDoublePress callback (react-native-maps#2937)
Browse files Browse the repository at this point in the history
Add to Google Maps on Android and Apple Maps on iOS
  • Loading branch information
nabati authored and christopherdro committed Aug 29, 2019
1 parent 6cf5118 commit 094f864
Show file tree
Hide file tree
Showing 7 changed files with 52 additions and 3 deletions.
1 change: 1 addition & 0 deletions docs/mapview.md
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ To access event data, you will need to use `e.nativeEvent`. For example, `onPres
| `onRegionChangeComplete` | `Region` | Callback that is called once when the region changes, such as when the user is done moving the map.
| `onUserLocationChange` | `{ coordinate: Location }` | Callback that is called when the underlying map figures our users current location (coordinate also includes isFromMockProvider value for Android API 18 and above). Make sure **showsUserLocation** is set to *true* and that the provider is `"google"`.
| `onPress` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user taps on the map.
| `onDoublePress` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user double taps on the map.
| `onPanDrag` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user presses and drags the map. **NOTE**: for iOS `scrollEnabled` should be set to false to trigger the event
| `onPoiClick` | `{ coordinate: LatLng, position: Point, placeId: string, name: string }` | Callback that is called when user click on a POI.
| `onLongPress` | `{ coordinate: LatLng, position: Point }` | Callback that is called when user makes a "long press" somewhere on the map.
Expand Down
1 change: 1 addition & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
Expand Up @@ -221,6 +221,7 @@ declare module "react-native-maps" {
onRegionChange?: (region: Region) => void;
onRegionChangeComplete?: (region: Region) => void;
onPress?: (event: MapEvent) => void;
onDoublePress?: (event: MapEvent) => void;
onLongPress?: (event: MapEvent) => void;
onUserLocationChange?: (event: EventUserLocation) => void;
onPanDrag?: (event: MapEvent) => void;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -378,6 +378,7 @@ public Map getExportedCustomDirectEventTypeConstants() {
map.putAll(MapBuilder.of(
"onIndoorLevelActivated", MapBuilder.of("registrationName", "onIndoorLevelActivated"),
"onIndoorBuildingFocused", MapBuilder.of("registrationName", "onIndoorBuildingFocused"),
"onDoublePress", MapBuilder.of("registrationName", "onDoublePress"),
"onMapLoaded", MapBuilder.of("registrationName", "onMapLoaded")
));

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -172,6 +172,12 @@ public boolean onScroll(MotionEvent e1, MotionEvent e2, float distanceX,
}
return false;
}

@Override
public boolean onDoubleTap(MotionEvent ev) {
onDoublePress(ev);
return false;
}
});

this.addOnLayoutChangeListener(new OnLayoutChangeListener() {
Expand Down Expand Up @@ -1088,6 +1094,13 @@ public void onPanDrag(MotionEvent ev) {
manager.pushEvent(context, this, "onPanDrag", event);
}

public void onDoublePress(MotionEvent ev) {
Point point = new Point((int) ev.getX(), (int) ev.getY());
LatLng coords = this.map.getProjection().fromScreenLocation(point);
WritableMap event = makeClickEventData(coords);
manager.pushEvent(context, this, "onDoublePress", event);
}

public void setKmlSrc(String kmlSrc) {
try {
InputStream kmlStream = new FileUtil(context).execute(kmlSrc).get();
Expand Down
5 changes: 5 additions & 0 deletions lib/components/MapView.js
Original file line number Diff line number Diff line change
Expand Up @@ -426,6 +426,11 @@ const propTypes = {
*/
onPress: PropTypes.func,

/**
* Callback that is called when user double taps on the map.
*/
onDoublePress: PropTypes.func,

/**
* Callback that is called when user makes a "long press" somewhere on the map.
*/
Expand Down
1 change: 1 addition & 0 deletions lib/ios/AirMaps/AIRMap.h
Original file line number Diff line number Diff line change
Expand Up @@ -56,6 +56,7 @@ extern const NSInteger AIRMapMaxZoomLevel;
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
@property (nonatomic, copy) RCTBubblingEventBlock onPanDrag;
@property (nonatomic, copy) RCTBubblingEventBlock onDoublePress;
@property (nonatomic, copy) RCTBubblingEventBlock onLongPress;
@property (nonatomic, copy) RCTDirectEventBlock onMarkerPress;
@property (nonatomic, copy) RCTDirectEventBlock onMarkerSelect;
Expand Down
33 changes: 30 additions & 3 deletions lib/ios/AirMaps/AIRMapManager.m
Original file line number Diff line number Diff line change
Expand Up @@ -57,19 +57,26 @@ - (UIView *)view

// MKMapView doesn't report tap events, so we attach gesture recognizers to it
UITapGestureRecognizer *tap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMapTap:)];
UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleMapDoubleTap:)];
[doubleTap setNumberOfTapsRequired:2];
[tap requireGestureRecognizerToFail:doubleTap];

UILongPressGestureRecognizer *longPress = [[UILongPressGestureRecognizer alloc] initWithTarget:self action:@selector(handleMapLongPress:)];
UIPanGestureRecognizer *drag = [[UIPanGestureRecognizer alloc] initWithTarget:self action:@selector(handleMapDrag:)];
[drag setMinimumNumberOfTouches:1];
// setting this to NO allows the parent MapView to continue receiving marker selection events
tap.cancelsTouchesInView = NO;
doubleTap.cancelsTouchesInView = NO;
longPress.cancelsTouchesInView = NO;


doubleTap.delegate = self;

// disable drag by default
drag.enabled = NO;

drag.delegate = self;

[map addGestureRecognizer:tap];
[map addGestureRecognizer:doubleTap];
[map addGestureRecognizer:longPress];
[map addGestureRecognizer:drag];

Expand Down Expand Up @@ -105,6 +112,7 @@ - (UIView *)view
RCT_EXPORT_VIEW_PROPERTY(onPanDrag, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onLongPress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onDoublePress, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onMarkerPress, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onMarkerSelect, RCTDirectEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onMarkerDeselect, RCTDirectEventBlock)
Expand Down Expand Up @@ -755,6 +763,25 @@ - (void)handleMapDrag:(UIPanGestureRecognizer*)recognizer {

}

- (void)handleMapDoubleTap:(UIPanGestureRecognizer*)recognizer {
AIRMap *map = (AIRMap *)recognizer.view;
if (!map.onDoublePress) return;

CGPoint touchPoint = [recognizer locationInView:map];
CLLocationCoordinate2D coord = [map convertPoint:touchPoint toCoordinateFromView:map];
map.onDoublePress(@{
@"coordinate": @{
@"latitude": @(coord.latitude),
@"longitude": @(coord.longitude),
},
@"position": @{
@"x": @(touchPoint.x),
@"y": @(touchPoint.y),
},
});

}


- (void)handleMapLongPress:(UITapGestureRecognizer *)recognizer {

Expand Down

0 comments on commit 094f864

Please sign in to comment.