Implement draggable annotations on MapView. Closes #2512

Summary: Closes https://github.com/facebook/react-native/pull/4441

Reviewed By: svcscm

Differential Revision: D2707897

Pulled By: nicklockwood

fb-gh-sync-id: 6f67f711c1ec1f821d03b9b1ea5cc39859d28fd1
This commit is contained in:
Jason Brown
2016-01-04 06:37:15 -08:00
committed by facebook-github-bot-7
parent 64edddadcc
commit b8aac8b77a
7 changed files with 122 additions and 75 deletions

View File

@@ -42,22 +42,23 @@ RCT_ENUM_CONVERTER(MKMapType, (@{
json = [self NSDictionary:json];
RCTMapAnnotation *annotation = [RCTMapAnnotation new];
annotation.coordinate = [self CLLocationCoordinate2D:json];
annotation.title = [RCTConvert NSString:json[@"title"]];
annotation.subtitle = [RCTConvert NSString:json[@"subtitle"]];
annotation.identifier = [RCTConvert NSString:json[@"id"]];
annotation.hasLeftCallout = [RCTConvert BOOL:json[@"hasLeftCallout"]];
annotation.hasRightCallout = [RCTConvert BOOL:json[@"hasRightCallout"]];
annotation.animateDrop = [RCTConvert BOOL:json[@"animateDrop"]];
annotation.tintColor = [RCTConvert UIColor:json[@"tintColor"]];
annotation.image = [RCTConvert UIImage:json[@"image"]];
annotation.draggable = [self BOOL:json[@"draggable"]];
annotation.title = [self NSString:json[@"title"]];
annotation.subtitle = [self NSString:json[@"subtitle"]];
annotation.identifier = [self NSString:json[@"id"]];
annotation.hasLeftCallout = [self BOOL:json[@"hasLeftCallout"]];
annotation.hasRightCallout = [self BOOL:json[@"hasRightCallout"]];
annotation.animateDrop = [self BOOL:json[@"animateDrop"]];
annotation.tintColor = [self UIColor:json[@"tintColor"]];
annotation.image = [self UIImage:json[@"image"]];
annotation.viewIndex =
[RCTConvert NSInteger:json[@"viewIndex"] ?: @(NSNotFound)];
[self NSInteger:json[@"viewIndex"] ?: @(NSNotFound)];
annotation.leftCalloutViewIndex =
[RCTConvert NSInteger:json[@"leftCalloutViewIndex"] ?: @(NSNotFound)];
[self NSInteger:json[@"leftCalloutViewIndex"] ?: @(NSNotFound)];
annotation.rightCalloutViewIndex =
[RCTConvert NSInteger:json[@"rightCalloutViewIndex"] ?: @(NSNotFound)];
[self NSInteger:json[@"rightCalloutViewIndex"] ?: @(NSNotFound)];
annotation.detailCalloutViewIndex =
[RCTConvert NSInteger:json[@"detailCalloutViewIndex"] ?: @(NSNotFound)];
[self NSInteger:json[@"detailCalloutViewIndex"] ?: @(NSNotFound)];
return annotation;
}
@@ -66,19 +67,19 @@ RCT_ARRAY_CONVERTER(RCTMapAnnotation)
+ (RCTMapOverlay *)RCTMapOverlay:(id)json
{
json = [self NSDictionary:json];
NSArray<NSDictionary *> *locations = [RCTConvert NSDictionaryArray:json[@"coordinates"]];
NSArray<NSDictionary *> *locations = [self NSDictionaryArray:json[@"coordinates"]];
CLLocationCoordinate2D coordinates[locations.count];
NSUInteger index = 0;
for (NSDictionary *location in locations) {
coordinates[index++] = [RCTConvert CLLocationCoordinate2D:location];
coordinates[index++] = [self CLLocationCoordinate2D:location];
}
RCTMapOverlay *overlay = [RCTMapOverlay polylineWithCoordinates:coordinates
count:locations.count];
overlay.strokeColor = [RCTConvert UIColor:json[@"strokeColor"]];
overlay.identifier = [RCTConvert NSString:json[@"id"]];
overlay.lineWidth = [RCTConvert CGFloat:json[@"lineWidth"] ?: @1];
overlay.strokeColor = [self UIColor:json[@"strokeColor"]];
overlay.identifier = [self NSString:json[@"id"]];
overlay.lineWidth = [self CGFloat:json[@"lineWidth"] ?: @1];
return overlay;
}

View File

@@ -30,6 +30,7 @@ RCT_EXTERN const CGFloat RCTMapZoomBoundBuffer;
@property (nonatomic, copy) RCTBubblingEventBlock onChange;
@property (nonatomic, copy) RCTBubblingEventBlock onPress;
@property (nonatomic, copy) RCTBubblingEventBlock onAnnotationDragStateChange;
- (void)setAnnotations:(NSArray<RCTMapAnnotation *> *)annotations;
- (void)setOverlays:(NSArray<RCTMapOverlay *> *)overlays;

View File

@@ -21,5 +21,6 @@
@property (nonatomic, assign) NSInteger leftCalloutViewIndex;
@property (nonatomic, assign) NSInteger rightCalloutViewIndex;
@property (nonatomic, assign) NSInteger detailCalloutViewIndex;
@property (nonatomic, assign) BOOL draggable;
@end

View File

@@ -95,6 +95,7 @@ RCT_EXPORT_VIEW_PROPERTY(legalLabelInsets, UIEdgeInsets)
RCT_EXPORT_VIEW_PROPERTY(mapType, MKMapType)
RCT_EXPORT_VIEW_PROPERTY(annotations, NSArray<RCTMapAnnotation *>)
RCT_EXPORT_VIEW_PROPERTY(overlays, NSArray<RCTMapOverlay *>)
RCT_EXPORT_VIEW_PROPERTY(onAnnotationDragStateChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onChange, RCTBubblingEventBlock)
RCT_EXPORT_VIEW_PROPERTY(onPress, RCTBubblingEventBlock)
RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
@@ -136,7 +137,6 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
- (void)mapView:(RCTMap *)mapView didSelectAnnotationView:(MKAnnotationView *)view
{
if (mapView.onPress && [view.annotation isKindOfClass:[RCTMapAnnotation class]]) {
RCTMapAnnotation *annotation = (RCTMapAnnotation *)view.annotation;
mapView.onPress(@{
@"action": @"annotation-click",
@@ -151,6 +151,30 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
}
}
- (void)mapView:(RCTMap *)mapView annotationView:(MKAnnotationView *)view
didChangeDragState:(MKAnnotationViewDragState)newState
fromOldState:(MKAnnotationViewDragState)oldState
{
static NSArray *states;
static dispatch_once_t onceToken;
dispatch_once(&onceToken, ^{
states = @[@"idle", @"starting", @"dragging", @"canceling", @"ending"];
});
if ([view.annotation isKindOfClass:[RCTMapAnnotation class]]) {
RCTMapAnnotation *annotation = (RCTMapAnnotation *)view.annotation;
if (mapView.onAnnotationDragStateChange) {
mapView.onAnnotationDragStateChange(@{
@"state": states[newState],
@"oldState": states[oldState],
@"annotationId": annotation.identifier,
@"latitude": @(annotation.coordinate.latitude),
@"longitude": @(annotation.coordinate.longitude),
});
}
}
}
- (MKAnnotationView *)mapView:(RCTMap *)mapView
viewForAnnotation:(RCTMapAnnotation *)annotation
{
@@ -159,7 +183,6 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
}
MKAnnotationView *annotationView;
annotationView.clipsToBounds = YES;
if (annotation.viewIndex != NSNotFound) {
NSString *reuseIdentifier = NSStringFromClass([RCTMapAnnotationView class]);
@@ -205,7 +228,7 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
annotation.tintColor ?: [MKPinAnnotationView redPinColor];
}
}
annotationView.canShowCallout = true;
annotationView.canShowCallout = (annotation.title.length > 0);
if (annotation.leftCalloutViewIndex != NSNotFound) {
annotationView.leftCalloutAccessoryView =
@@ -255,6 +278,8 @@ RCT_CUSTOM_VIEW_PROPERTY(region, MKCoordinateRegion, RCTMap)
}
}
annotationView.draggable = annotation.draggable;
return annotationView;
}