From 6ed8c426b440e5d1014fe3143c2d66fe38493002 Mon Sep 17 00:00:00 2001 From: "Justin R. Miller" Date: Fri, 9 Nov 2012 17:35:40 -0800 Subject: [PATCH] refs #64: simplestyle marker support for static map views --- MapView/Map/RMMapBoxSource.h | 2 ++ MapView/Map/RMMapBoxSource.m | 11 ++++--- MapView/Map/RMStaticMapView.h | 55 +++++++++++++++++++++++------------ MapView/Map/RMStaticMapView.m | 51 +++++++++++++++++++++++++++----- 4 files changed, 88 insertions(+), 31 deletions(-) diff --git a/MapView/Map/RMMapBoxSource.h b/MapView/Map/RMMapBoxSource.h index ca825c4cb..a3ce309ab 100644 --- a/MapView/Map/RMMapBoxSource.h +++ b/MapView/Map/RMMapBoxSource.h @@ -113,4 +113,6 @@ typedef enum : NSUInteger { * Note that you may want to clear the tile cache after changing this value in order to provide a consistent experience. */ @property (nonatomic, assign) RMMapBoxSourceQuality imageQuality; +@property (nonatomic, readonly, assign) dispatch_queue_t dataQueue; + @end diff --git a/MapView/Map/RMMapBoxSource.m b/MapView/Map/RMMapBoxSource.m index 0ec29fc2c..675211a01 100644 --- a/MapView/Map/RMMapBoxSource.m +++ b/MapView/Map/RMMapBoxSource.m @@ -48,7 +48,7 @@ - (id)initWithInfo:(NSDictionary *)info; @implementation RMMapBoxSource -@synthesize infoDictionary=_infoDictionary, imageQuality=_imageQuality; +@synthesize infoDictionary=_infoDictionary, imageQuality=_imageQuality, dataQueue=_dataQueue; - (id)initWithMapID:(NSString *)mapID { @@ -64,15 +64,17 @@ - (id)initWithTileJSON:(NSString *)tileJSON enablingDataOnMapView:(RMMapView *)m { if (self = [super init]) { + _dataQueue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL); + _infoDictionary = (NSDictionary *)[[NSJSONSerialization JSONObjectWithData:[tileJSON dataUsingEncoding:NSUTF8StringEncoding] options:0 error:nil] retain]; - + id dataObject; if (mapView && (dataObject = [_infoDictionary objectForKey:@"data"]) && dataObject) { - dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0), ^(void) + dispatch_async(_dataQueue, ^(void) { if ([dataObject isKindOfClass:[NSArray class]] && [[dataObject objectAtIndex:0] isKindOfClass:[NSString class]]) { @@ -108,7 +110,7 @@ - (id)initWithTileJSON:(NSString *)tileJSON enablingDataOnMapView:(RMMapView *)m dispatch_async(dispatch_get_main_queue(), ^(void) { [mapView addAnnotation:pointAnnotation]; - }); + }); } } } @@ -175,6 +177,7 @@ - (id)initWithMapID:(NSString *)mapID enablingDataOnMapView:(RMMapView *)mapView - (void)dealloc { + dispatch_release(_dataQueue); [_infoDictionary release]; [super dealloc]; } diff --git a/MapView/Map/RMStaticMapView.h b/MapView/Map/RMStaticMapView.h index 96de60455..441ee0514 100644 --- a/MapView/Map/RMStaticMapView.h +++ b/MapView/Map/RMStaticMapView.h @@ -27,32 +27,49 @@ #import -/** An RMStaticMapView object provides an embeddable, static map image view. You use this class to display map information in your application that does not need to change or provide user interaction. You can center the map on a given coordinate and zoom level, specify the size of the area you want to display, and optionally provide a completion handler that can be performed upon map load. - * - * Note that although RMStaticMapView is a subclass of RMMapView, it is provided for convenience only. Unpredictable results may occur if the view is treated as a normal, dynamic map view. If you need to support annotations, multiple tile sources, or other complex functionality, you should use RMMapView directly. - * - * @warning Please note that you are responsible for getting permission to use the map data, and for ensuring your use adheres to the relevant terms of use. You are also responsible for displaying attribution details for the data elsewhere in your application, if applicable. */ +/** An RMStaticMapView object provides an embeddable, static map image view. You use this class to display map information in your application that does not need to change or provide user interaction. You can center the map on a given coordinate and zoom level, specify the size of the area you want to display, and optionally provide a completion handler that can be performed upon map load. The map can also automatically have markers added to it if they were provided in simplestyle data when the map was created. +* +* @warning Please note that you are responsible for getting permission to use the map data, and for ensuring your use adheres to the relevant terms of use. You are also responsible for displaying attribution details for the data elsewhere in your application, if applicable. */ @interface RMStaticMapView : RMMapView /** @name Initializing a Static Map View */ +/** Initialize a static map view with a given frame and mapID. +* +* The map ID is expected to provide initial center coordinate and zoom level information, and may optionally also include simplestyle marker data, which will automatically be added to the map view. +* @param frame The frame with which to initialize the map view. +* @param mapID The MapBox map ID string, typically in the format `.map-`. +* @return An initialized map view, or `nil` if the map view was unable to be initialized. */ +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID; + +/** Initialize a static map view with a given frame and mapID, performing a block upon completion of the map load. +* +* The map ID is expected to provide initial center coordinate and zoom level information, and may optionally also include simplestyle marker data, which will automatically be added to the map view. The block will be performed upon completion of the load of both the map and of any simplestyle markers included with the map ID. +* @param frame The frame with which to initialize the map view. +* @param mapID The MapBox map ID string, typically in the format `.map-`. +* @param handler A block to be performed upon map and marker load completion. An image of the map, including markers, is passed as an argument to the block in the event that you wish to use it elsewhere. The handler will be called on the main dispatch queue. +* @return An initialized map view, or `nil` if the map view was unable to be initialized. */ +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID completionHandler:(void (^)(UIImage *))handler; + /** Initialize a static map view with a given frame, mapID, center coordinate, and zoom level. - * @param frame The frame with which to initialize the map view. - * @param mapID The MapBox map ID string, typically in the format `.map-`. - * @param centerCoordinate The map center coordinate. - * @param zoomLevel The map zoom level. - * @return An initialized map view, or `nil` if the map view was unable to be initialized. */ +* +* The map ID may optionally also include simplestyle marker data, which will automatically be added to the map view. +* @param frame The frame with which to initialize the map view. +* @param mapID The MapBox map ID string, typically in the format `.map-`. +* @param centerCoordinate The map center coordinate. +* @param zoomLevel The map zoom level. +* @return An initialized map view, or `nil` if the map view was unable to be initialized. */ - (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel; -/** Initialize a static map view with a given frame, mapID, center coordinate, and zoom level, performing a block upon completion of the map load. - * @param frame The frame with which to initialize the map view. - * @param mapID The MapBox map ID string, typically in the format `.map-`. - * @param centerCoordinate The map center coordinate. - * @param zoomLevel The map zoom level. - * @param handler A block to be performed upon map load completion. An image of the map is passed as an argument to the block in the event that you wish to use it elsewhere. The handler will be called on the main dispatch queue. - * @return An initialized map view, or `nil` if the map view was unable to be initialized. */ +/** Designated initializer. Initialize a static map view with a given frame, mapID, center coordinate, and zoom level, performing a block upon completion of the map load. +* +* The map ID may optionally also include simplestyle marker data, which will automatically be added to the map view. The block will be performed upon completion of the load of both the map and of any simplestyle markers included with the map ID. +* @param frame The frame with which to initialize the map view. +* @param mapID The MapBox map ID string, typically in the format `.map-`. +* @param centerCoordinate The map center coordinate. +* @param zoomLevel The map zoom level. +* @param handler A block to be performed upon map load completion. An image of the map, including markers, is passed as an argument to the block in the event that you wish to use it elsewhere. The handler will be called on the main dispatch queue. +* @return An initialized map view, or `nil` if the map view was unable to be initialized. */ - (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel completionHandler:(void (^)(UIImage *))handler; -// TODO: static markers - @end diff --git a/MapView/Map/RMStaticMapView.m b/MapView/Map/RMStaticMapView.m index 69d876c26..b4b5115ac 100644 --- a/MapView/Map/RMStaticMapView.m +++ b/MapView/Map/RMStaticMapView.m @@ -28,6 +28,19 @@ #import "RMStaticMapView.h" @implementation RMStaticMapView +{ + __weak RMStaticMapView *_weakSelf; +} + +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID +{ + return [self initWithFrame:frame mapID:mapID centerCoordinate:CLLocationCoordinate2DMake(MAXFLOAT, MAXFLOAT) zoomLevel:-1 completionHandler:nil]; +} + +- (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID completionHandler:(void (^)(UIImage *))handler +{ + return [self initWithFrame:frame mapID:mapID centerCoordinate:CLLocationCoordinate2DMake(MAXFLOAT, MAXFLOAT) zoomLevel:-1 completionHandler:handler]; +} - (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel { @@ -36,9 +49,23 @@ - (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLo - (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLocationCoordinate2D)centerCoordinate zoomLevel:(CGFloat)zoomLevel completionHandler:(void (^)(UIImage *))handler { - if (!(self = [super initWithFrame:frame andTilesource:[[[RMMapBoxSource alloc] initWithMapID:mapID] autorelease] centerCoordinate:centerCoordinate zoomLevel:zoomLevel maxZoomLevel:zoomLevel minZoomLevel:zoomLevel backgroundImage:nil])) + if (!(self = [super initWithFrame:frame])) return nil; + RMMapBoxSource *tileSource = [[[RMMapBoxSource alloc] initWithMapID:mapID enablingDataOnMapView:self] autorelease]; + + self.tileSource = tileSource; + + if ( ! CLLocationCoordinate2DIsValid(centerCoordinate)) + centerCoordinate = [tileSource centerCoordinate]; + + [self setCenterCoordinate:centerCoordinate animated:NO]; + + if (zoomLevel < 0) + zoomLevel = [tileSource centerZoom]; + + [self setZoom:zoomLevel]; + self.backgroundColor = [UIColor colorWithPatternImage:[RMMapView resourceImageNamed:@"LoadingTile.png"]]; self.hideAttribution = YES; @@ -47,21 +74,29 @@ - (id)initWithFrame:(CGRect)frame mapID:(NSString *)mapID centerCoordinate:(CLLo self.userInteractionEnabled = NO; - __unsafe_unretained RMStaticMapView *weakSelf = self; + _weakSelf = self; - dispatch_async(dispatch_get_main_queue(), ^(void) + dispatch_async(tileSource.dataQueue, ^(void) { - if (weakSelf) + dispatch_sync(dispatch_get_main_queue(), ^(void) { - UIImage *image = [weakSelf takeSnapshot]; + UIImage *image = [_weakSelf takeSnapshot]; - if (image) - handler(image); - } + handler(image); + }); }); return self; } +- (void)addAnnotation:(RMAnnotation *)annotation +{ + annotation.layer = [[RMMarker alloc] initWithMapBoxMarkerImage:[annotation.userInfo objectForKey:@"marker-symbol"] + tintColorHex:[annotation.userInfo objectForKey:@"marker-color"] + sizeString:[annotation.userInfo objectForKey:@"marker-size"]]; + + [super addAnnotation:annotation]; +} + @end