Skip to content

Commit

Permalink
refs Outdooractive#64: simplestyle marker support for static map views
Browse files Browse the repository at this point in the history
  • Loading branch information
incanus committed Nov 10, 2012
1 parent 2cc70e1 commit 6ed8c42
Show file tree
Hide file tree
Showing 4 changed files with 88 additions and 31 deletions.
2 changes: 2 additions & 0 deletions MapView/Map/RMMapBoxSource.h
Expand Up @@ -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
11 changes: 7 additions & 4 deletions MapView/Map/RMMapBoxSource.m
Expand Up @@ -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
{
Expand All @@ -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]])
{
Expand Down Expand Up @@ -108,7 +110,7 @@ - (id)initWithTileJSON:(NSString *)tileJSON enablingDataOnMapView:(RMMapView *)m
dispatch_async(dispatch_get_main_queue(), ^(void)
{
[mapView addAnnotation:pointAnnotation];
});
});
}
}
}
Expand Down Expand Up @@ -175,6 +177,7 @@ - (id)initWithMapID:(NSString *)mapID enablingDataOnMapView:(RMMapView *)mapView

- (void)dealloc
{
dispatch_release(_dataQueue);
[_infoDictionary release];
[super dealloc];
}
Expand Down
55 changes: 36 additions & 19 deletions MapView/Map/RMStaticMapView.h
Expand Up @@ -27,32 +27,49 @@

#import <UIKit/UIKit.h>

/** 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 `<username>.map-<mapname>`.
* @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 `<username>.map-<mapname>`.
* @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 `<username>.map-<random characters>`.
* @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 `<username>.map-<mapname>`.
* @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 `<username>.map-<random characters>`.
* @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 `<username>.map-<mapname>`.
* @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
51 changes: 43 additions & 8 deletions MapView/Map/RMStaticMapView.m
Expand Up @@ -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
{
Expand All @@ -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;
Expand All @@ -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

0 comments on commit 6ed8c42

Please sign in to comment.