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

By Neal Goldstein, Dave Wilson

iOS app programmers have so many options for their projects. What about showing the user’s location on the map? That’s easy! In the MapController.m file, add the code in bold to viewDidLoad in MapController.m.

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

Your additions start by making the MapController the Map View delegate. showsUserLocation is a MKMapView property that tells the Map view whether to show the user location. If YES, you get that same blue pulsing dot you see displayed in the built-in Maps application.

If you were to compile and run the application as it stands now on your iPad, you’d be asked if it were okay to use your current location, and if you tapped OK, you’d get a U.S. map in Landscape orientation with a blue dot that represents the iPad’s current location. (You may have to pan the map to see it.)

Of course, to see it in Landscape orientation, you have to turn the iPad, or choose Hardware→Rotate Right (or Rotate Left) from the Simulator menu.

An iPad app shows the location in a map.

That’s what happens on your iPad. On the Simulator, the story is different.

After launching the application in the Simulator, choose San Francisco by showing the Debug area in the View selector on the toolbar, clicking the Simulate Location icon (it looks like the standard Location icon) in the Debug bar in the Workspace window, and then selecting San Francisco from the menu that appears. You can also add more locations.

You also can simulate the location (with some interesting choices) on the Simulator Debug menu — choose Debug→Location. Check out City Run for example, or even enter the GPS coordinates for any location (choose Custom Location).

If you don’t see the current location, you might want to check to make sure that you’ve created the mapView outlet and connected it to the Map view in the storyboard.

Touching the blue dot also displays what’s called an annotation, and you can customize the text to display whatever you cleverly devise — including the address of the current location.

The Xcode workspace in a Mac.