In this tutorial we'll be looking at inserting a map view into your iPhone simulator, locating your current location, and switching between map, hybrid, and satellite views.

If this is your first time reading a tutorial, please go to the bottom of this thread for a link to all of my tutorials in order.

So let's start by creating a new project, selecting Single View application under iOS, and naming it MapView.

First we need to add the Map framework. To do this we first select our entire project in the left column:



Next we go to Build Phases, in the project summary page (fourth column):



Click on the third option, Link Binary With Libraries. Click on the plus button on the left:



Now you should see the below screen. Search for Map, and click on MapKit.framework. Click add, and the framework will be added to your project.



If you have more than one version of the iPhone SDK installed, you will see more than one version of the framework. I purged my previous Xcode installations, so i only have the 5.0.1 framework. Select that, it's the one we want.

Once it's added we can go straight into our ViewController.h and begin coding.

Start by adding this code right below the #import UIKit line:
Code:
#import <MapKit/MapKit.h>
*Also, add a curly bracket at the end of the @interface line.

Your .h should now look like:



The import line, if you haven't already figured it out, just imports the MapKit.framework we added before.

Next we'll add our MapView itself and name it, add an outlet for that view, and two actions.

Type this right below the @interface line:
Code:
	MKMapView *mapview ;
}
@property (nonatomic, retain) IBOutlet MKMapView *mapview ;
-(IBAction)setMap:(id)sender ;
-(IBAction)getLocation ;
Now your .h should look like:



In the above code we added our map view and named it map view, we added an outlet for our map view, we set an action for setting the view for our map, and an action for getting our current location.

That's it for the .h, we can now move on to our ViewController.m.

Once you're in the .m enter this code right below our @implementation line:
Code:
@synthesize mapview ;

-(IBAction)getLocation {
	mapview.showsUserLocation = YES ;
}

-(IBAction)setMap:(id)sender {
	switch (((UISegmentedControl *) sender).selectedSegmentIndex) {
        case 0:
            mapview.mapType = MKMapTypeStandard ;
            break;
        case 1:
            mapview.mapType = MKMapTypeSatellite ;
            break;
        case 2:
            mapview.mapType = MKMapTypeHybrid ;
            break;
            
        default:
            break;
    }
}
I strongly recommend copying from the case 0: line to the break ; line and pasting that twice below the break; line, and changing the values, to save time.

Now your .m should look like:


Pay attention to the highlighted code, it's what we just added.

This code synthesizes our map view, defines our action for getting the current location, and defines our action for the three different map view types we'll be using: Standard, Satellite, and Hybrid.

That's it for coding, now we can go to our .storyboard file.

We're going to add a Toolbar, a MapView, a Segmented Controls button, and a flexible space bar.



Arrange the Objects like shown below:



Next, rename the "Item" label to say Location. After that, select the segmented controls (First/Second) and go into the Attributes Inspector, the fourth arrow in the right column.



Go down to the Segments section, the fourth option, and change it from 2 to 3.

Next, rename the three segments to say Map, Satellite, and Hybrid respectively. Your view should now look like:



Next we'll link our actions to our view. Select the entire view and click on Connections Inspector, the last arrow in the right column.

Drag the mapview option under Outlets to the MapView, the getLocation action under Received Actions to the Location button, and the setMap action to the segmented controls.

Make sure you set the action for the segmented controls to Value Changed:



And, we're done. You can save, click Run, and test your Map.

Note: Because we are using an iPhone simulator, the Location button automatically zooms in on California, instead of your own location.

Thanks for reading, post any questions, problems, or concerns you have below, and be sure to check out my other tutorials here!