Advertise Mobile SDKs Books Events Forum News Social Networking Support Us
Follow @iphonedevsdk on Twitter

Interface 2, Advanced iOS
Mockup & Code Gen
($9.99)

Make your own iPhone apps
and run them live!
(free)

Pic Frame Dynamo: Photo Editing
($0.99)

Abiliator
($1.99)

Want your application or service advertised on iPhone Dev SDK?

Go Back   iPhone Dev SDK Forum > iPhone SDK Development Forums > iPhone SDK Tutorials > Tutorial Discussion

Reply
 
LinkBack Thread Tools Display Modes
Old 02-01-2010, 02:49 AM   #1 (permalink)
Registered Member
 
Join Date: Sep 2009
Posts: 24
iamsgtyang is on a distinguished road
Default MKMapView tutorial using latitude and longitude

Quick tutorial on how to create a MKMapView that sets its region to a specific latitude and longitude. You can check out the source code at my site if needed.

1. Create New Project, View-based Application, and name it MapTutorial
2. Add a new existing framework: MapKit.framework
3. Add the following to MapTutorialViewController.h

#import <MapKit/MapKit.h>
MKMapView *mapView;

The final MapTutorialViewController.h file should look like this

#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface MapTutorialViewController : UIViewController {

MKMapView *mapView;

}

@end

4. Add the following to viewDidLoad in MapTutorialViewController.m

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.mapType = MKMapTypeHybrid;

CLLocationCoordinate2D coord = {latitude: 61.2180556, longitude: -149.9002778};
MKCoordinateSpan span = {latitudeDelta: 0.2, longitudeDelta: 0.2};
MKCoordinateRegion region = {coord, span};

[mapView setRegion:region];
[self.view addSubview:mapView];

What happened here is we allocated MKMapView to our mapView and set the location and size of the map view to fit the screen’s view bounds. We also declared the mapType to be MKMapTypeHybrid. If you want to know more, right click on MKMapTypeHybrid in your Xcode and search for the term in the documentation.

Now, we can just set the map to display our current location, which is easy but it takes the fun away. This is where the map’s API is tricky. We can set the region to display on the map by Google Map’s latitude and longitude. To do this, we have to get the MKCoordinateRegion for the location we want.

Here’s the long part. We have to know the longitude and latitude of the location from Google Maps. You get this in the URL when you search for a location. This is where CLLocationCoordinate2D coord = {} come in. You will set the latitude and longitude here.

Next the MKCoordinateSpan (defines the area spanned by a map region), I set it at 0.2. MKCoordinateRegion is made of CLLocationCoordinate2D and MKCoordinateSpan.

The reason for all this is because MKMapView’s setRegion parameter requires a MKCoordinateRegion. Finally, we add the mapView as a subview to self.view

The Final MapTutorialViewController.m file should look like this

#import “MapTutorialViewController.h”

@implementation MapTutorialViewController

- (void)viewDidLoad {

[super viewDidLoad];

mapView = [[MKMapView alloc] initWithFrame:self.view.bounds];
mapView.mapType = MKMapTypeHybrid;

CLLocationCoordinate2D coord = {latitude: 61.2180556, longitude: -149.9002778};
MKCoordinateSpan span = {latitudeDelta: 0.2, longitudeDelta: 0.2};
MKCoordinateRegion region = {coord, span};

[mapView setRegion:region];
[self.view addSubview:mapView];

}

- (void)didReceiveMemoryWarning {

[super didReceiveMemoryWarning];

}

- (void)dealloc {

[super dealloc];

}

@end

Now that’s it. Build and Run, you will see a hybrid map at the latitude and longitude provided (Anchorage, Alaska) To change the location, just change the latitude and longitude value at CLLocationCoordinate2D coord = {latitude: value, longitude: value};
iamsgtyang is offline   Reply With Quote
Old 10-24-2010, 02:48 PM   #2 (permalink)
Registered Member
 
Join Date: Oct 2010
Location: Silverstone, UK
Posts: 8
Philips is on a distinguished road
Default thanks

This was the most straight forward and easy to understand work-through I have come across. Most tutorials try to add a number of 'bells and whistles' from the start which makes it very difficult to apply the material in new contexts.

Anyway, thank you. All I need now is to disentangle the issue of adding 'annotations'!
Philips is offline   Reply With Quote
Old 12-02-2010, 12:30 AM   #3 (permalink)
Ans
IA Interactive Solutions
 
Join Date: Dec 2010
Location: Lahore, Pakistan
Posts: 3
Ans is on a distinguished road
Send a message via Skype™ to Ans
Default Great Work

very helpful tutorial. one very minor correction. add #import <MapKit/MapKit.h> in .m file too..
can anyone tell how to get location so that when map load it focus on my location at startup.

aasims.wordpress.com
Ans is offline   Reply With Quote
Old 12-08-2010, 12:02 AM   #4 (permalink)
Registered Member
 
Join Date: Dec 2010
Location: Ankara/Turkey
Posts: 1
dogancoruh is on a distinguished road
Default Congrats

For sure, this is the best and straight forward explanation of MKMapView component for a newbie as Philips said. Thanks for quality material for all

dogancoruh.blogspot.com

Last edited by dogancoruh; 12-08-2010 at 12:05 AM.
dogancoruh is offline   Reply With Quote
Old 02-07-2011, 01:42 PM   #5 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 3
Sammy42 is on a distinguished road
Default

For some reason I get 'mapView' undeclared? With the code:

mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];

Anybody know why?
Sammy42 is offline   Reply With Quote
Old 02-28-2011, 04:31 AM   #6 (permalink)
Registered Member
 
Join Date: Feb 2011
Posts: 1
khalils is on a distinguished road
Default

Quote:
Originally Posted by Sammy42 View Post
For some reason I get 'mapView' undeclared? With the code:

mapView=[[MKMapView alloc] initWithFrame:self.view.bounds];

Anybody know why?
Did u declare the mapView in the header file?
khalils is offline   Reply With Quote
Old 07-03-2011, 02:16 AM   #7 (permalink)
Registered Member
 
Join Date: Feb 2010
Posts: 25
jonthornham is on a distinguished road
Default Thanks

Awesome post. Thanks for taking the time to put it up.

Jon
jonthornham is offline   Reply With Quote
Reply

Bookmarks

Thread Tools
Display Modes

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are On
Pingbacks are On
Refbacks are On



» Advertisements
» Stats
Members: 175,696
Threads: 94,139
Posts: 402,959
Top Poster: BrianSlick (7,990)
Welcome to our newest member, jasper_muc
Powered by vBadvanced CMPS v3.1.0

All times are GMT -5. The time now is 01:54 PM.
Powered by vBulletin® Version 3.8.0
Copyright ©2000 - 2012, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.3.0