/samples/SimpleMap/Classes/MapViewViewController.m

http://github.com/route-me/route-me · Objective C · 186 lines · 104 code · 36 blank · 46 comment · 2 complexity · 03ff4de3e3db3c459b6c86c2dc36d8bc MD5 · raw file

  1. //
  2. // MapViewViewController.m
  3. //
  4. // Copyright (c) 2008, Route-Me Contributors
  5. // All rights reserved.
  6. //
  7. // Redistribution and use in source and binary forms, with or without
  8. // modification, are permitted provided that the following conditions are met:
  9. //
  10. // * Redistributions of source code must retain the above copyright notice, this
  11. // list of conditions and the following disclaimer.
  12. // * Redistributions in binary form must reproduce the above copyright notice,
  13. // this list of conditions and the following disclaimer in the documentation
  14. // and/or other materials provided with the distribution.
  15. //
  16. // THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS "AS IS"
  17. // AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT LIMITED TO, THE
  18. // IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS FOR A PARTICULAR PURPOSE
  19. // ARE DISCLAIMED. IN NO EVENT SHALL THE COPYRIGHT OWNER OR CONTRIBUTORS BE
  20. // LIABLE FOR ANY DIRECT, INDIRECT, INCIDENTAL, SPECIAL, EXEMPLARY, OR
  21. // CONSEQUENTIAL DAMAGES (INCLUDING, BUT NOT LIMITED TO, PROCUREMENT OF
  22. // SUBSTITUTE GOODS OR SERVICES; LOSS OF USE, DATA, OR PROFITS; OR BUSINESS
  23. // INTERRUPTION) HOWEVER CAUSED AND ON ANY THEORY OF LIABILITY, WHETHER IN
  24. // CONTRACT, STRICT LIABILITY, OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE)
  25. // ARISING IN ANY WAY OUT OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE
  26. // POSSIBILITY OF SUCH DAMAGE.
  27. #import "MapViewViewController.h"
  28. #import "RMMapContents.h"
  29. #import "RMFoundation.h"
  30. #import "RMMarker.h"
  31. #import "RMMarkerManager.h"
  32. @implementation MapViewViewController
  33. @synthesize mapView;
  34. /*
  35. // Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
  36. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
  37. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
  38. // Custom initialization
  39. }
  40. return self;
  41. }
  42. */
  43. /*
  44. // Implement loadView to create a view hierarchy programmatically.
  45. - (void)loadView {
  46. }
  47. */
  48. - (void)testMarkers
  49. {
  50. RMMarkerManager *markerManager = [mapView markerManager];
  51. NSArray *markers = [markerManager markers];
  52. NSLog(@"Nb markers %d", [markers count]);
  53. NSEnumerator *markerEnumerator = [markers objectEnumerator];
  54. RMMarker *aMarker;
  55. while (aMarker = (RMMarker *)[markerEnumerator nextObject])
  56. {
  57. RMProjectedPoint point = [aMarker projectedLocation];
  58. NSLog(@"Marker projected location: east:%lf, north:%lf", point.easting, point.northing);
  59. CGPoint screenPoint = [markerManager screenCoordinatesForMarker: aMarker];
  60. NSLog(@"Marker screen location: X:%lf, Y:%lf", screenPoint.x, screenPoint.y);
  61. CLLocationCoordinate2D coordinates = [markerManager latitudeLongitudeForMarker: aMarker];
  62. NSLog(@"Marker Lat/Lon location: Lat:%lf, Lon:%lf", coordinates.latitude, coordinates.longitude);
  63. [markerManager removeMarker:aMarker];
  64. }
  65. // Put the marker back
  66. RMMarker *marker = [[RMMarker alloc]initWithUIImage:[UIImage imageNamed:@"marker-blue.png"]
  67. anchorPoint:CGPointMake(0.5, 1.0)];
  68. [marker changeLabelUsingText:@"Hello"];
  69. [markerManager addMarker:marker AtLatLong:[[mapView contents] mapCenter]];
  70. [marker release];
  71. markers = [markerManager markersWithinScreenBounds];
  72. NSLog(@"Nb Markers in Screen: %d", [markers count]);
  73. // [mapView getScreenCoordinateBounds];
  74. [markerManager hideAllMarkers];
  75. [markerManager unhideAllMarkers];
  76. }
  77. - (BOOL)mapView:(RMMapView *)map shouldDragMarker:(RMMarker *)marker withEvent:(UIEvent *)event
  78. {
  79. //If you do not implement this function, then all drags on markers will be sent to the didDragMarker function.
  80. //If you always return YES you will get the same result
  81. //If you always return NO you will never get a call to the didDragMarker function
  82. return YES;
  83. }
  84. - (void)mapView:(RMMapView *)map didDragMarker:(RMMarker *)marker withEvent:(UIEvent *)event
  85. {
  86. CGPoint position = [[[event allTouches] anyObject] locationInView:mapView];
  87. RMMarkerManager *markerManager = [mapView markerManager];
  88. NSLog(@"New location: east:%lf north:%lf", [marker projectedLocation].easting, [marker projectedLocation].northing);
  89. CGRect rect = [marker bounds];
  90. [markerManager moveMarker:marker AtXY:CGPointMake(position.x,position.y +rect.size.height/3)];
  91. }
  92. - (void) singleTapOnMap: (RMMapView*) map At: (CGPoint) point
  93. {
  94. NSLog(@"Clicked on Map - New location: X:%lf Y:%lf", point.x, point.y);
  95. }
  96. - (void) tapOnMarker: (RMMarker*) marker onMap: (RMMapView*) map
  97. {
  98. NSLog(@"MARKER TAPPED!");
  99. RMMarkerManager *markerManager = [mapView markerManager];
  100. if(!tap)
  101. {
  102. [marker replaceUIImage:[UIImage imageNamed:@"marker-red.png"]];
  103. [marker changeLabelUsingText:@"World"];
  104. tap=YES;
  105. [markerManager moveMarker:marker AtXY:CGPointMake([marker position].x,[marker position].y + 20.0)];
  106. [mapView setDeceleration:YES];
  107. }else
  108. {
  109. [marker replaceUIImage:[UIImage imageNamed:@"marker-blue.png"]
  110. anchorPoint:CGPointMake(0.5, 1.0)];
  111. [marker changeLabelUsingText:@"Hello"];
  112. [markerManager moveMarker:marker AtXY:CGPointMake([marker position].x,[marker position].y - 20.0)];
  113. tap=NO;
  114. [mapView setDeceleration:NO];
  115. }
  116. }
  117. - (void) tapOnLabelForMarker:(RMMarker*) marker onMap:(RMMapView*) map
  118. {
  119. NSLog(@"Label <0x%x, RC:%U> tapped for marker <0x%x, RC:%U>", marker.label, [marker.label retainCount], marker, [marker retainCount]);
  120. [marker changeLabelUsingText:[NSString stringWithFormat:@"Tapped! (%U)", ++tapCount]];
  121. }
  122. // Implement viewDidLoad to do additional setup after loading the view.
  123. - (void)viewDidLoad {
  124. NSLog(@"%@ viewDidLoad", self);
  125. [super viewDidLoad];
  126. tap=NO;
  127. RMMarkerManager *markerManager = [mapView markerManager];
  128. [mapView setDelegate:self];
  129. CLLocationCoordinate2D coolPlace;
  130. coolPlace.latitude = -33.9464;
  131. coolPlace.longitude = 151.2381;
  132. RMMarker *marker = [[RMMarker alloc]initWithUIImage:[UIImage imageNamed:@"marker-blue.png"]
  133. anchorPoint:CGPointMake(0.5, 1.0)];
  134. [marker setTextForegroundColor:[UIColor blueColor]];
  135. [marker changeLabelUsingText:@"Hello"];
  136. [markerManager addMarker:marker AtLatLong:[[mapView contents] mapCenter]];
  137. [marker release];
  138. NSLog(@"Center: Lat: %lf Lon: %lf", mapView.contents.mapCenter.latitude, mapView.contents.mapCenter.longitude);
  139. }
  140. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
  141. return YES;
  142. }
  143. - (void)didReceiveMemoryWarning {
  144. [super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  145. }
  146. - (void)dealloc {
  147. [mapView release];
  148. [super dealloc];
  149. }
  150. @end