PageRenderTime 53ms CodeModel.GetById 1ms app.highlight 49ms RepoModel.GetById 1ms app.codeStats 0ms

/samples/MarkerMurder/Classes/MainViewController.m

http://github.com/route-me/route-me
Objective C | 140 lines | 99 code | 26 blank | 15 comment | 4 complexity | 6b82111e90a990aa4ff03696b8f0a139 MD5 | raw file
  1//
  2//  MainViewController.m
  3//  SampleMap : Diagnostic map
  4//
  5
  6#import "MainViewController.h"
  7#import "MarkerMurderAppDelegate.h"
  8
  9#import "MainView.h"
 10
 11#import "RMOpenAerialMapSource.h"
 12#import "RMOpenStreetMapSource.h"
 13#import "RMMapContents.h"
 14#import "RMMapView.h"
 15#import "RMMarkerManager.h"
 16#import "RMMarker.h"
 17#import "RMMercatorToScreenProjection.h"
 18#import "RMProjection.h"
 19
 20@implementation MainViewController
 21
 22@synthesize mapView;
 23@synthesize infoTextView;
 24
 25- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
 26    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
 27        // Custom initialization
 28    }
 29    return self;
 30}
 31
 32- (void)addMarkers
 33{
 34	CLLocationCoordinate2D markerPosition;
 35#define kNumberRows 1
 36#define kNumberColumns 9
 37#define kSpacing 2.0
 38
 39	UIImage *redMarkerImage = [UIImage imageNamed:@"marker-red.png"];
 40	UIImage *blueMarkerImage = [UIImage imageNamed:@"marker-blue.png"];
 41	UIImage *xMarkerImage = [UIImage imageNamed:@"marker-X.png"];
 42	NSLog(@"%@", xMarkerImage);
 43	markerPosition.latitude = center.latitude - ((kNumberRows - 1)/2.0 * kSpacing);
 44	int i, j;
 45	for (i = 0; i < kNumberRows; i++) {
 46		markerPosition.longitude = center.longitude - ((kNumberColumns - 1)/2.0 * kSpacing);
 47		for (j = 0; j < kNumberColumns; j++) {
 48			markerPosition.longitude += kSpacing;
 49			NSLog(@"%f %f", markerPosition.latitude, markerPosition.longitude);
 50			RMMarker *newMarker;
 51			if ((markerPosition.longitude < -180) ||
 52				(markerPosition.longitude > 0))
 53				newMarker = [[RMMarker alloc] initWithUIImage:redMarkerImage anchorPoint:CGPointMake(0.5, 1.0)];
 54			else
 55				newMarker = [[RMMarker alloc] initWithUIImage:blueMarkerImage anchorPoint:CGPointMake(0.5, 1.0)];
 56			[self.mapView.contents.markerManager addMarker:newMarker
 57			 AtLatLong:markerPosition];
 58			[newMarker changeLabelUsingText:[NSString stringWithFormat:@"%4.1f", markerPosition.longitude]];
 59			[newMarker release];
 60			RMMarker *xMarker = [[RMMarker alloc] initWithUIImage:xMarkerImage anchorPoint:CGPointMake(0.5, 0.5)];
 61			[self.mapView.contents.markerManager addMarker:xMarker AtLatLong:markerPosition];
 62			[xMarker release];
 63		}
 64		markerPosition.latitude += kSpacing;
 65	}
 66}
 67
 68// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
 69- (void)viewDidLoad {
 70    [super viewDidLoad];
 71    [mapView setDelegate:self];
 72	id myTilesource = [[[RMOpenStreetMapSource alloc] init] autorelease];
 73    
 74	// have to initialize the RMMapContents object explicitly if we want it to use a particular tilesource
 75	[[[RMMapContents alloc] initWithView:mapView 
 76							  tilesource:myTilesource] autorelease];
 77
 78	center.latitude = 66.44;
 79	center.longitude = -179.0;
 80
 81	[mapView moveToLatLong:center];
 82	[mapView.contents setZoom:6.0];
 83	[[mapView contents] moveBy:CGSizeMake(-5.0, 0.0)];
 84	[self updateInfo];
 85	[self performSelector:@selector(addMarkers) withObject:nil afterDelay:1.0];
 86}
 87
 88
 89/*
 90 // Override to allow orientations other than the default portrait orientation.
 91 - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
 92 // Return YES for supported orientations
 93 return (interfaceOrientation == UIInterfaceOrientationPortrait);
 94 }
 95*/
 96
 97
 98- (void)didReceiveMemoryWarning {
 99	RMLog(@"didReceiveMemoryWarning %@", self);
100    [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
101    // Release anything that's not essential, such as cached data
102}
103
104- (void)viewDidAppear:(BOOL)animated {
105    [self updateInfo];
106}
107
108- (void)dealloc {
109	LogMethod();
110    self.infoTextView = nil; 
111    self.mapView = nil; 
112    [super dealloc];
113}
114
115- (void)updateInfo {
116	RMMapContents *contents = self.mapView.contents;
117    CLLocationCoordinate2D mapCenter = [contents mapCenter];
118    
119    [infoTextView setText:[NSString stringWithFormat:@"Latitude : %f\nLongitude : %f\nZoom level : %.2f\n%@", 
120                           mapCenter.latitude, 
121                           mapCenter.longitude, 
122                           contents.zoom, 
123						   [[contents tileSource] shortAttribution]
124						   ]];
125}
126
127
128#pragma mark -
129#pragma mark Delegate methods
130
131- (void) afterMapMove: (RMMapView*) map {
132    [self updateInfo];
133}
134
135- (void) afterMapZoom: (RMMapView*) map byFactor: (float) zoomFactor near:(CGPoint) center {
136	[self updateInfo];
137}
138
139
140@end