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