PageRenderTime 31ms CodeModel.GetById 16ms RepoModel.GetById 0ms app.codeStats 0ms

/samples/SimpleSampleMap/Classes/MapViewViewController.m

http://github.com/route-me/route-me
Objective C | 264 lines | 173 code | 52 blank | 39 comment | 11 complexity | 07d3e11c89b3a1d4eae8cdcb7e0a4bc5 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. @synthesize locationManager;
  35. @synthesize currentLocation;
  36. // Override initWithNibName:bundle: to load the view using a nib file then perform additional customization that is not appropriate for viewDidLoad.
  37. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  38. {
  39. if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil])
  40. {
  41. // Custom initialization
  42. }
  43. return self;
  44. }
  45. - (void)testMarkers
  46. {
  47. RMMarkerManager *markerManager = [mapView markerManager];
  48. NSArray *markers = [markerManager markers];
  49. NSLog(@"Nb markers %d", [markers count]);
  50. NSEnumerator *markerEnumerator = [markers objectEnumerator];
  51. RMMarker *aMarker;
  52. while (aMarker = (RMMarker *)[markerEnumerator nextObject])
  53. {
  54. // RMXYPoint point = [aMarker location];
  55. // CGPoint screenPoint = [markerManager getMarkerScreenCoordinate: aMarker];
  56. // CLLocationCoordinate2D coordinates = [markerManager getMarkerCoordinate2D: aMarker];
  57. [markerManager removeMarker:aMarker];
  58. }
  59. // Put the marker back
  60. RMMarker *marker =
  61. [[RMMarker alloc] initWithUIImage: [UIImage imageNamed:@"marker-blue.png"]];
  62. [marker changeLabelUsingText:@"Hello"];
  63. [markerManager addMarker:marker
  64. AtLatLong:[[mapView contents] mapCenter]];
  65. [marker release];
  66. markers = [markerManager markersWithinScreenBounds];
  67. NSLog(@"Nb Markers in Screen: %d", [markers count]);
  68. // [mapView getScreenCoordinateBounds];
  69. [markerManager hideAllMarkers];
  70. [markerManager unhideAllMarkers];
  71. }
  72. - (BOOL)mapView:(RMMapView *)map
  73. shouldDragMarker:(RMMarker *)marker
  74. withEvent:(UIEvent *)event
  75. {
  76. //If you do not implement this function, then all drags on markers will be sent to the didDragMarker function.
  77. //If you always return YES you will get the same result
  78. //If you always return NO you will never get a call to the didDragMarker function
  79. return YES;
  80. }
  81. - (void)mapView:(RMMapView *)map
  82. didDragMarker:(RMMarker *)marker
  83. withEvent:(UIEvent *)event
  84. {
  85. CGPoint position = [[[event allTouches] anyObject] locationInView:mapView];
  86. RMMarkerManager *markerManager = [mapView markerManager];
  87. NSLog(@"New location: east:%lf north:%lf", [marker projectedLocation].easting, [marker projectedLocation].northing);
  88. CGRect rect = [marker bounds];
  89. [markerManager moveMarker:marker
  90. AtXY:CGPointMake(position.x,position.y +rect.size.height/3)];
  91. }
  92. - (void) singleTapOnMap: (RMMapView*) map
  93. At: (CGPoint) point
  94. {
  95. NSLog(@"Clicked on Map - New location: X:%lf Y:%lf", point.x, point.y);
  96. }
  97. - (void) tapOnMarker: (RMMarker*) marker
  98. onMap: (RMMapView*) map
  99. {
  100. NSLog(@"MARKER TAPPED!");
  101. RMMarkerManager *markerManager = [mapView markerManager];
  102. if (!tap)
  103. {
  104. [marker replaceUIImage:[UIImage imageNamed:@"marker-red.png"]
  105. anchorPoint:CGPointMake(0.5, 1.0)];
  106. [marker changeLabelUsingText:@"World"];
  107. tap=YES;
  108. [markerManager moveMarker:marker
  109. AtXY:CGPointMake([marker position].x,[marker position].y + 20.0)];
  110. [mapView setDeceleration:YES];
  111. }
  112. else
  113. {
  114. [marker replaceUIImage:[UIImage imageNamed:@"marker-blue.png"]
  115. anchorPoint:CGPointMake(0.5, 1.0)];
  116. [marker changeLabelUsingText:@"Hello"];
  117. [markerManager moveMarker:marker
  118. AtXY:CGPointMake([marker position].x, [marker position].y - 20.0)];
  119. tap = NO;
  120. [mapView setDeceleration:NO];
  121. }
  122. }
  123. - (void) tapOnLabelForMarker:(RMMarker*) marker
  124. onMap:(RMMapView*) map
  125. {
  126. NSLog(@"Label <0x%x, RC:%U> tapped for marker <0x%x, RC:%U>", marker.label, [marker.label retainCount], marker, [marker retainCount]);
  127. [marker changeLabelUsingText:[NSString stringWithFormat:@"Tapped! (%U)", ++tapCount]];
  128. }
  129. // Implement viewDidLoad to do additional setup after loading the view.
  130. - (void)viewDidLoad
  131. {
  132. [super viewDidLoad];
  133. locationManager = [[CLLocationManager alloc] init];
  134. locationManager.delegate = self;
  135. locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  136. if (locationManager.locationServicesEnabled == NO)
  137. {
  138. NSLog(@"Services not enabled");
  139. return;
  140. }
  141. currentLocation. latitude = 33.413313;
  142. currentLocation.longitude = -111.907326;
  143. [locationManager startUpdatingLocation];
  144. tap = NO;
  145. RMMarkerManager *markerManager = [mapView markerManager];
  146. [mapView setDelegate:self];
  147. [mapView setBackgroundColor:[UIColor grayColor]]; //or clear etc
  148. if (locationManager.location != nil)
  149. {
  150. currentLocation = locationManager.location.coordinate;
  151. NSLog(@"Location: Lat: %lf Lon: %lf", currentLocation.latitude, currentLocation.longitude);
  152. }
  153. [mapView moveToLatLong:currentLocation];
  154. [self.view addSubview:mapView];
  155. RMMarker *marker = [[RMMarker alloc] initWithUIImage: [UIImage imageNamed:@"marker-blue.png"]];
  156. [marker setTextForegroundColor:[UIColor blueColor]];
  157. [marker changeLabelUsingText:@"Hello"];
  158. [markerManager addMarker:marker
  159. AtLatLong:currentLocation];
  160. [marker release];
  161. }
  162. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  163. {
  164. return YES;
  165. }
  166. - (void)didReceiveMemoryWarning
  167. {
  168. // due to a bug, RMMapView should never be released, as it causes the application to crash
  169. //[super didReceiveMemoryWarning]; // Releases the view if it doesn't have a superview
  170. [mapView.contents didReceiveMemoryWarning];
  171. }
  172. - (void)dealloc
  173. {
  174. [mapView release];
  175. [locationManager stopUpdatingLocation];
  176. [locationManager release];
  177. [super dealloc];
  178. }
  179. #pragma mark --
  180. #pragma mark locationManagerDelegate Methods
  181. - (void)locationManager: (CLLocationManager *)manager
  182. didUpdateToLocation: (CLLocation *)newLocation
  183. fromLocation: (CLLocation *)oldLocation
  184. {
  185. NSLog(@"Moving from lat: %lf lon: %lf to lat: %lf lon: %lf",
  186. oldLocation.coordinate.latitude, oldLocation.coordinate.longitude,
  187. newLocation.coordinate.latitude, newLocation.coordinate.longitude);
  188. currentLocation = newLocation.coordinate;
  189. RMMarkerManager *markerManager = [mapView markerManager];
  190. NSArray *markers = [markerManager markers];
  191. for (NSInteger i = 0; i < [markers count]; ++i)
  192. {
  193. RMMarker *marker = [markers objectAtIndex: i];
  194. CLLocationCoordinate2D location = [markerManager latitudeLongitudeForMarker:marker];
  195. if (location.latitude == oldLocation.coordinate.latitude &&
  196. location.longitude == oldLocation.coordinate.longitude)
  197. {
  198. [markerManager moveMarker: marker
  199. AtLatLon: newLocation.coordinate];
  200. break; // We're done.
  201. }
  202. }
  203. [mapView moveToLatLong:currentLocation];
  204. }
  205. - (void)locationManager: (CLLocationManager *)manager
  206. didFailWithError: (NSError *)error
  207. {
  208. NSLog(@"Location Manager error: %@", [error localizedDescription]);
  209. }
  210. @synthesize tap;
  211. @synthesize tapCount;
  212. @end