- //
- // GeoRealityViewController.m
- // GeoReality
- //
- // Created by Maurício Gomes on 16/03/11.
- // Copyright 2011 __MyCompanyName__. All rights reserved.
- //
- #import "GeoRealityViewController.h"
- #include "GeoMath.h"
- @implementation ObjLocation
- {
- }
- @synthesize object;
- @synthesize latitude;
- @synthesize longitude;
- @synthesize altitude;
- @end
- @implementation GeoRealityViewController
- @dynamic ArraySize;
- - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
- {
- self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
- if (self) {
- // Custom initialization
- }
- return self;
- }
- - (void)dealloc
- {
- [super dealloc];
- }
- - (void)didReceiveMemoryWarning
- {
- // Releases the view if it doesn't have a superview.
- [super didReceiveMemoryWarning];
- // Release any cached data, images, etc that aren't in use.
- }
- #pragma mark - View lifecycle
- - (void)viewDidAppear:(BOOL)animated
- {
- // [super viewDidAppear animated:animated];
- // Do any additional setup after loading the view from its nib.
- if([UIImagePickerController isSourceTypeAvailable:UIImagePickerControllerSourceTypeCamera])
- if([UIImagePickerController isCameraDeviceAvailable:UIImagePickerControllerCameraDeviceRear])
- {
- imagePickerOverlay = [[UIView alloc] initWithFrame:[UIScreen mainScreen].applicationFrame];
- [imagePickerOverlay setAutoresizingMask:UIViewAutoresizingFlexibleHeight|UIViewAutoresizingFlexibleWidth];
- UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [aButton addTarget:self
- action:@selector(aMethod:)
- forControlEvents:UIControlEventTouchDown];
- [aButton setTitle:@"View Show" forState:UIControlStateNormal];
- aButton.frame = CGRectMake(80.0, 250.0, 160.0, 40.0);
- //[_vController setArraySize:10];
- //[self addView:aButton atLatitude:0.0 atLongitude:0.0 andAltitude:0.0];
- ImagePicker = [[UIImagePickerController alloc] init];
- ImagePicker.delegate = self;
- ImagePicker.sourceType = UIImagePickerControllerSourceTypeCamera;
- ImagePicker.allowsEditing = NO;
- ImagePicker.showsCameraControls = NO;
- ImagePicker.cameraOverlayView = imagePickerOverlay;
- [self presentModalViewController:ImagePicker animated:animated];
- locationManager = [[CLLocationManager alloc] init];
- locationManager.delegate = self;
- locationManager.desiredAccuracy = kCLLocationAccuracyBest;
- if([CLLocationManager headingAvailable])
- {
- [locationManager startUpdatingHeading];
- }
- currentHeading = nil;
- lastTime = 0;
- mach_timebase_info(&mach_info);
- }
- else
- {
- NSLog(@"Rear Camera Not Available");
- }
- else
- {
- NSLog(@"Cameras as Source Type not available");
- }
- NSLog(@"GeoRealityViewController viewDidAppear");
- }
- - (void)viewDidLoad
- {
- [super viewDidLoad];
- GeoMath_Init(200);
- NSLog(@"GeoRealityViewController viewDidLoad");
- }
- - (void)viewDidUnload
- {
- [super viewDidUnload];
- GeoMath_Dealloc();
- // Release any retained subviews of the main view.
- // e.g. self.myOutlet = nil;
- }
- - (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
- {
- // Return YES for supported orientations
- return (interfaceOrientation == UIInterfaceOrientationPortrait);
- }
- - (int)addView:(UIView*)view atLatitude:(double)aLatitude atLongitude:(double)aLongitude andAltitude:(double)aAltitude
- {
- [ImagePicker.cameraOverlayView addSubview:view];
- return GeoMath_AddObject((int)view, aLatitude, aLongitude, aAltitude);
- }
- - (void)update
- {
- //if(!additionsQueue) return;
- /* Get time elapsed */
- uint64_t curTime = mach_absolute_time();
- uint64_t deltaTime = curTime - lastTime;
- lastTime = curTime;
- /* Convert to nanoseconds */
- deltaTime *= mach_info.numer;
- deltaTime /= mach_info.denom;
- if(deltaTime < 33333333) return; //don't waste cycles updating too fast
- int size = [additionsQueue count];
- NSLog(@"%d", size);
- int counter = 0;
- while(counter < size)
- {
- UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [aButton addTarget:self
- action:@selector(aMethod:)
- forControlEvents:UIControlEventTouchDown];
- [aButton setTitle:@"Show View" forState:UIControlStateNormal];
- aButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
- ObjLocation* someItem = [additionsQueue objectAtIndex:counter];
- [self addView:aButton atLatitude:someItem.latitude atLongitude:someItem.longitude andAltitude:someItem.altitude];
- /*The line above don't work*/
- ++counter;
- //[someItem release];
- }
- if(counter > 0) [ImagePicker.cameraOverlayView setNeedsDisplay], [ImagePicker.cameraOverlayView setNeedsLayout];
- [additionsQueue removeAllObjects];
- UIButton* aButton = [UIButton buttonWithType:UIButtonTypeRoundedRect];
- [aButton addTarget:self
- action:@selector(aMethod:)
- forControlEvents:UIControlEventTouchDown];
- [aButton setTitle:@"Show View" forState:UIControlStateNormal];
- aButton.frame = CGRectMake(80.0, 210.0, 160.0, 40.0);
- [self queueAddView:aButton atLatitude:0.0 atLongitude:0.0 andAltitude:0.0];
- }
- - (void)setArraySize:(int)size
- {
- GeoMath_Dealloc();
- GeoMath_Init(size);
- }
- - (int)getArraySize
- {
- return GeoMath_getArraySize();
- }
- - (void)queueAddView:(UIView*)view atLatitude:(double)aLatitude atLongitude:(double)aLongitude andAltitude:(double)aAltitude
- {
- if(!additionsQueue) additionsQueue = [[NSMutableArray alloc] initWithCapacity:50];
- ObjLocation* aObject = [[ObjLocation alloc] init];
- aObject.object = view;
- aObject.latitude = aLatitude;
- aObject.longitude = aLongitude;
- aObject.altitude = aAltitude;
- [additionsQueue addObject:aObject];
- [aObject release];
- }
- - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
- {
- if(currentHeading == nil)
- {
- if([CLLocationManager locationServicesEnabled])
- {
- [locationManager startUpdatingLocation];
- }
- }
- currentHeading = [newHeading retain];
- [self update];
- }
- - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
- {
- if (startingPoint == nil) startingPoint = [newLocation retain];
- if (currentHeading == nil) return;
- [self update];
- }
- - (BOOL)locationManagerShouldDisplayHeadingCalibration:(CLLocationManager *)manager
- {
- return YES;
- }
- @end