-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathViewController.m
281 lines (222 loc) · 9.84 KB
/
ViewController.m
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
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
//
// ViewController.m
// FlightNavigator
//
// Created by Yoo Rinjae on 12. 11. 9..
// Copyright (c) 2012년 DearMai. All rights reserved.
//
#import "ViewController.h"
#import "AirportListViewController.h"
#import "PointController.h"
#import "RouteController.h"
#import "AirportController.h"
#import "PointAnnotation.h"
#import "AirportAnnotation.h"
#import "SignificantPoint.h"
#import "Airport.h"
#import "FlightRoute.h"
#import "ShortestPath.h"
@interface ViewController () {
PointController *pointController;
RouteController *routeController;
AirportController *airportController;
}
@property (weak, nonatomic) IBOutlet UIBarButtonItem *btnDeparture;
@property (weak, nonatomic) IBOutlet MKMapView *map;
@end
@implementation ViewController
@synthesize labelAirport;
#pragma -
- (void)actionReloadAirportData {
[self reloadDisplayWithPath:NO];
}
-(void)reloadDisplayWithPath:(BOOL)isPath{
NSString *depature, *arrival, *str;
NSLog(@"ViewController#actionReloadAirportData");
NSLog(@"%@", labelAirport);
if(self.departureAirport == nil) depature = @"선택 않됨";
else depature = self.departureAirport.name;
if(self.arrivalAirport == nil) arrival = @"선택 않됨";
else arrival = self.arrivalAirport.name;
str = [NSString stringWithFormat:@"출발 공항 : %@, 도착 공항 : %@", depature, arrival];
NSLog(@"%@", self.labelAirport);
[self.labelAirport setText:str];
self.labelAirport.text = str;
// Display annocation
for (MKAnnotationView *anno in [self.map annotations]) {
[self.map removeAnnotation:(id)anno];
}
if(self.departureAirport != nil) {
AirportAnnotation *anno = [[AirportAnnotation alloc] init];
CLLocationCoordinate2D location;
location.latitude = self.departureAirport.latitude;
location.longitude = self.departureAirport.longitude;
anno.title = self.departureAirport.name;
anno.coordinate = location;
MKPinAnnotationView *pinAnno =
[[MKPinAnnotationView alloc] initWithAnnotation:anno
reuseIdentifier:self.departureAirport.name];
pinAnno.pinColor = MKPinAnnotationColorRed;
[self.map addAnnotation:(id<MKAnnotation>)pinAnno];
}
if(self.arrivalAirport != nil) {
AirportAnnotation *anno = [[AirportAnnotation alloc] init];
CLLocationCoordinate2D location;
location.latitude = self.arrivalAirport.latitude;
location.longitude = self.arrivalAirport.longitude;
anno.title = self.arrivalAirport.name;
anno.coordinate = location;
MKPinAnnotationView *pinAnno = [[MKPinAnnotationView alloc] initWithAnnotation:anno reuseIdentifier:self.arrivalAirport.name];
[self.map addAnnotation:(id<MKAnnotation>)pinAnno];
}
NSLog(@"%@", self.labelAirport.text);
// 경로 표시
for (MKOverlayView *view in [self.map overlays]) {
[self.map removeOverlay:(id)view];
}
if(isPath == YES){
dispatch_sync(dispatch_get_main_queue(), ^(void){
// 전체 경로 표시
NSArray *routes = [routeController routes];
for(FlightRoute *route in routes){
NSUInteger routeCount = [route.pointNameArray count];
CLLocationCoordinate2D coords[routeCount];
for(int i = 0; i < routeCount; i++){
NSString *name = [route.pointNameArray objectAtIndex:i];
SignificantPoint *point = [pointController pointObjectAtName:name];
coords[i].latitude = point.latitude;
coords[i].longitude = point.longitude;
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coords count:routeCount];
polyLine.title = @"FlightRoute";
[self.map addOverlay:polyLine];
}
// 검색된 경로 표시
ShortestPath *instance = [[ShortestPath alloc] initWithPoints:pointController.points
andRoutes:routeController.routes
andAirports:airportController.airports];
NSArray *route = [instance main:self.departureAirport andArrivalAirport:self.arrivalAirport];
NSUInteger routeCount = [route count] + 3;
CLLocationCoordinate2D coords[routeCount];
coords[0].latitude = self.arrivalAirport.latitude;
coords[0].longitude = self.arrivalAirport.longitude;
SignificantPoint *nearestArrivalAirport = [instance getNearestPointWithAirport:self.arrivalAirport];
coords[1].latitude = nearestArrivalAirport.latitude;
coords[1].longitude = nearestArrivalAirport.longitude;
for(int i = 0; i < routeCount - 3; i++){
SignificantPoint *point = [route objectAtIndex:i];
coords[i + 2].latitude = point.latitude;
coords[i + 2].longitude = point.longitude;
}
coords[routeCount - 1].latitude = self.departureAirport.latitude;
coords[routeCount - 1].longitude = self.departureAirport.longitude;
NSLog(@"경로 표시");
for(int i = 1; i < routeCount; i++){
NSLog(@"%f - %f", coords[i].latitude, coords[i].longitude);
}
MKPolyline *polyLine = [MKPolyline polylineWithCoordinates:coords count:routeCount];
polyLine.title = @"asd";
[self.map addOverlay:polyLine];
});
}
}
#pragma -
- (IBAction)actionSelectAirport:(UIBarButtonItem *)sender {
AirportListViewController *vc = [self.storyboard instantiateViewControllerWithIdentifier:@"SCENE_AIRPORT"];
vc.modalPresentationStyle = UIModalPresentationFormSheet;
vc.airportType = sender.tag;
[self presentViewController:vc animated:YES completion:nil];
//[self performSegueWithIdentifier:@"AIRPORT" sender:self];
//self.view.alpha = 0.5;
}
- (IBAction)actionCalcPath:(UIBarButtonItem *)sender {
if(self.departureAirport == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"알림"
message:@"출발 공항이 선택 되지 않았습니다."
delegate:nil
cancelButtonTitle:@"확인"
otherButtonTitles:nil];
[alert show];
return;
}
if(self.arrivalAirport == nil) {
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"알림"
message:@"도착 공항이 선택 되지 않았습니다."
delegate:nil
cancelButtonTitle:@"확인"
otherButtonTitles:nil];
[alert show];
return;
}
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:nil
message:@"경로 탐색 중..."
delegate:self
cancelButtonTitle:nil
otherButtonTitles:nil];
UIActivityIndicatorView *activityIndicatorView = [[UIActivityIndicatorView alloc] initWithActivityIndicatorStyle:UIActivityIndicatorViewStyleWhiteLarge];
[activityIndicatorView startAnimating];
activityIndicatorView.frame = CGRectMake(125, 55, 30, 30);
[alert addSubview:activityIndicatorView];
[alert show];
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0), ^(void){
[self reloadDisplayWithPath:YES];
dispatch_sync(dispatch_get_main_queue(), ^(void){
[alert dismissWithClickedButtonIndex:0 animated:YES];
});
});
}
#pragma Delegate
-(MKOverlayView *)mapView:(MKMapView *)mapView viewForOverlay:(id<MKOverlay>)overlay {
NSLog(@"이건 뭐지~~~~~~");
MKPolylineView *polyLineView = [[MKPolylineView alloc] initWithOverlay: overlay];
if([[overlay title] isEqualToString:@"FlightRoute"] == YES) {
polyLineView.strokeColor = [UIColor blueColor];
polyLineView.lineWidth = 1.0;
} else {
polyLineView.strokeColor = [UIColor redColor];
polyLineView.lineWidth = 5.0;
}
return polyLineView;
}
#pragma -
- (void)viewDidLoad
{
[super viewDidLoad];
pointController = [PointController getInstance];
airportController = [AirportController getInstance];
routeController = [RouteController getInstance];
//
//
// for(NSUInteger i = 0; i < [pointController countPoints]; i++){
// SignificantPoint *point = [pointController pointObjectAtIndex:i];
// CLLocationCoordinate2D location;
// location.latitude = point.latitude;
// location.longitude = point.longitude;
// PointAnnotation *pointAnno = [[PointAnnotation alloc] initWithTitle:point.name
// andLocation:location];
//
// [self.map addAnnotation:pointAnno];
// //[self.map viewForAnnotation:pointAnno];
// }
//
MKCoordinateRegion region;
MKCoordinateSpan span;
double ratio = 5;
span.latitudeDelta = ratio;
span.longitudeDelta = ratio;
CLLocationCoordinate2D location;
location.latitude = 35.395785;
location.longitude = 127.934143;
region.center = location;
region.span = span;
[self.map setRegion:region];
[self.map setCenterCoordinate:region.center animated:YES];
[self.map regionThatFits:region];
[self.map setDelegate:self];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end