Skip to content

Instantly share code, notes, and snippets.

@cpaika
Last active August 29, 2015 14:09
Show Gist options
  • Save cpaika/c6d64cf70e3af101dc22 to your computer and use it in GitHub Desktop.
Save cpaika/c6d64cf70e3af101dc22 to your computer and use it in GitHub Desktop.
Presentation Core Location
@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *previousPoint;
@property (assign, nonatomic) CLLocationDistance totalMovementDistance;
@property (weak, nonatomic) IBOutlet UILabel *latitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *longitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;
@property (weak, nonatomic) IBOutlet UILabel *altitudeLabel;
@property (weak, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;
@property (weak, nonatomic) IBOutlet UILabel *distanceTraveledLabel;
#pragma mark - CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
didUpdateLocations:(NSArray *)locations {
CLLocation *newLocation = [locations lastObject];
NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.latitude];
self.latitudeLabel.text = latitudeString;
NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
newLocation.coordinate.longitude];
self.longitudeLabel.text = longitudeString;
NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.horizontalAccuracy];
self.horizontalAccuracyLabel.text = horizontalAccuracyString;
NSString *altitudeString = [NSString stringWithFormat:@"%gm",
newLocation.altitude];
self.altitudeLabel.text = altitudeString;
NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
newLocation.verticalAccuracy];
self.verticalAccuracyLabel.text = verticalAccuracyString;
if (newLocation.verticalAccuracy < 0 ||
newLocation.horizontalAccuracy < 0) {
// invalid accuracy
return;
}
if (newLocation.horizontalAccuracy > 100 ||
newLocation.verticalAccuracy > 50) {
// accuracy radius is so large, we don't want to use it
return;
}
if (self.previousPoint == nil) {
self.totalMovementDistance = 0;
} else {
NSLog(@"movement distance: %f", [newLocation
distanceFromLocation:self.previousPoint]);
self.totalMovementDistance += [newLocation
distanceFromLocation:self.previousPoint];
}
self.previousPoint = newLocation;
NSString *distanceString = [NSString stringWithFormat:@"%gm",
self.totalMovementDistance];
self.distanceTraveledLabel.text = distanceString;
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error {
NSString *errorType = (error.code == kCLErrorDenied) ?
@"Access Denied" : @"Unknown Error";
UIAlertView *alert = [[UIAlertView alloc]
initWithTitle:@"Error getting Location"
message:errorType
delegate:nil
cancelButtonTitle:@"Okay"
otherButtonTitles:nil];
[alert show];
}
@property (strong, nonatomic) Firebase * timeRef;
@property (strong, nonatomic) Firebase * latRef;
@property (strong, nonatomic) Firebase * longRef;
@property (strong, nonatomic) CLLocation * coords;
@property (strong, nonatomic) Firebase *root;
@property (strong, nonatomic) NSMutableDictionary * others;
@property (assign, nonatomic) double oldTime;
UIDevice *device = [UIDevice currentDevice];
NSString *currentDeviceId = [[device identifierForVendor]UUIDString];
NSString * url = @"https://491presentation.firebaseio.com/";
self.root = [[Firebase alloc] initWithUrl:url];
url = [url stringByAppendingString: currentDeviceId];
Firebase *myRootRef = [[Firebase alloc] initWithUrl:url];
self.latRef = [myRootRef childByAppendingPath: @"/latitude"];
self.longRef = [myRootRef childByAppendingPath: @"/longitude"];
self.timeRef = [myRootRef childByAppendingPath:@"/velocity"];
[self.root observeEventType:FEventTypeChildAdded withBlock:^(FDataSnapshot *snapshot) {
[self displayData:snapshot];
} withCancelBlock:^(NSError *error) {
NSLog(@"%@", error.description);
}];
[self.root observeEventType:FEventTypeChildChanged withBlock:^(FDataSnapshot *snapshot) {
[self changeChild:snapshot];
} withCancelBlock:^(NSError *error) {
NSLog(@"%@", error.description);
}];
self.others = [[NSMutableDictionary alloc] init];
self.oldTime = [[NSDate date] timeIntervalSince1970];
double now = [[NSDate date] timeIntervalSince1970];
double difference = now - self.oldTime;
double distance = [self.coords distanceFromLocation:newLocation];
double velocity = distance/difference;
self.coords = newLocation;
self.oldTime = now;
[self sendLocation:velocity];
-(void)displayData:(FDataSnapshot*)snapshot
{
CLLocationCoordinate2D coordinates;
NSString * lat = snapshot.value[@"latitude"];
NSString * lon = snapshot.value[@"longitude"];
coordinates.latitude = [lat doubleValue];
coordinates.longitude = [lon doubleValue];
BIDPlace *start = [[BIDPlace alloc] init];
start.coordinate = coordinates;
start.title = snapshot.key;
start.subtitle = @"This user is located here";
[self.mapView addAnnotation:start];
[self.others setObject:start forKey:snapshot.key];
}
-(void)changeChild:(FDataSnapshot*)snapshot
{
BIDPlace * current = self.others[snapshot.key];
CLLocationCoordinate2D coordinates;
NSString * lat = snapshot.value[@"latitude"];
NSString * lon = snapshot.value[@"longitude"];
NSString * velocity = snapshot.value[@"velocity"];
coordinates.latitude = [lat doubleValue];
coordinates.longitude = [lon doubleValue];
current.coordinate = coordinates;
CLLocation * loc = [[CLLocation alloc] initWithLatitude:coordinates.latitude longitude:coordinates.longitude];
NSString * distance = @"This user is ";
double distValue = [self.coords distanceFromLocation:loc];
NSNumber *myDoubleNumber = [NSNumber numberWithDouble:distValue];
distance = [distance stringByAppendingString: [myDoubleNumber stringValue]];
distance = [distance stringByAppendingString:@" m away"];
current.title =[@"Velocity: " stringByAppendingString: velocity];
current.subtitle = distance;
}
-(void) sendLocation:(double)velocity
{
[self.latRef setValue:[NSString stringWithFormat:@"%f", self.coords.coordinate.latitude]];
[self.longRef setValue:[NSString stringWithFormat:@"%f", self.coords.coordinate.longitude]];
[self.timeRef setValue: @(velocity).stringValue];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment