-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathARViewController.m
More file actions
180 lines (134 loc) · 5.63 KB
/
ARViewController.m
File metadata and controls
180 lines (134 loc) · 5.63 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
//
// ARView.m
//
// Created by Massimo Donati on 30/11/10.
// Copyright 2010 __MyCompanyName__. All rights reserved.
//
#import "ARViewController.h"
#import "TestARAppViewController.h"
@implementation ARViewController
@synthesize overlayView;
@synthesize marker;
@synthesize toMap;
@synthesize xValue;
@synthesize infoMarker;
@synthesize picker;
@synthesize locationManager;
@synthesize rootController;
@synthesize userLocation;
@synthesize markerLocation;
@synthesize angle;
-(void) mapView:(id)sender {
[self.picker dismissModalViewControllerAnimated:YES];
[self.rootController dismissModalViewControllerAnimated:YES];
}
- (void)viewDidLoad {
[super viewDidLoad];
}
-(void)viewDidAppear:(BOOL)animated{
self.picker = [[[UIImagePickerController alloc] init] autorelease];
self.picker.delegate=self;
self.picker.sourceType = UIImagePickerControllerSourceTypeCamera;
self.picker.showsCameraControls = NO;
self.picker.navigationBarHidden = YES;
self.picker.wantsFullScreenLayout = YES;
self.picker.cameraViewTransform = CGAffineTransformScale(self.picker.cameraViewTransform, CAMERA_TRANSFORM_X, CAMERA_TRANSFORM_Y);
self.picker.cameraOverlayView = self.overlayView;
[self presentModalViewController:self.picker animated:NO];
[self.picker release];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter=1;//l'aggiornamento della posizione avverrà di metro in metro
[self.locationManager startUpdatingHeading];
[self.locationManager startUpdatingLocation];
[self calculatePosition:self.locationManager.heading.trueHeading];
self.infoMarker.imageView.center=self.marker.center;
[super viewDidAppear:YES];
}
-(void)infoButtonPushed:(id)sender{
double meters=[userLocation distanceFromLocation:markerLocation];
NSString *coordmark=[[NSString alloc] initWithFormat:@"(%g°;%g°)",markerLocation.coordinate.longitude,markerLocation.coordinate.latitude];
NSString *coordpos=[[NSString alloc] initWithFormat:@"(%g°;%g°)",userLocation.coordinate.longitude,userLocation.coordinate.latitude];
NSString *message =[[NSString alloc] initWithFormat: @"Il marker è visualizzato esattamente nella posizione geografica che tu hai scielto con il mirino, le sue coordinate sono \n %@ e dista %.2f metri dalla tua posizione che ha le seguenti coordinate \n %@.",coordmark,meters,coordpos];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Marker Information" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[alert show];
[coordmark release];
[coordpos release];
[message release];
[alert release];
}
-(void)calculatePosition:(CLLocationDirection) heading
{
// Posizione del marker
float markerPosValue = [self angleBetweenCoordinate1:markerLocation.coordinate AndCoordinate2:userLocation.coordinate];
[self.xValue setText:[NSString stringWithFormat:@"%.1f", markerPosValue]];
// Valore dell'heading, reale
float simHeadingValue = heading;
// Posizione del marker rispetto l'heading
float markerOffset= markerPosValue - simHeadingValue;
// Segno dell'offset
float offsetSign=(markerOffset<0)?-1:1;
// Valore assoluto dell'offset
float offsetABS = markerOffset * offsetSign;
// Gestione del caso in cui heading e marker sono in due emisferi diversi
if(offsetABS>180) { offsetABS = 360 - offsetABS; offsetSign *= -1; }
// Se il marker è nel cono visivo
if(offsetABS<40)
{
float markerScreenPos = 136 + offsetABS*offsetSign*9;
[self.marker setFrame:CGRectMake(markerScreenPos, 146, marker.frame.size.width, marker.frame.size.height)];
self.infoMarker.frame = CGRectMake(markerScreenPos, 146, self.infoMarker.frame.size.width, self.infoMarker.frame.size.height);
self.marker.hidden=NO;
self.infoMarker.hidden = NO;
}
else
{
self.marker.hidden=YES;
}
}
// calcola l'angolo tra me e il marker utilizzando le coordinate geografiche
-(double) angleBetweenCoordinate1:(CLLocationCoordinate2D)mark
AndCoordinate2:(CLLocationCoordinate2D)user{
double differenceLongitude = mark.longitude - user.longitude;
double y = sin(-differenceLongitude) * cos(mark.latitude);
double x = cos(user.latitude) * sin(mark.latitude) - sin(user.latitude) * cos(mark.latitude) * cos(-differenceLongitude);
double bearing = atan2(y, x);
bearing = fmodf(bearing * 180.0 / M_PI + 360.0, 360.0);
return bearing;
}
-(void) viewDidDisappear:(BOOL)animated{
self.picker =nil;
[super viewDidDisappear:YES];
}
// This delegate method is invoked when the location manager has heading data.
- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)heading {
//calcolo il nuovo centro del il marker
[self calculatePosition:self.locationManager.heading.trueHeading];
}
//metodo del delegato per gestire l'evento scaturito da un aggiornamento della posizione dell'utente
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
self.userLocation = newLocation;
[self calculatePosition:self.locationManager.heading.trueHeading];
}
- (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.
}
- (void)viewDidUnload {
// Release any retained subviews of the main view.
// e.g. self.myOutlet = nil;
self.locationManager=nil;
self.picker = nil;
}
- (void)dealloc {
[overlayView release];
[marker release];
[toMap release];
[picker release];
[locationManager stopUpdatingLocation];
[locationManager stopUpdatingHeading];
[locationManager release];
[super dealloc];
}
@end