How to Show the User’s Region on a Map in Your iOS App - dummies

How to Show the User’s Region on a Map in Your iOS App

By Neal Goldstein, Dave Wilson

What if you want your iOS app to display your user’s location in relation to where they are? Ideally, when you get to New York (or wherever), you should see a map that centers on where you are as opposed to the entire United States. First, you need to look at how you center the map. To do that, you need to add the setInitialRegion method declaration to MapController.m.

First, update the class extension by adding the bolded code in Listing 17-3 to the class extension in MapController.m.

@interface MapController ()
@property (weak, nonatomic) IBOutlet MKMapView *mapView;
- (IBAction)mapType:(id)sender;
- (void)setInitialRegion;

Because the Map controller will get its data from the object, as it should, you have to update the implementation to import the class as well. Add the bolded coded to MapController.m.

#import "MapController.h"
#import "AppDelegate.h"
#import "Trip.h"

Finally, add the setInitialRegion method to MapController.m.

- (void) setInitialRegion {
AppDelegate* appDelegate =
[[UIApplication sharedApplication] delegate];
MKCoordinateRegion region;
CLLocationCoordinate2D initialCoordinate =
[appDelegate.trip destinationCoordinate]; = initialCoordinate.latitude; = initialCoordinate.longitude;
region.span.latitudeDelta = .05;
region.span.longitudeDelta = .05;
[self.mapView setRegion:region animated:NO];

You then need to update viewDidLoad to use this method. Add the code in bold in Listing 17-6 to viewDidLoad in MapController.m to send this message.

- (void)viewDidLoad
[super viewDidLoad];
self.mapView.delegate = self;
self.mapView.showsUserLocation = YES;
[self setInitialRegion];

If you run this now, you don’t see the annotations … at least not yet.

Setting the region is how you center the map and set the zoom level. You accomplish all this with the following statement:

[self.mapView setRegion:region animated:NO];

A region is a Map view property that specifies four pieces of information:

  • Specifies the latitude of the center of the map.

  • Specifies the longitude of the center of the map.

    For example, the center of the map would be New York if you were to set those values as = 40.712756; = -74.006047;
  • region.span.latitudeDelta: Specifies the north-to-south distance (in latitudinal degrees) to display on the map. One degree of latitude is approximately 111 kilometers (69 miles). A region.span.latitudeDelta of 0.0036 would specify a north-to-south distance on the map of about a quarter of a mile. Latitudes north of the equator have positive values, whereas latitudes south of the equator have negative values.

  • region.span.longiudeDelta: Specifies the east-to-west distance (in longitudinal degrees) to display on the map. Unfortunately, the number of miles in one degree of longitude varies based on the latitude. For example, one degree of longitude is approximately 69 miles at the equator, but shrinks to 0 miles at the poles.

    Longitudes east of the zero meridian (by international convention, the zero or prime meridian passes through the Royal Observatory, Greenwich, in east London) have positive values, and longitudes west of the zero meridian have negative values.


Although the span values provide an implicit zoom value for the map, the actual region you see displayed may not equal the span you specify because the map will go to the zoom level that best fits the region that’s set.

This also means that even if you just change the center coordinate in the map, the zoom level may change because distances represented by a particular span may change at different latitudes and longitudes. To account for that, those smart developers at Apple included a property you can set that changes the center coordinate without changing the zoom level:

@property (nonatomic)     CLLocationCoordinate2D centerCoordinate

When you change the value of this property with a new CLLocationCoordinate2D, the map is centered on the new coordinate, and the span values are updated to maintain the current zoom level.

That CLLocationCoordinate2D type is something you’ll be using a lot. The CLLocationCoordinate2D type is a structure that contains a geographical coordinate using the WGS 84 reference frame (the reference coordinate system used by the Global Positioning System):

typedef struct {CLLocationDegrees latitude;CLLocationDegrees longitude;} CLLocationCoordinate2D;

Here’s a little explanation:

  • latitude: The latitude in degrees. This is the value you set in the code you just entered ( = latitude;). Positive values indicate latitudes north of the equator. Negative values indicate latitudes south of the equator.

  • longitude: The longitude in degrees. This is the value you set in the code you just entered ( = longitude;). Measurements are relative to the zero meridian, with positive values extending east of the meridian and negative values extending west of the meridian.

CLLocationDegrees represents a latitude or longitude value specified in degrees and is a double.

To center the map display on New York, you send the setInitialRegion message (the code you just entered) when the view is loaded in the viewDidLoad: method.