Description: creating a map with a timeline, which data is loaded from file.
#import <MapCharting/MapChart.h>
#import <MapCharting/MapChart2D.h>
#import "MapDataSource.h"
@interface ViewController : UIViewController<MapChartDelegate>{
MapChart2D *m_view; // 2D map
MapDataSource *datasource; // Data source
}
@end
#import "ViewController.h"
#import <MapCharting/MapTopobase.h>
#import <MapCharting/MapAreaVisual.h>
#import "SolidColorBrush.h"
#import "DataDependency.h"
#import <MapCharting/MapTooltip.h>
#import <MapCharting/MapLabel.h>
#import <MapCharting/MapLegend.h>
#import <MapCharting/MapShape.h>
#import "UIColor+transition.h"
#import <MapCharting/MapLabel.h>
#import <MapCharting/MapChartSign.h>
#import <MapCharting/MapArrow.h>
#import <MapCharting/MapFilledArrow.h>
#import <MapCharting/MapBarVisual.h>
#import <MapCharting/MapPieVisual.h>
#import "UIColor+hex.h"
@implementation ViewController
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Load data from source
datasource = [[MapDataSource alloc]initWithFileName:@"data.txt"];
// Determine minimum and maximum data values
[datasource prepare];
// Create a map based on loaded data
[self dataSourceFinishedLoadData];
}
// Creates a map based on loaded data
- (void)dataSourceFinishedLoadData
{
// Create a 2D map
m_view = [[MapChart2D alloc] init];
// Load file with topobase
NSData *file = [NSData dataWithContentsOfFile:[[NSBundle mainBundle] pathForResource:@"world.svg" ofType:nil]];
// Create an object for working with map topobase
MapTopobase *topobase = [[MapTopobase alloc] initWithData:file];
// Create an object for working with map layer
MapLayer *layer = [topobase rootLayer];
// Create a tooltip for layer
layer.tooltip = [MapTooltip new];
// Set object for working with map topobase
[m_view setTopobase:[layer topobase]];
// Set object containing methods for working with map
[m_view setDelegate: self];
// Determine affine transformation applied to all map areas
[[m_view topobase] setTransform: CGAffineTransformIdentity];
// Create an object for working with map factor
MapAreaVisual *visual = [[MapAreaVisual alloc] init];
/* Determine whether map area background fill parameters
depend on data source */
DataDependency *dataDependency = [DataDependency dependency];
[dataDependency setIsDependent:YES];
// Set data source
[visual setDataSource: datasource];
[dataDependency setDataSource:datasource];
// Set colors for map layer area background fill
NSArray *colors = [NSArray arrayWithObjects:@"ff462c", @"ebaf36", @"ffd900", @"b1ca40", @"6a8535", nil];
// Determine minimum map value
double value = [datasource minValue];
// Determine map scale step
double step = (datasource.maxValue - datasource.minValue) / [colors count];
// Determine correspondence between map scale intervals and layer area background fill colors
ValueScale *scale = [[ValueScale new] autorelease];
for (int i = 1; i <[colors count]; i++) {
value += step;
[scale.value addObject:[NSNumber numberWithDouble:value]];
// Determine colors for the Less value
UIColor *lessColor = [UIColor colorWithHex:[colors objectAtIndex:(i - 1)]];
[scale.less addObject:[SolidColorBrush solidColorBrushWithColor:lessColor]];
// Determine colors for the Equal or Greater value
UIColor *equalColor = [UIColor colorWithHex:[colors objectAtIndex:(i)]];
[scale.equal addObject:[SolidColorBrush solidColorBrushWithColor:equalColor]];
[scale.greater addObject:[SolidColorBrush solidColorBrushWithColor:equalColor]];
}
// Set scale
[dataDependency setScale:scale];
[visual setBackground:dataDependency];
// Set brush for map background fill
UIColor *backgroundColor = [[[Brush new] autorelease] mainColor];
[m_view setBackground:[SolidColorBrush solidColorBrushWithColor:backgroundColor]];
// Set map title color
m_view.caption.textColor = [UIColor blackColor];
// Set label color for map layer areas, for which there is no data
[m_view setNoDataText: NSLocalizedString(@"NO_DATA", nil)];
// Create a brush for filling map layer areas that have not data
SolidColorBrush *brush = [[[SolidColorBrush alloc] init] autorelease];
// Set brush transparency
[brush setOpacity: 0.2];
// Set brush color
[brush setColor: [UIColor grayColor]];
// Set this brush
[[[visual background] scale] setNoData: brush];
// Get map legend
MapLegend *legend = [m_view legend];
// Display legend
[legend setVisibility: YES];
// Set scale
[legend setValueScale: scale];
// Align legend by map center
[legend setBlockAlignment: NWLegendBlockAlignmentBottomCenter];
// Set legend margins
Thickness margin = {0,20,0,0};
[legend setMargin:margin];
// Set legend text color and font parameters
[legend setTextColor:[UIColor darkGrayColor]];
[legend setFont:[UIFont systemFontOfSize:12]];
// Set the Less interval record format
NSString *lessFormat = [NSMutableString stringWithString:NSLocalizedString(@"LESS_FORMAT", nil)];
[legend setLessFormat:lessFormat];
// Set the Greater interval record format
NSString *greaterFormat = [NSMutableString stringWithString:NSLocalizedString(@"GREATER_FORMAT", nil)];
[legend setGreaterFormat:greaterFormat];
// Set between interval record format
NSString *betweenFormat = [NSMutableString stringWithString:NSLocalizedString(@"BETWEEN_FORMAT", nil)];
[legend setBetweenFormat:betweenFormat];
// Enable displaying of equality relations in legend
[legend setShowEquals: YES];
NSString *equalFormat = [NSMutableString stringWithString:NSLocalizedString(@"EQUAL_FORMAT", nil)];
[legend setEqualFormat: equalFormat];
// Create legend top text
MapLabel *header = [[MapLabel new] autorelease];
[header setText: NSLocalizedString(@"HEADER_TEXT", nil)];
// Set text font parameters
[header setFont: [UIFont systemFontOfSize:14]];
[header setTextColor:[UIColor blackColor]];
// Set text margins
Thickness headerMargin = {5, 0, 0, 0};
[header setMargin:headerMargin];
// Set legend top text
[[m_view legend] setHeader:header];
// Create and set legend bottom text
MapLabel *footer = [MapLabel new];
[footer setText: NSLocalizedString(@"FOOTER_TEXT", nil)];
// Set text margins
Thickness footerMargin = [footer margin];
footerMargin.top = 80;
footerMargin.left = -60;
[footer setMargin:footerMargin];
[legend setFooter: footer];
// Set marker shape for map legend
[legend setMarkerShape: MarkerShapeRhombus];
// Set legend orientation
[legend setOrientation: NWLegendOrientationFreeVertical];
// Set legend position
CGPoint pt = {[m_view legend].origin.x, 375};
[legend setOrigin: pt];
// Get timeline
MapTimeAxis *timeAxis = [m_view timeAxis];
// Display timeline
[timeAxis setHidden: NO];
// Set object containing methods for working with timeline
[timeAxis setDelegate: m_view];
// Set data source
[timeAxis setDataSource:datasource];
// Set up animation delay
[timeAxis setDelayTime: AnimateTypeSlow];
// Set font parameters for timeline
[timeAxis setFont:[UIFont fontWithName:@"Arial" size:16]];
// Set minimum distance in points between two neighbor tick marks
[timeAxis setMinTickSpacing:1.0];
// Set blue color for tick marks
[timeAxis setTickColor: [UIColor blueColor]];
/* Set time of moving from one tick mark
to other one during animation playback */
[timeAxis setJumpTime: AnimateTypeNormal];
// Set current timeline slider position
[timeAxis setIndex:0];
// Create and display label over the current timeline slider position
MapTooltip *caption = [[MapTooltip new] autorelease];
[caption setVisibility:YES];
[timeAxis setLabel: caption];
/* Add an object for working with map factor
into array of visual elements corresponding to child layers and areas
*/
[[layer visuals] addObject: visual];
// Add map layer into array of displayed layers
[[m_view layers] addObject:layer];
[self setView: m_view];
}
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear: animated];
[self mapChartDidAppear: m_view];
}
- (void)mapChartDidAppear:(MapChart *)mapChart {
// Set new component size
struct CGRect frame;
frame = CGRectMake(0, 0, 375, 550);
[mapChart setFrame:frame];
// Set gray color border
[[mapChart layer] setBorderColor: [[UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:1] CGColor]];
[[mapChart layer] setBorderWidth: 1.0f];
// Create three toolbars
UIToolbar *toolBar1 = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 0, frame.size.width, 35)] autorelease];
UIToolbar *toolBar2 = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 30, frame.size.width, 35)] autorelease];
UIToolbar *toolBar3 = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 60, frame.size.width, 35)] autorelease];
UIToolbar *toolBar4 = [[[UIToolbar alloc] initWithFrame:CGRectMake(0, 90, frame.size.width, 35)] autorelease];
// Add created toolbars
[self.view addSubview:toolBar1];
[self.view addSubview:toolBar2];
[self.view addSubview:toolBar3];
[self.view addSubview:toolBar4];
// Determine button display settings
NSDictionary *appearSettings = [NSDictionary dictionaryWithObjectsAndKeys: [UIFont fontWithName:@"Arial" size:14], UITextAttributeFont, nil];
// Create required buttons
UIBarButtonItem *toFitButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"ToFit", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(toFit)] autorelease];
[toFitButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *invButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Invalidate", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(invalidate)] autorelease];
[invButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *invOverlayButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"InvalidateOverlay", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(invalidateOverlay)] autorelease];
[invOverlayButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *showAllLabelsButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"ShowLabels", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(showAllLabels)] autorelease];
[showAllLabelsButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *hideAllLabelsButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"HideLabels", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(hideAllLabels)] autorelease];
[hideAllLabelsButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *resetButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"ResetTimeline", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(reset)] autorelease];
[resetButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *backButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Back", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(back)] autorelease];
[backButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *forwardButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Forward", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(forward)] autorelease];
[forwardButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *abortButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Abort", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(abort)] autorelease];
[abortButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
UIBarButtonItem *stopButton = [[[UIBarButtonItem alloc] initWithTitle:NSLocalizedString(@"Stop", nil) style:UIBarButtonItemStyleBordered target:self action:@selector(stop)] autorelease];
[stopButton setTitleTextAttributes:appearSettings forState:UIControlStateNormal];
// Add buttons to toolbars
[toolBar1 setItems: [NSArray arrayWithObjects: toFitButton,invButton,invOverlayButton, nil]];
[toolBar2 setItems: [NSArray arrayWithObjects: showAllLabelsButton,hideAllLabelsButton, nil]];
[toolBar3 setItems: [NSArray arrayWithObjects: forwardButton, backButton, resetButton, nil]];
[toolBar4 setItems: [NSArray arrayWithObjects: abortButton, stopButton, nil]];
}
// Places map by center and zooms it to fit the screen
- (void) toFit
{
[m_view sizeContentToFit];
}
// Fully redraws map
- (void) invalidate
{
[m_view invalidate];
}
// Fully redraws map layers
- (void)invalidateOverlay
{
[m_view invalidateOverlay];
}
// Resets all timeline data and reloads it from data source
- (void)reset
{
[m_view.timeAxis reset];
}
// Moves timeline slider forward
- (void)back
{
[m_view.timeAxis stepBackward];
}
//Moves timeline slider backward
- (void)forward
{
[m_view.timeAxis stepForward];
}
// Aborts timeline animation playback
- (void)abort
{
[m_view.timeAxis abort];
}
// Stops timeline animation playback
-(void) stop {
[m_view.timeAxis stop];
}
// Displays labels for all map layer areas
- (void)showAllLabels
{
// Create a label for map layer areas
MapLabel *label = [MapLabel new];
// Make label visible
label.visibility = YES;
label.font = [UIFont fontWithName:@"Arial" size:14];
// Set mask
label.maskText = @"%@";
label.maxWidth = 150.0;
label.minWidth = 100.0;
label.textColor = [UIColor blackColor];
label.textWrapping = TextWrappingNoWrap;
// Set tag for all map layer areas
MapLayer *currentLayer = [[m_view layers] objectAtIndex:0];
NSArray *shapes = [currentLayer shapesInRect:[currentLayer bounds]];
for (MapShape *shape in shapes) {
NSString *shapeId = [shape ID];
if ([shapeId isEqual: @"AU"] || [shapeId isEqual: @"RU"] || [shapeId isEqual: @"US"]) {
[shape setLabel: label];
}
}
[label autorelease];
// Rerender map
[self toFit];
}
// Removes labels of all map layer areas
- (void)hideAllLabels {
// Remove labels of all map layer areas
NSArray *shapes = [[m_view.layers objectAtIndex:0] allShapes];
for (MapShape *shape in shapes) {
[shape setLabel: NULL];
}
// Fully redraw map
[self toFit];
}
// Called after map layer area touch
- (void) mapChart:(MapChart2D *)mapChart touchDownInShape:(MapShape *)shape {
/* Zoom and scroll
for displaying the specified map layer area with animation playback */
[mapChart zoomToShape:shape animated:YES];
// Display tooltip for map layer area
[m_view showPopupInShape:shape];
// Fully redraw map
[mapChart invalidate];
}
@end
See also: