Skip to content
This repository has been archived by the owner on Aug 8, 2023. It is now read-only.

Commit

Permalink
first cut of annotation selection
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Mar 18, 2015
1 parent d47b298 commit d53fb94
Showing 1 changed file with 60 additions and 0 deletions.
60 changes: 60 additions & 0 deletions platform/ios/MGLMapView.mm
Original file line number Diff line number Diff line change
Expand Up @@ -67,6 +67,8 @@ @interface MGLMapView () <UIGestureRecognizerDelegate, GLKViewDelegate>
@property (nonatomic) UIRotationGestureRecognizer *rotate;
@property (nonatomic) UILongPressGestureRecognizer *quickZoom;
@property (nonatomic) NSMutableArray *bundledStyleNames;
@property (nonatomic) NSMutableArray *currentNearbyAnnotations;
@property (nonatomic) NSNumber *selectedAnnotationID;
@property (nonatomic, readonly) NSDictionary *allowedStyleTypes;
@property (nonatomic) CGPoint centerPoint;
@property (nonatomic) CGFloat scale;
Expand Down Expand Up @@ -302,6 +304,9 @@ - (BOOL)commonInit
[self addGestureRecognizer:_rotate];
_rotateEnabled = YES;

UITapGestureRecognizer *singleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleSingleTapGesture:)];
[self addGestureRecognizer:singleTap];

UITapGestureRecognizer *doubleTap = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(handleDoubleTapGesture:)];
doubleTap.numberOfTapsRequired = 2;
[self addGestureRecognizer:doubleTap];
Expand All @@ -320,6 +325,10 @@ - (BOOL)commonInit
[self addGestureRecognizer:_quickZoom];
}

// setup nearby annotations for taps
//
_currentNearbyAnnotations = [NSMutableArray array];

// observe app activity
//
[[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(appDidBackground:) name:UIApplicationDidEnterBackgroundNotification object:nil];
Expand Down Expand Up @@ -671,6 +680,57 @@ - (void)handleRotateGesture:(UIRotationGestureRecognizer *)rotate
}
}

- (void)handleSingleTapGesture:(UITapGestureRecognizer *)singleTap
{
CGPoint tapPoint = [singleTap locationInView:self];

CGFloat touchSize = 44;
CGRect tapRect = CGRectMake(tapPoint.x - touchSize / 2, tapPoint.y - touchSize / 2, touchSize, touchSize);
CGPoint tapRectLowerLeft = CGPointMake(tapRect.origin.x, tapRect.origin.y + tapRect.size.height);
CGPoint tapRectUpperRight = CGPointMake(tapRect.origin.x + tapRect.size.width, tapRect.origin.y);
CLLocationCoordinate2D tapRectSW = [self convertPoint:tapRectLowerLeft toCoordinateFromView:self];
CLLocationCoordinate2D tapRectNE = [self convertPoint:tapRectUpperRight toCoordinateFromView:self];

mbgl::LatLngBounds tapBounds = mbgl::LatLngBounds(mbgl::LatLng(tapRectSW.latitude, tapRectSW.longitude),
mbgl::LatLng(tapRectNE.latitude, tapRectNE.longitude));

std::vector<uint32_t> nearbyAnnotations = mbglMap->getAnnotationsInBounds(tapBounds);

if (nearbyAnnotations.size())
{
NSMutableArray *compareAnnotations = [NSMutableArray array];

for (NSUInteger i = 0; i < nearbyAnnotations.size(); i++)
{
[compareAnnotations addObject:@(nearbyAnnotations[i])];
[compareAnnotations sortUsingSelector:@selector(compare:)];
}

if ([self.currentNearbyAnnotations isEqualToArray:compareAnnotations])
{
if ([self.selectedAnnotationID isEqualToValue:self.currentNearbyAnnotations.lastObject])
{
self.selectedAnnotationID = self.currentNearbyAnnotations.firstObject;
}
else
{
self.selectedAnnotationID = self.currentNearbyAnnotations[[self.currentNearbyAnnotations indexOfObject:self.selectedAnnotationID] + 1];
}
}
else
{
[self.currentNearbyAnnotations setArray:compareAnnotations];
self.selectedAnnotationID = self.currentNearbyAnnotations.firstObject;
}
}
else
{
self.selectedAnnotationID = @(-1);
}

NSLog(@"selected: %@", self.selectedAnnotationID);
}

- (void)handleDoubleTapGesture:(UITapGestureRecognizer *)doubleTap
{
if ( ! self.isZoomEnabled) return;
Expand Down

0 comments on commit d53fb94

Please sign in to comment.