Skip to content

Commit

Permalink
Document designated initializer. Only remove observer for myLocation …
Browse files Browse the repository at this point in the history
…if it was successfully added
  • Loading branch information
myell0w committed May 13, 2013
1 parent db311fe commit 0d4004c
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 4 deletions.
14 changes: 14 additions & 0 deletions MTDirectionsKit/UI/GoogleMapsSDK/MTDGMSMapView.h
Expand Up @@ -41,6 +41,20 @@
*/
@interface MTDGMSMapView : GMSMapView <MTDMapView>

/******************************************
@name Lifecycle
******************************************/

/**
Creates and returns an instance of MTDGMSMapView.
@param frame The frame rectangle for the view, measured in points. The origin of the frame is relative to the superview in which you plan to add it. This method uses the frame rectangle to set the center and bounds properties accordingly.
@param camera Controls the camera, which defines how the map is oriented
@return an instance of MTDGMSMapView
*/
+ (instancetype)mapWithFrame:(CGRect)frame camera:(GMSCameraPosition *)camera;

/******************************************
@name Delegate
******************************************/
Expand Down
25 changes: 21 additions & 4 deletions MTDirectionsKit/UI/GoogleMapsSDK/MTDGMSMapView.m
Expand Up @@ -36,7 +36,9 @@ @interface MTDGMSMapView () <GMSMapViewDelegate>
@end


@implementation MTDGMSMapView
@implementation MTDGMSMapView {
BOOL _observingLocation;
}

@synthesize directionsOverlay = _directionsOverlay;
@synthesize directionsDisplayType = _directionsDisplayType;
Expand All @@ -45,14 +47,23 @@ @implementation MTDGMSMapView
#pragma mark - Lifecycle
////////////////////////////////////////////////////////////////////////

- (id)initWithFrame:(CGRect)frame camera:(GMSCameraPosition *)camera {
+ (instancetype)mapWithFrame:(CGRect)frame camera:(GMSCameraPosition *)camera {
return (MTDGMSMapView *)[super mapWithFrame:frame camera:camera];
}

- (instancetype)initWithFrame:(CGRect)frame camera:(GMSCameraPosition *)camera {
if ((self = [super initWithFrame:frame camera:camera])) {
[self mtd_setup];
}

return self;
}

- (id)initWithFrame:(__unused CGRect)frame {
MTDAssert(NO, @"Unable to create an instance of MTDGSMMapView with initWithFrame:. Use + [MTDGSMMapView mapViewWithFrame:camera:] instead.");
return nil;
}

- (id)initWithCoder:(NSCoder *)aDecoder {
if ((self = [super initWithCoder:aDecoder])) {
[self mtd_setup];
Expand All @@ -66,7 +77,10 @@ - (void)dealloc {
_mtd_proxy = nil;
self.delegate = nil;
[self cancelLoadOfDirections];
[self removeObserver:self forKeyPath:MTDKey(myLocation)];

if (_observingLocation) {
[self removeObserver:self forKeyPath:MTDKey(myLocation)];
}
}

////////////////////////////////////////////////////////////////////////
Expand Down Expand Up @@ -390,7 +404,10 @@ - (void)mtd_setup {
_directionsEdgePadding = 125.f;
_mtd_proxy = [[MTDMapViewProxy alloc] initWithMapView:self];

[self addObserver:self forKeyPath:MTDKey(myLocation) options:NSKeyValueObservingOptionNew context:&myLocationContext];
@synchronized(self) {
[self addObserver:self forKeyPath:MTDKey(myLocation) options:NSKeyValueObservingOptionNew context:&myLocationContext];
_observingLocation = YES;
}
}

- (void)mtd_addOverlay:(MTDDirectionsOverlay *)overlay {
Expand Down

0 comments on commit 0d4004c

Please sign in to comment.