My Solution to 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>
#import <MapKit/MapKit.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>
{
CLLocationManager *locationManager;

IBOutlet MKMapView *worldView;
IBOutlet UITextField *locationTitleField;
IBOutlet UIActivityIndicatorView *activityIndicator;
IBOutlet UISegmentedControl *mapTypeSel;

}

-(void)findLocation;
-(void)foundLocation:(CLLocation *)loc;
-(IBAction)changeMapType:(id)sender;

@end
[/code]

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

#import “WhereamiViewController.h”
#import “BNRMapPoint.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];

/*Chapter 4’s Challenges

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

*/
[mapTypeSel addTarget:self action:@selector(changeMapType:) forControlEvents:UIControlEventValueChanged];
}
return self;
}

-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@“Location:%@”, newLocation);
NSTimeInterval t = [[newLocation timestamp] timeIntervalSinceNow];
if (t<-180) {
return;
}
[self foundLocation: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];
}

-(void)viewDidLoad
{
[worldView setShowsUserLocation:YES];
}

-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
CLLocationCoordinate2D loc = [userLocation coordinate];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
// [mapView setMapType:MKMapTypeSatellite];
[mapView setRegion:region animated:YES];
}

-(BOOL)textFieldShouldReturn:(UITextField *)textField
{
[self findLocation];
[textField resignFirstResponder];
return YES;
}

-(void)findLocation
{
[locationManager startUpdatingLocation];
[activityIndicator startAnimating];
[locationTitleField setHidden:YES];
}

-(void)foundLocation:(CLLocation *)loc
{
CLLocationCoordinate2D coord = loc.coordinate;
NSDateFormatter *aDateFormatter = [[NSDateFormatter alloc]init];
[aDateFormatter setDateStyle:NSDateFormatterMediumStyle];

BNRMapPoint *mp = [[BNRMapPoint alloc]initWithCoordinate:coord title:locationTitleField.text subtitle:[aDateFormatter stringFromDate:[NSDate date]]];
[worldView addAnnotation:mp];
MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
[worldView setRegion:region animated:YES];

locationTitleField.text = @"";
[activityIndicator stopAnimating];
[locationTitleField setHidden:NO];
[locationManager stopUpdatingLocation];

}
-(IBAction)changeMapType:(id)sender
{
[worldView setMapType:[sender selectedSegmentIndex]];
NSLog(@“MapType changed.”);

}

@end
[/code]

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

#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface BNRMapPoint : NSObject
{

}

-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)st;

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@end
[/code]

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

#import “BNRMapPoint.h”

@implementation BNRMapPoint

@synthesize coordinate,title, subtitle;

-(id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)st
{
self = [super init];
if (self) {
coordinate = c;//readonly property whether can use setter func on?no
self.title = t;
self.subtitle = st;
NSLog(@"%@",self.subtitle);
}
return self;
}

-(id)init
{
return [self initWithCoordinate:CLLocationCoordinate2DMake(43.07, -89.32) title:@“hometown” subtitle:@“today”];
}

@end
[/code]