My Solution to Bronze and Silver Challenges


#1

[code]//
// WhereamiViewController.h
// Whereami
//
// Created by Roy Law on 13-5-28.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>

@interface WhereamiViewController : UIViewController
{
CLLocationManager *locationManager;
}

@end
[/code]

[code]//
// WhereamiViewController.m
// Whereami
//
// Created by Roy Law on 13-5-28.
// Copyright © 2013年 Roy Law. All rights reserved.
//

#import “WhereamiViewController.h”

@interface WhereamiViewController ()

@end

@implementation WhereamiViewController

-(id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nil bundle:nil];
if (self) {
locationManager = [[CLLocationManager alloc] init];

    [locationManager setDelegate:self];
    
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [locationManager startUpdatingLocation];
    [locationManager setDistanceFilter:50.000000];
    if ([CLLocationManager headingAvailable]) {
        [locationManager startUpdatingHeading];
    }else{
        NSLog(@"Heading is not supported.");
    }
 }
return self;

}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@“Location:%@”, newLocation);
}

-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@“could not find location:%@”, error);
}

-(void)locationManager:(CLLocationManager *)manager
didUpdateHeading:(CLHeading *)newHeading
{
NSLog(@“Heading:%@”, newHeading);
}

-(void)dealloc
{
[locationManager setDelegate:nil];
}

@end
[/code]
Console:

2013-05-29 09:01:11.008 Whereami[376:c07] Heading is not supported. 2013-05-29 09:01:11.018 Whereami[376:c07] Location:<+37.33233141,-122.03121860> +/- 5.00m (speed 0.00 mps / course -1.00) @ 5/29/13, 9:01:11 AM China Standard Time


#2

Thanks for the solution. There’s one thing I don’t get though:

How does this code continue to update the heading? Since it’s only called once, upon WhereamiViewController’s initialization inside initWithNibName:bundle:, how is it that every time I move the device it knows to execute this code?

if ([CLLocationManager headingAvailable]) {
[locationManager startUpdatingHeading];
}

thanks,