December 2012
Intermediate to advanced
834 pages
27h
English
You want to instantiate and display a map on a view.
Create an instance of the MKMapView class and add it to a view or assign
it as a subview of your view controller. Here is the sample .h file of a view controller that creates an
instance of MKMapView and displays it
full-screen on its view:
#import <UIKit/UIKit.h>#import <MapKit/MapKit.h>@interfaceCreating_a_Map_ViewViewController:UIViewController@property(nonatomic,strong)MKMapView*myMapView;@end
This is a simple root view controller with a variable of type
MKMapView. Later in the
implementation of this view controller (.m file), we will initialize the map and set
its type to Satellite, like
so:
#import "Creating_a_Map_ViewViewController.h"@implementationCreating_a_Map_ViewViewController-(void)didReceiveMemoryWarning{[superdidReceiveMemoryWarning];}-(void)viewDidLoad{[superviewDidLoad];self.view.backgroundColor=[UIColorwhiteColor];self.myMapView=[[MKMapViewalloc]initWithFrame:self.view.bounds];/* Set the map type to Satellite */self.myMapView.mapType=MKMapTypeSatellite;self.myMapView.autoresizingMask=UIViewAutoresizingFlexibleWidth|UIViewAutoresizingFlexibleHeight;/* Add it to our view */[self.viewaddSubview:self.myMapView];}-(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation{returnYES;}@end
Creating an instance of the MKMapView class is quite straightforward. We can simply assign a frame to it ...
Read now
Unlock full access