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 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 and set object for working with map topobase
MapTopobase *topobase = [[MapTopobase alloc] initWithData:file];
[m_view setTopobase: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];
// Set data source
[visual setDataSource: datasource];
/* Determine whether map area background fill parameters
depend on 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 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 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 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.color = [UIColor grayColor];
// Set this brush
[[[visual background] scale] setNoData: brush];
// Use text direction from left to right
[m_view setUsingRightToLeft: NO];
// Get map legend
MapLegend *legend = [m_view legend];
// Hide map legend
[legend setVisibility:NO];
// Get timeline
MapTimeAxis *timeAxis = [m_view timeAxis];
// Hide timeline
[timeAxis setHidden: YES];
/* 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];
// Determine whether icons are displayed
[m_view setShowSigns: YES];
// Create an image
UIImage *img = [UIImage imageNamed: @"sign.gif"];
// Create an icon on map with the use of image
MapChartSign *mapSign = [[MapChartSign alloc] initWithImage:img];
// Specify map layer area with identifier
[mapSign setShape:@"RU"];
// Set icon offset
[mapSign setPoint: CGPointMake(0.5, 0.7)];
// Set array of map icons
[m_view setSigns: [[NSArray alloc] initWithObjects:mapSign, nil]];
// Create labels for map layer areas
MapLabel *label = [MapLabel new];
// Determine whether labels are displayed
[label setVisibility: YES];
[label setFont: [UIFont fontWithName:@"Arial" size:16]];
// Region name is set automatically
[label setText: @""];
[label setMaxWidth: 150.0];
[label setMinWidth: 100.0];
[label setTextColor: [UIColor blackColor]];
[label setTextWrapping: TextWrappingNoWrap];
// Display border and labels for specified map layer areas
NSArray *countries = [NSArray arrayWithObjects:@"RU", @"AU", @"US", nil];
for (NSString *Id in countries) {
MapShape *shape = [layer shapeWithId:Id];
[shape setStrokeColor: [UIColor colorWithHex:@"#ffa604"]];
[shape setStrokeThickness: 2.0];
[shape setLabel: label];
}
// Execute custom example
[self executeExample];
}
// Executes custom example located in the body of this method
-(void) executeExample {
};
- (void)viewDidAppear:(BOOL)animated {
[super viewDidAppear:animated];
// Set new size of component
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];
}
// Called after map layer area touch
- (void) mapChart:(MapChart2D *)mapChart touchDownInShape:(MapShape *)shape
{
}
- (void)mapChartDidAppear:(MapChart *)mapChart {
}
@end
See also: