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

Commit

Permalink
Pushed annotation class into mgl proper
Browse files Browse the repository at this point in the history
Split out model object protocol from view object, per MapKit and MVC. Crashes when tapping on a pin.

WIP for #894
  • Loading branch information
1ec5 committed Mar 4, 2015
1 parent 1a75453 commit 6cd782c
Show file tree
Hide file tree
Showing 7 changed files with 176 additions and 53 deletions.
3 changes: 3 additions & 0 deletions gyp/platform-ios.gypi
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,9 @@
'../platform/darwin/reachability.m',
'../include/mbgl/ios/MGLMapView.h',
'../platform/ios/MGLMapView.mm',
'../include/mbgl/ios/MGLAnnotation.h',
'../include/mbgl/ios/MGLAnnotationView.h',
'../platform/ios/MGLAnnotationView.m',
'../include/mbgl/ios/MGLStyleFunctionValue.h',
'../platform/ios/MGLStyleFunctionValue.m',
'../include/mbgl/ios/MGLTypes.h',
Expand Down
16 changes: 16 additions & 0 deletions include/mbgl/ios/MGLAnnotation.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,16 @@
//
// MGLAnnotation.h
// mbgl
//
// Created by Minh Nguyen on 2015-03-04.
//
//

#import <Foundation/Foundation.h>

@protocol MGLAnnotation <NSObject>

//! The receiver’s center, expressed as a coordinate on the containing map.
@property (assign) CLLocationCoordinate2D coordinate;

@end
24 changes: 24 additions & 0 deletions include/mbgl/ios/MGLAnnotationView.h
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
//
// MGLAnnotationView.h
// mbgl
//
// Created by Minh Nguyen on 2015-03-04.
//
//

#import <UIKit/UIKit.h>

@class SMCalloutView;
@protocol MGLAnnotation;

@interface MGLAnnotationView : UIImageView

@property (nonatomic) id <MGLAnnotation> annotation;

//! The receiver’s title string.
@property (nonatomic) NSString *title;

//! The receiver’s subtitle string.
@property (nonatomic) NSString *subtitle;

@end
13 changes: 11 additions & 2 deletions ios/app/MBXAnnotation.h
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,17 @@
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface MBXAnnotation : UIImageView
#import <mbgl/ios/MGLAnnotation.h>
#import <mbgl/ios/MGLAnnotationView.h>

@property (assign) CLLocationCoordinate2D coordinate;
@interface MBXAnnotation : NSObject <MGLAnnotation>

+ (instancetype)annotationWithLocation:(CLLocationCoordinate2D)coordinate;

- (instancetype)initWithLocation:(CLLocationCoordinate2D)coordinate;

@end

@interface MBXAnnotationView : MGLAnnotationView

@end
56 changes: 26 additions & 30 deletions ios/app/MBXAnnotation.m
Original file line number Diff line number Diff line change
Expand Up @@ -8,46 +8,42 @@

#import "MBXAnnotation.h"

@implementation MBXAnnotation
#import "SMCalloutView.h"

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self commonInit];
}
return self;
@implementation MBXAnnotation {
CLLocationCoordinate2D _coordinate;
}

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self commonInit];
}
return self;
}
@synthesize coordinate = _coordinate;

- (instancetype)initWithImage:(UIImage *)image {
if (self = [super initWithImage:image]) {
[self commonInit];
}
return self;
+ (instancetype)annotationWithLocation:(CLLocationCoordinate2D)coordinate {
return [[self alloc] initWithLocation:coordinate];
}

- (instancetype)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage {
if (self = [super initWithImage:image highlightedImage:highlightedImage]) {
[self commonInit];
- (instancetype)initWithLocation:(CLLocationCoordinate2D)coordinate {
if (self = [super init]) {
_coordinate = coordinate;
}
return self;
}

- (void)commonInit {
self.userInteractionEnabled = YES;
}
@end

/*
// Only override drawRect: if you perform custom drawing.
// An empty implementation adversely affects performance during animation.
- (void)drawRect:(CGRect)rect {
// Drawing code
}
*/
@implementation MBXAnnotationView

//- (id)initWithCoder:(NSCoder *)aDecoder {
// if (self = [super initWithCoder:aDecoder]) {
// [self commonInit];
// }
// return self;
//}
//
//- (void)commonInit {
//
//}
//
//- (void)didMoveToSuperview {
// [super didMoveToSuperview];
//}

@end
32 changes: 11 additions & 21 deletions ios/app/MBXViewController.mm
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
#import "MBXViewController.h"
#import "SMCalloutView.h"

#import <mbgl/ios/MGLMapView.h>

Expand All @@ -23,7 +22,6 @@
@interface MBXViewController () <UIActionSheetDelegate, CLLocationManagerDelegate>

@property (nonatomic) MGLMapView *mapView;
@property (nonatomic) SMCalloutView *calloutView;
@property (nonatomic) UITapGestureRecognizer *tapRecognizer;
@property (nonatomic) CLLocationManager *locationManager;
@property (nonatomic) NSMutableArray *features;
Expand Down Expand Up @@ -81,8 +79,6 @@ - (void)viewDidLoad
self.navigationController.navigationBar.tintColor = kTintColor;
self.mapView.tintColor = kTintColor;

self.calloutView = [SMCalloutView platformCalloutView];
[self.mapView addSubview:self.calloutView];
self.tapRecognizer = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(showPopupForTapRecognizer:)];
self.tapRecognizer.numberOfTapsRequired = 1;

Expand Down Expand Up @@ -127,12 +123,12 @@ - (void)viewDidAppear:(BOOL)animated
CLLocationCoordinate2D c = CLLocationCoordinate2DMake([feature[@"geometry"][@"coordinates"][1] doubleValue],
[feature[@"geometry"][@"coordinates"][0] doubleValue]);
CGPoint p = [self.mapView convertCoordinate:c toPointToView:self.mapView];
MBXAnnotation *ann = [[MBXAnnotation alloc] initWithImage:self.pin];
ann.coordinate = c;
ann.center = p;
[ann addGestureRecognizer:self.tapRecognizer];
[self.mapView.glView addSubview:ann];
[self.features addObject:ann];
MBXAnnotationView *annotationView = [[MBXAnnotationView alloc] initWithImage:self.pin];
annotationView.annotation = [MBXAnnotation annotationWithLocation:c];
annotationView.center = p;
[annotationView addGestureRecognizer:self.tapRecognizer];
[self.mapView.glView addSubview:annotationView];
[self.features addObject:annotationView];
}
}

Expand All @@ -156,13 +152,13 @@ - (void)refresh
if (self.mapView.centerCoordinate.latitude != self.lastCenter.latitude ||
self.mapView.centerCoordinate.longitude != self.lastCenter.longitude ||
self.mapView.zoomLevel != self.lastZoom) {
for (MBXAnnotation *ann in self.features) {
CGPoint p = [self.mapView convertCoordinate:ann.coordinate toPointToView:self.mapView];
for (MBXAnnotationView *annotationView in self.features) {
CGPoint p = [self.mapView convertCoordinate:annotationView.annotation.coordinate toPointToView:self.mapView];
if (CGRectContainsPoint(self.mapView.bounds, p)) {
ann.center = p;
[ann setHidden:NO];
annotationView.center = p;
[annotationView setHidden:NO];
} else {
[ann setHidden:YES];
[annotationView setHidden:YES];
}
}

Expand Down Expand Up @@ -203,12 +199,6 @@ - (NSUInteger)supportedInterfaceOrientations

#pragma mark - Actions

- (void)showPopupForTapRecognizer:(UITapGestureRecognizer *)tapRecognizer {
self.calloutView.title = @"Hey";
self.calloutView.subtitle = @"There!";
[self.calloutView presentCalloutFromRect:tapRecognizer.view.bounds inView:tapRecognizer.view constrainedToView:self.mapView animated:YES];
}

- (void)showSettings
{
UIActionSheet *sheet = [[UIActionSheet alloc] initWithTitle:@"Map Settings"
Expand Down
85 changes: 85 additions & 0 deletions platform/ios/MGLAnnotationView.m
Original file line number Diff line number Diff line change
@@ -0,0 +1,85 @@
//
// MGLAnnotationView.m
// mbgl
//
// Created by Minh Nguyen on 2015-03-04.
//
//

#import "MGLAnnotationView.h"

#import "../../calloutview/SMCalloutView.h"

@interface MGLAnnotationView ()

//! The callout view that pops up when the receiver is selected.
@property (nonatomic) SMCalloutView *calloutView;

@end

@implementation MGLAnnotationView

- (id)initWithCoder:(NSCoder *)aDecoder {
if (self = [super initWithCoder:aDecoder]) {
[self commonInit];
}
return self;
}

- (instancetype)initWithFrame:(CGRect)frame {
if (self = [super initWithFrame:frame]) {
[self commonInit];
}
return self;
}

- (instancetype)initWithImage:(UIImage *)image {
if (self = [super initWithImage:image]) {
[self commonInit];
}
return self;
}

- (instancetype)initWithImage:(UIImage *)image highlightedImage:(UIImage *)highlightedImage {
if (self = [super initWithImage:image highlightedImage:highlightedImage]) {
[self commonInit];
}
return self;
}

- (void)commonInit {
self.userInteractionEnabled = YES;
self.calloutView = [SMCalloutView platformCalloutView];
}

- (void)didMoveToSuperview {
[super didMoveToSuperview];

[self.superview addSubview:self.calloutView];
}

- (NSString *)title {
return self.calloutView.title;
}

- (void)setTitle:(NSString *)title {
self.calloutView.title = title;
}

- (NSSet *)keyPathsForValuesAffectingTitle {
return [NSSet setWithObject:@"calloutview.title"];
}

- (NSString *)subtitle {
return self.calloutView.subtitle;
}

- (void)setSubtitle:(NSString *)subtitle {
self.calloutView.subtitle = subtitle;
}

- (NSSet *)keyPathsForValuesAffectingSubtitle {
return [NSSet setWithObject:@"calloutview.subtitle"];
}

@end

0 comments on commit 6cd782c

Please sign in to comment.