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
MKMapView *mapView;
The final MapTutorialViewController.h file should look like this
#import
#import
@interface MapTutorialViewController : UIViewController {
MKMapView *mapView;
}
@end4. 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 screens 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 maps API is tricky. We can set the region to display on the map by Google Maps latitude and longitude. To do this, we have to get the MKCoordinateRegion for the location we want.
Heres 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 MKMapViews 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];
}
@endNow thats 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};
Replies
Anyway, thank you. All I need now is to disentangle the issue of adding 'annotations'!
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomecan anyone tell how to get location so that when map load it focus on my location at startup.
aasims.wordpress.com
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesomedogancoruh.blogspot.com
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomemapView=[[MKMapView alloc] initWithFrame:self.view.bounds];
Anybody know why?
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like AwesomeJon
- Spam
- Abuse
- Troll
0 • Off Topic Insightful Disagree Dislike Like Awesome