Description: a map is built by obtained data, contains two arrows and three tooltips.
#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 minimu 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 and set object for working with map topobase MapTopobase *topobase = [[MapTopobase alloc] initWithData:file]; [m_view setTopobase: topobase]; // Determine affine transformation applied to all map areas [topobase setTransform: CGAffineTransformIdentity]; [m_view setClipsToBounds:YES]; // Disable map zoom [m_view setZoomEnabled:NO]; // Create an object for working with map factor MapAreaVisual *visual = [[MapAreaVisual alloc] init]; // Set data source [visual setDataSource: datasource]; /* Determine whether map area background fill parameters depend on the data source */ [[visual background] setIsDependent:YES]; // Create an object for working with map layer MapLayer *layer = [[m_view topobase] rootLayer]; // Create a tooltip for layer layer.tooltip = [MapTooltip new]; // 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 [[visual background] setScale:scale]; // Set brush for map background fill [m_view setBackground:[SolidColorBrush solidColorBrushWithColor:[UIColor whiteColor]]]; // Set map title color m_view.caption.textColor = [UIColor blackColor]; // Set label text for map layer areas, for which there is not data [m_view setNoDataText: NSLocalizedString(@"NO_DATA", nil)]; // Create a brush for filling map layer areas with that have no data SolidColorBrush *brush = [[[SolidColorBrush alloc] init] autorelease]; // Set brush transparency [brush setOpacity: 0.2]; // Set brush color brush.color = [UIColor grayColor]; // Set this brush [[[visual background] scale] setNoData: brush]; Thickness th = {0}; th.top = 20; th.bottom = 20; // 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]; // Hide legend [legend setVisibility:NO]; // Get timeline MapTimeAxis *timeAxis = [m_view timeAxis]; // Hide timeline [timeAxis setHidden: YES]; // Create an arrow between regions MapArrow *arrow1 = [MapArrow new]; // Set label text arrow1.text = NSLocalizedString(@"MAP_ARROW_1", nil);; // Set label text color arrow1.textColor = [UIColor blackColor]; // Set fill color SolidColorBrush *br = [SolidColorBrush new]; [br setColor:[UIColor lightGrayColor]]; // Set label background arrow1.textBackground = br; // Set font family arrow1.fontName = @"Arial"; // Set font size arrow1.fontSize = 15.0; // Set map area, in which arrow starts arrow1.startShape = [layer shapeWithId:@"US"]; // Set map area, in which arrow ends arrow1.endShape = [layer shapeWithId:@"RU"]; // Set arrow bend angle arrow1.angle = 0; // Set border line color arrow1.strokeColor = [UIColor blackColor]; // Set border line thickness arrow1.strokeThickness = 1.0; // Set border line type arrow1.strokeStyle = StrokeStyleSolid; // Set arrow pointer width [arrow1 setPointerWidth:10]; // Set arrow pointer length [arrow1 setPointerLength:20]; CGPoint start = {100.0,50.0}; arrow1.startPoint = start; // Map point, in which arrow starts CGPoint end = {300.0,450.0}; arrow1.endPoint = end; // Map point, in which arrow ends // Arrow between map points MapFilledArrow *arrow2 = [MapFilledArrow new]; arrow2.base = 10; arrow2.fill = br; // Set label text arrow2.text = NSLocalizedString(@"MAP_ARROW_2", nil); // Set label text color arrow2.textColor = [UIColor blackColor]; arrow2.textAlignment = UITextAlignmentCenter; arrow2.textOrientation = TextOrientationAlongTheArrow; // Set brush color [br setColor:[UIColor lightGrayColor]]; // Set label background arrow2.textBackground = [br autorelease]; CGPoint st = {100.0,50.0}; // Set map point, in which arrow starts arrow1.startPoint = st; CGPoint en = {300.0,450.0}; // Set map point, in which arrow ends arrow2.endPoint = en; // Set arrow bend angle arrow2.angle = 10; // Set border line color arrow2.strokeColor = [UIColor blackColor]; // Set border line thickness arrow2.strokeThickness = 2.0; // Set border line type arrow2.strokeStyle = StrokeStyleSolid; // Set arrow pointer width [arrow2 setPointerWidth:30]; // Set arrow pointer length [arrow2 setPointerLength:50]; // Create an array of arrows NSArray *arrows = [NSArray arrayWithObjects:arrow1,arrow2, nil]; [m_view setArrows:arrows]; // Display arrows [m_view setShowArrows:YES]; // Create tooltips for map areas NSArray *countries = [NSArray arrayWithObjects:@"AU", @"CN", @"RU", nil]; for(int i = 0; i < [countries count]; i++) { MapShape *shape = [layer shapeWithId: [countries objectAtIndex:i]]; shape.strokeColor = [UIColor darkGrayColor]; shape.strokeThickness = 2.0; MapTooltip *tip = [MapTooltip new]; tip.visibility = YES; tip.text = shape.title; tip.font = [UIFont fontWithName:@"Arial" size:14]; tip.background = [SolidColorBrush solidColorBrushWithColor:[UIColor whiteColor]]; tip.borderColor = [UIColor blackColor]; tip.borderThickness = 1.0; tip.borderRadius = 3.0; Thickness pad = {2.0,2.0,2.0,2.0}; tip.padding = pad; shape.tooltip = tip; } /* 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]; } - (void)viewDidAppear:(BOOL)animated { [super viewDidAppear:animated]; // Set new map size struct CGRect frame; frame = CGRectMake(0, 0, 375, 470); [m_view setFrame:frame]; // Set gray color border [[m_view layer] setBorderColor: [[UIColor colorWithRed:0.75 green:0.75 blue:0.75 alpha:1] CGColor]]; [[m_view layer] setBorderWidth: 1.0f]; } - (void)mapChartDidAppear:(MapChart *)mapChart { } @end
See also: