Bronze, Silver & Gold Challenge


#1

BNRMapPoint.h

[code]#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface BNRMapPoint : NSObject

{

}

                                                                   //*********************
                                                                   //                    *
                                                                   //   Gold Challenge   *
                                                                   //                    *
  • (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)s;

                                                                     //                    *
                                                                     //*********************
    

@property (nonatomic,readonly) CLLocationCoordinate2D coordinate;

@property (nonatomic, copy) NSString *title;

// ********************************************************************************************
// *
// Gold Challenge *
// *

@property (nonatomic, copy) NSString *subtitle;

// *
// ********************************************************************************************

@end
[/code]

BNRMapPoint.m

[code]#import “BNRMapPoint.h”

@implementation BNRMapPoint

                           //*****************
                           //                *
                           // Gold Challenge *
                           //                *

@synthesize coordinate, title, subtitle;

                           //                *
                           //*****************

                                                                   //*********************
                                                                   //                    *
                                                                   //   Gold Challenge   *
                                                                   //                    *
  • (id)initWithCoordinate:(CLLocationCoordinate2D)c title:(NSString *)t subtitle:(NSString *)s

                                                                     //                    *
                                                                     //*********************
    

{
self = [super init];
if (self)
{
coordinate = c;
[self setTitle:t];

    // ********************************************************************************************
    //                                                                                            *
    //                                       Gold Challenge                                       *
    //                                                                                            *
    
    [self setSubtitle:s];
    
    //                                                                                            *
    // ********************************************************************************************
}
return self;

}

  • (id)init
    {
    //*********************
    // *
    // Gold Challenge *
    // *

    return [self initWithCoordinate:CLLocationCoordinate2DMake(43.07, -89.32) title:@“Hometown” subtitle:@“Date”];

                                                                                              //                    *
                                                                                              //*********************
    

}

@end
[/code]

WhereamiViewController.h

[code]#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
#import <MapKit/MapKit.h>

@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate, MKMapViewDelegate, UITextFieldDelegate>

{
CLLocationManager *locationManager;

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

// ********************************************************************************************
//                                                                                            *
//                                      Silver Challenge                                      *
//                                                                                            *

IBOutlet UISegmentedControl *segmentMapType;

//                                                                                            *
// ********************************************************************************************

}

  • (void)findLocation;
  • (void)foundLocation:(CLLocation *)loc;

// Necessary if a create the target-action by IB
//- (IBAction)changeMapType:(id)sender;

@end
[/code]

WhereamiViewController.m

[code]#import “WhereamiViewController.h”
#import “BNRMapPoint.h”

@implementation WhereamiViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];

    if (self)
    {
    locationManager = [[CLLocationManager alloc] init];
    [locationManager setDelegate:self];
    [locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    }

    return self;
    }

  • (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
    NSLog(@"%@", 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)dealloc
    {
    [locationManager setDelegate:nil];
    }

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

// ********************************************************************************************
//                                                                                            *
//                                      Bronze Challenge                                      *
//                                                                                            *

//[worldView setMapType:MKMapTypeSatellite];

//                                                                                            *
// ********************************************************************************************

// ********************************************************************************************
//                                                                                            *
//                                      Silver Challenge                                      *
//                                                                                            *

[segmentMapType addTarget:self action:@selector(change) forControlEvents:UIControlEventValueChanged];

//                                                                                            *
// ********************************************************************************************

}

  • (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    CLLocationCoordinate2D loc = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 250, 250);
    [worldView 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];

    // ********************************************************************************************
    // *
    // Gold Challenge *
    // *

    NSString *dateAnnotation = [NSDateFormatter localizedStringFromDate:[loc timestamp] dateStyle:NSDateFormatterMediumStyle timeStyle:NSDateFormatterMediumStyle];

    BNRMapPoint *mp = [[BNRMapPoint alloc] initWithCoordinate:coord title:[locationTitleField text] subtitle:dateAnnotation];

    // *
    // ********************************************************************************************

    [worldView addAnnotation:mp];

    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [worldView setRegion:region animated:YES];

    [locationTitleField setText:@""];
    [activityIndicator stopAnimating];
    [locationTitleField setHidden:NO];
    [locationManager stopUpdatingLocation];
    }

// Necessary if a create the target-action by IB
//- (IBAction)changeMapType:(id)sender
//{
// [worldView setMapType:[segmentMapType selectedSegmentIndex]];
//}

// ********************************************************************************************
// *
// Silver Challenge *
// *

  • (void)change
    {
    [worldView setMapType:[segmentMapType selectedSegmentIndex]];
    }

// *
// ********************************************************************************************

@end[/code]


#2

This is a good solution, I think…but when I try to implemente it the compiler tells me:"No visible @interface for ‘BNRMapPoint’ declares the selector "InitWithCoordinate:title:"
Can somebody helps me please?