Bronze Challenge


#1

The first step was to create a new subclass of UIViewController. I gave mine the title “MapViewViewController.” I chose to not use a XIB file. Instead, I created the view programmatically. After creating the view, I added it to the UITabBarController in HypnoAppDelegate.

Here is the code for MapViewViewController.h:

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

@interface MapViewViewController : UIViewController
{
MKMapView *map;
}

@end[/code]

Here is the code for MapViewViewController.m:

[code]#import “MapViewViewController.h”

@interface MapViewViewController ()

@end

@implementation MapViewViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    map = [[MKMapView alloc] init];
    [map setDelegate:self];
    [map setShowsUserLocation: YES];

      [[self tabBarItem] setTitle:@"Map View"];
    

    }
    return self;
    }

  • (void)loadView
    {
    [self setView:map];
    }

  • (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
    {
    CLLocationCoordinate2D location = [userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(location, 500, 500);
    [map setRegion:region animated:YES];
    }

@end
[/code]

Here is the code for HypnoAppDelegate.m:

[code]#import “HypnoAppDelegate.h”
#import “HypnosisViewController.h”
#import “TimeViewController.h”
#import “MapViewViewController.h”

@implementation HypnoAppDelegate

@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];

    HypnosisViewController *hvc = [[HypnosisViewController alloc] init];
    TimeViewController *tvc = [[TimeViewController alloc] init];
    MapViewViewController *mvc = [[MapViewViewController alloc] init];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];
    NSArray *viewControllers = [NSArray arrayWithObjects:hvc, tvc, mvc, nil];
    [tabBarController setViewControllers:viewControllers];
    [[self window] setRootViewController:tabBarController];

    return YES;
    }

@end[/code]

This code compiles and runs on my machine. Hopefully it will do the same on yours.

(Almost forgot, remember to simulate a location in Xcode!)


#2

Also don’t forget to import the MapKit.framework.


#3

Here is my solution

MapViewController.h

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

@interface MapViewController : UIViewController
{
MKMapView *map;
}

@end[/code]

MapViewController.m

[code]#import “MapViewController.h”

@implementation MapViewController

  • (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
    {
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
    map = [[MKMapView alloc] init];

      [[self tabBarItem] setTitle:@"Map View"];
    

    }
    return self;
    }

  • (void)loadView
    {
    [self setView:map];
    }

@end[/code]

HypnoAppDelegate.h

[code]#import “HypnoAppDelegate.h”
#import “HypnosisViewController.h”
#import “TimeViewController.h”
#import “MapViewController.h”

@implementation HypnoAppDelegate

@synthesize window = _window;

  • (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
    {
    self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
    // Override point for customization after application launch.

    TimeViewController *tvc = [[TimeViewController alloc] init];

    HypnosisViewController *hvc = [[HypnosisViewController alloc] init];

    // Bronze Challenge
    MapViewController *mvc = [[MapViewController alloc] init];

    UITabBarController *tabBarController = [[UITabBarController alloc] init];

    NSArray *viewControllers = [NSArray arrayWithObjects:hvc, tvc, mvc, nil];
    [tabBarController setViewControllers:viewControllers];

    [[self window] setRootViewController:tabBarController];

    self.window.backgroundColor = [UIColor whiteColor];
    [self.window makeKeyAndVisible];
    return YES;
    }

@end
[/code]


#4

andre3k1, you would also need to use the CLLocationManager so that we find a location and update the map to select a region with that location.


#5

I first did plain solution, and then inspired by andre3k1 also incorporated location into the map. I also used the xib file to associate the Map View as an outlet and the root UIView as the root view of the View Controller.

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

@interface MapViewController : UIViewController <MKMapViewDelegate, CLLocationManagerDelegate>
{
    IBOutlet MKMapView *mapView;
    
    CLLocationManager *locationManager;
}

- (void)foundLocation:(CLLocation *)loc;

@end
#import "MapViewController.h"
#import <Mapkit/Mapkit.h>

@interface MapViewController ()

@end

@implementation MapViewController

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {
        // Custom initialization
        UITabBarItem *tbi = [self tabBarItem];
        [tbi setTitle:@"Map"];
        UIImage *i = [UIImage imageNamed:@"Time.png"];
        [tbi setImage:i];
        
        locationManager = [[CLLocationManager alloc] init];
        mapView = [[MKMapView alloc] init];
        
        [locationManager setDelegate:self];
        [mapView setShowsUserLocation:YES];
        //[mapView setMapType:MKMapTypeHybrid];
    }
    return self;
}

- (void)foundLocation:(CLLocation *)loc
{
    CLLocationCoordinate2D coord = [loc coordinate];
    
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(coord, 250, 250);
    [mapView setRegion:region animated:YES];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    [self foundLocation:newLocation];
}

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

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    NSLog(@"MapViewController did unload");
}

- (void) viewWillAppear:(BOOL)animated
{
    [super viewWillAppear];
    NSLog(@"MapViewController view will appear");
}

- (void) viewDidAppear:(BOOL)animated
{
    [super viewDidAppear];
    [locationManager startUpdatingLocation]; 
    NSLog(@"MapViewController view did appear");
}

- (void) viewWillDisappear:(BOOL)animated
{
    [super viewWillDisappear];
    NSLog(@"MapViewController view will disapper");
}

- (void) viewDidDisappear:(BOOL)animated
{
    [super viewDidDisappear];
    [locationManager stopUpdatingLocation];
    NSLog(@"MapViewController did disappear");
}

@end