Don't like ads? PRO users don't see any ads ;-)
Guest

Untitled

By: a guest on Jun 30th, 2012  |  syntax: None  |  size: 7.11 KB  |  hits: 13  |  expires: Never
download  |  raw  |  embed  |  report abuse  |  print
Text below is selected. Please press Ctrl+C to copy to your clipboard. (⌘+C on Mac)
  1. //
  2. //  GeoRealityViewController.m
  3. //  GeoReality
  4. //
  5. //  Created by Maurício Gomes on 16/03/11.
  6. //  Copyright 2011 __MyCompanyName__. All rights reserved.
  7. //
  8.  
  9. #import "GeoRealityViewController.h"
  10. #include "GeoMath.h"
  11.  
  12. @implementation ObjLocation
  13. {
  14.    
  15. }
  16.  
  17. @synthesize object;
  18. @synthesize latitude;
  19. @synthesize longitude;
  20. @synthesize altitude;
  21. @end
  22.  
  23. @implementation GeoRealityViewController
  24.  
  25. @dynamic ArraySize;
  26.  
  27. - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
  28. {
  29.     self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
  30.     if (self) {
  31.         // Custom initialization
  32.     }
  33.     return self;
  34. }
  35.  
  36. - (void)dealloc
  37. {
  38.     [super dealloc];
  39. }
  40.  
  41. - (void)didReceiveMemoryWarning
  42. {
  43.     // Releases the view if it doesn't have a superview.
  44.     [super didReceiveMemoryWarning];
  45.    
  46.     // Release any cached data, images, etc that aren't in use.
  47. }
  48.  
  49. #pragma mark - View lifecycle
  50.  
  51. - (void)viewDidAppear:(BOOL)animated
  52. {
  53. //    [super viewDidAppear animated:animated];
  54.     // Do any additional setup after loading the view from its nib.
  55.     if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
  56.         if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
  57.         {
  58.             imagePickerOverlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
  59.             [imagePickerOverlay setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
  60.            
  61.             UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  62.             [aButton addTarget:self
  63.                         action:@selector(aMethod:)
  64.               forControlEvents:UIControlEventTouchDown];
  65.             [aButton setTitle:@"View Show" forState:UIControlStateNormal];
  66.             aButton.frame = CGRectMake(80.0, 250.0, 160.0, 40.0);
  67.            
  68.             //[_vController setArraySize:10];
  69.             //[self addView:aButton atLatitude:0.0 atLongitude:0.0 andAltitude:0.0];
  70.            
  71.            
  72.             ImagePicker = [[UIImagePickerController alloc] init];
  73.             ImagePicker.delegate = self;
  74.             ImagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
  75.             ImagePicker.allowsEditing = NO;
  76.             ImagePicker.showsCameraControls = NO;
  77.             ImagePicker.cameraOverlayView = imagePickerOverlay;
  78.            
  79.            
  80.             [self presentModalViewController:ImagePicker animated:animated];
  81.            
  82.             locationManager = [[CLLocationManager alloc] init];
  83.             locationManager.delegate = self;
  84.             locationManager.desiredAccuracy = kCLLocationAccuracyBest;
  85.            
  86.             if([CLLocationManager headingAvailable])
  87.             {
  88.                 [locationManager startUpdatingHeading];
  89.             }
  90.                        
  91.             currentHeading = nil;
  92.            
  93.             lastTime = 0;
  94.             mach_timebase_info(&mach_info);
  95.         }
  96.         else
  97.         {
  98.             NSLog(@"Rear Camera Not Available");
  99.         }
  100.     else
  101.     {
  102.         NSLog(@"Cameras as Source Type not available");
  103.     }
  104.    
  105.    
  106.    
  107.     NSLog(@"GeoRealityViewController viewDidAppear");
  108. }
  109.  
  110. - (void)viewDidLoad
  111. {
  112.     [super viewDidLoad];
  113.    
  114.     GeoMath_Init(200);
  115.    
  116.     NSLog(@"GeoRealityViewController viewDidLoad");
  117. }
  118.  
  119. - (void)viewDidUnload
  120. {
  121.     [super viewDidUnload];
  122.    
  123.     GeoMath_Dealloc();
  124.     // Release any retained subviews of the main view.
  125.     // e.g. self.myOutlet = nil;
  126. }
  127.  
  128. - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
  129. {
  130.     // Return YES for supported orientations
  131.     return (interfaceOrientation == UIInterfaceOrientationPortrait);
  132. }
  133.  
  134. - (int)addView:(UIView*)view atLatitude:(double)aLatitude atLongitude:(double)aLongitude andAltitude:(double)aAltitude
  135. {
  136.     [ImagePicker.cameraOverlayView addSubview:view];
  137.     return GeoMath_AddObject((int)view, aLatitude, aLongitude, aAltitude);
  138. }
  139.  
  140. - (void)update
  141. {
  142.     //if(!additionsQueue) return;
  143.    
  144.     /* Get time elapsed */
  145.     uint64_t curTime = mach_absolute_time();
  146.     uint64_t deltaTime = curTime - lastTime;
  147.     lastTime = curTime;
  148.    
  149.     /* Convert to nanoseconds */
  150.     deltaTime *= mach_info.numer;
  151.     deltaTime /= mach_info.denom;
  152.    
  153.     if(deltaTime < 33333333) return; //don't waste cycles updating too fast
  154.    
  155.     int size = [additionsQueue count];
  156.     NSLog(@"%d", size);
  157.     int counter = 0;
  158.     while(counter < size)
  159.     {
  160.         UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  161.         [aButton addTarget:self
  162.                     action:@selector(aMethod:)
  163.           forControlEvents:UIControlEventTouchDown];
  164.         [aButton setTitle:@"Show View" forState:UIControlStateNormal];
  165.         aButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
  166.        
  167.        
  168.         ObjLocation* someItem = [additionsQueue objectAtIndex:counter];
  169.         [self addView:aButton atLatitude:someItem.latitude atLongitude:someItem.longitude andAltitude:someItem.altitude];
  170.         /*The line above don't work*/
  171.         ++counter;
  172.         //[someItem release];
  173.        
  174.     }
  175.    
  176.     if(counter > 0) [ImagePicker.cameraOverlayView setNeedsDisplay], [ImagePicker.cameraOverlayView setNeedsLayout];
  177.    
  178.     [additionsQueue removeAllObjects];
  179.    
  180.     UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
  181.     [aButton addTarget:self
  182.                 action:@selector(aMethod:)
  183.       forControlEvents:UIControlEventTouchDown];
  184.     [aButton setTitle:@"Show View" forState:UIControlStateNormal];
  185.     aButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
  186.     [self queueAddView:aButton atLatitude:0.0 atLongitude:0.0 andAltitude:0.0];
  187. }
  188.  
  189. - (void)setArraySize:(int)size
  190. {
  191.     GeoMath_Dealloc();
  192.     GeoMath_Init(size);
  193. }
  194.  
  195. - (int)getArraySize
  196. {
  197.     return GeoMath_getArraySize();
  198. }
  199.  
  200. - (void)queueAddView:(UIView*)view atLatitude:(double)aLatitude atLongitude:(double)aLongitude andAltitude:(double)aAltitude
  201. {
  202.     if(!additionsQueue) additionsQueue = [[NSMutableArray alloc] initWithCapacity:50];
  203.        
  204.     ObjLocation* aObject = [[ObjLocation alloc] init];
  205.     aObject.object = view;
  206.     aObject.latitude = aLatitude;
  207.     aObject.longitude = aLongitude;
  208.     aObject.altitude = aAltitude;
  209.    
  210.     [additionsQueue addObject:aObject];
  211.    
  212.     [aObject release];
  213. }
  214.  
  215. - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
  216. {
  217.     if(currentHeading == nil)
  218.         {
  219.                 if([CLLocationManager locationServicesEnabled])
  220.                 {
  221.                         [locationManager startUpdatingLocation];
  222.                 }
  223.         }
  224.        
  225.         currentHeading = [newHeading retain];
  226.    
  227.     [self update];
  228. }
  229.  
  230. - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
  231. {
  232.    
  233.         if (startingPoint == nil) startingPoint = [newLocation retain];
  234.         if (currentHeading == nil) return;
  235.    
  236.     [self update];
  237. }
  238.  
  239. - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
  240. {
  241.         return YES;
  242. }
  243.  
  244. @end