-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathFind_ViewController.m
142 lines (106 loc) · 4.81 KB
/
Find_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
//
// Find_ViewController.m
// Beacon_new
//
// Created by Aaron Crawfis on 10/5/13.
// Copyright (c) 2013 Aaron Crawfis. All rights reserved.
//
#import "Find_ViewController.h"
#import <Parse/Parse.h>
#import "Map_ViewController.h"
#import "MapViewAnnotation.h"
#define METERS_PER_MILE 1609.344
@interface Find_ViewController ()
@end
@interface AnnotationDelegate : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString * title;
NSString * subtitle;
}
@end
@implementation Find_ViewController
- (void)viewWillAppear:(BOOL)animated {
[PFGeoPoint geoPointForCurrentLocationInBackground:^(PFGeoPoint *geoPoint, NSError *error) {
if (!error) {
// do something with the new geoPoint
// 1
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = geoPoint.latitude;
zoomLocation.longitude= geoPoint.longitude;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
[_mapView setRegion:viewRegion animated:YES];
}
}];
}
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
}
return self;
}
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
- (IBAction)locateBeacon:(id)sender {
UIAlertView * alert = [[UIAlertView alloc] initWithTitle:@"Locate Beacon" message:@"What is Your Friend's Beacon?" delegate:self cancelButtonTitle:@"Find Friends!" otherButtonTitles:nil];
alert.alertViewStyle = UIAlertViewStylePlainTextInput;
[alert show];
}
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
NSString *beaconName =[[alertView textFieldAtIndex:0] text];
NSLog(@"Entered: %@",beaconName);
PFQuery *query = [PFQuery queryWithClassName:@"Beacon"];
[query whereKey:@"beaconName" equalTo:beaconName];
[query findObjectsInBackgroundWithBlock:^(NSArray *objects, NSError *error) {
if (!error) {
// The find succeeded.
for (PFObject *object in objects) {
//////////////////////////////////////////////////////////////////
NSTimeInterval timePassed = [object.createdAt timeIntervalSinceNow];
if ( timePassed > -(5*60))
{
NSLog(@"Success");
PFGeoPoint *geoPoint = [object objectForKey:@"geolocation"];
CLLocationCoordinate2D *beaconCoordinate = &((CLLocationCoordinate2D){geoPoint.latitude, geoPoint.longitude});
// Map Annotation
MapViewAnnotation *newAnnotation = [[MapViewAnnotation alloc] initWithTitle:[[alertView textFieldAtIndex:0] text] andCoordinate:*beaconCoordinate];
[self.mapView addAnnotation:newAnnotation];
[self.mapView selectAnnotation:newAnnotation animated:YES];
// 1
CLLocationCoordinate2D zoomLocation;
zoomLocation.latitude = geoPoint.latitude;
zoomLocation.longitude= geoPoint.longitude;
// 2
MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 0.5*METERS_PER_MILE, 0.5*METERS_PER_MILE);
// 3
[_mapView setRegion:viewRegion animated:YES];
}
//////////////////////////////////////////////////////////////////
else
{
NSLog(@"Fail");
UIAlertView *fail = [[UIAlertView alloc]initWithTitle: @"Please Try Again"
message: @"This Beacon Does Not Exist"
delegate: nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[fail show];
}
}
} else {
// Log details of the failure
NSLog(@"Error: %@ %@", error, [error userInfo]);
}
}];}
@end