Description: creating a map with 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
}
// Executes custom example located in the body of this method
- (void) executeExample;
@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 filling map layer area background
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 map 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 text 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 do not have 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];
Thickness th = {0};
th.top = 30;
// Determine title text
[[m_view caption] setText: NSLocalizedString(@"WORLD_MAP", nil)];
// Set title margin
[[m_view caption] setMargin: th];
// Determine font parameters
[[m_view caption] setFont:[UIFont systemFontOfSize:18]];
// Use text direction from left to right
[m_view setUsingRightToLeft: NO];
// Get map legend
MapLegend *legend = [m_view legend];
// 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 blackColor]];
[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];
// 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];
// Set marker shape for map legend
[legend setMarkerShape: MarkerShapeRectangle];
// Set legend orientation
[legend setOrientation: NWLegendOrientationFreeVertical];
// Set legend position
CGPoint pt = {[m_view legend].origin.x, 325};
[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 timeline font parameters
[timeAxis setFont:[UIFont fontWithName:@"Arial" size:16]];
// Set minimum distance in points between two neighbor timeline tick marks
[timeAxis setMinTickSpacing:1.0];
// Set blue color for timeline tick marks
[timeAxis setTickColor: [UIColor blueColor]];
/* Set time of moving from one timeline value
to another one during animation playback */
[timeAxis setJumpTime: AnimateTypeNormal];
// Set current position of timeline slider
[timeAxis setIndex:0];
// Create and display label over 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 to array of displayed layers
[[m_view layers] addObject:layer];
[self setView: m_view];
// Execute custom example
[self executeExample];
}
// Executes custom example located in the body of this method
-(void) executeExample {
};
- (void)viewDidAppear:(BOOL)animated
{
[super viewDidAppear: animated];
[self mapChartDidAppear: m_view];
}
// Called after map layer are touch
- (void) mapChart:(MapChart2D *)mapChart touchDownInShape:(MapShape *)shape {
[self mapChart:mapChart touchDownInShapeWithID:[shape ID]];
}
// Called after map layer area touch
- (void) mapChart:(MapChart2D *)mapChart touchDownInShapeWithID:(NSString *)shapeId {
// Get map layer area by identifier
MapShape *shape = [[[mapChart layers] objectAtIndex:0] shapeWithId:shapeId];
/* Zoom and scroll
to display 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];
}
- (void)mapChartDidAppear:(MapChart *)mapChart {
// Set new size for component
struct CGRect frame;
frame = CGRectMake(0, 0, 375, 470);
[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];
}
// Called after timeline index change
-(void)mapChart:(MapChart *)mapChart setDoubleTimeAxisIndex:(double)idx {
// Determine map transparency depending on the current timeline index
double coef = idx - floor(idx);
double alpha = 1 - coef;
if (coef > 0.5) {
alpha = coef;
}
// Set new value of map transparency
[mapChart setAlpha:alpha];
// Fully redraw map layers
[mapChart invalidateOverlay];
}
@end
See also: