6

I have read and followed the instructions on How do I zoom an MKMapView to the users current location without CLLocationManager?

However, while I do get the current user location, I am unable to actually make the map visually center there.

For example, in the viewDidLoad function, how can I cause the map to center visually around the users known location? I have pasted the code from CurrentLocation below (See the XXX comment in viewDidLoad


#import "MapViewController.h"
#import "PlacemarkViewController.h"

@implementation MapViewController

@synthesize mapView, reverseGeocoder, getAddressButton;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // XXX HERE: How can I cause self.mapView to actually recenter itself at self.mapview.userLocation.location?


    mapView.showsUserLocation = YES;
}

- (void)viewDidUnload
{
    self.mapView = nil;
    self.getAddressButton = nil;
}

- (void)dealloc
{
    [reverseGeocoder release];
    [mapView release];
    [getAddressButton release];

    [super dealloc];
}

- (IBAction)reverseGeocodeCurrentLocation
{
    self.reverseGeocoder =
        [[[MKReverseGeocoder alloc] initWithCoordinate:mapView.userLocation.location.coordinate] autorelease];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFailWithError:(NSError *)error
{
    NSString *errorMessage = [error localizedDescription];
    UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Cannot obtain address."
                                                        message:errorMessage
                                                       delegate:nil
                                              cancelButtonTitle:@"OK"
                                              otherButtonTitles:nil];
    [alertView show];
    [alertView release];
}

- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
    PlacemarkViewController *placemarkViewController =
        [[PlacemarkViewController alloc] initWithNibName:@"PlacemarkViewController" bundle:nil];
    placemarkViewController.placemark = placemark;
    [self presentModalViewController:placemarkViewController animated:YES];
}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    // we have received our current location, so enable the "Get Current Address" button
    [getAddressButton setEnabled:YES];
}

@end
Community
  • 1
  • 1
DeShawn Terrell
  • 295
  • 2
  • 4
  • 7

1 Answers1

26

It looks like you can simply use the centerCoordinate property of the MKMapView class - the docs say:

Changing the value in this property centers the map on the new coordinate without changing the current zoom level. It also updates the values in the region property to reflect the new center coordinate and the new span values needed to maintain the current zoom level.

So basically, you just do:

self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate;
Tim
  • 59,527
  • 19
  • 156
  • 165
  • 1
    you would want either `mapView.userLocation.coordinate` or `mapView.userLocation.location.coordinate`. The centerCoordinate property is of type `CLLocationCoordinate2D`. – Anurag Feb 06 '11 at 18:06
  • Thanks Guys. So doing self.mapView.centerCoordinate = self.mapView.userLocation.location.coordinate; does not work from within viewDidLoad. It works elsewhere (like when done within reverseGeocodeCurrentLocation) - DT – DeShawn Terrell Feb 06 '11 at 20:20
  • Is there any particular reason you need it to be inside `viewDidLoad`? – Tim Feb 07 '11 at 05:06
  • 8
    You can also use [self.mapView setCenterCoordinate:self.mapView.userLocation.location.coordinate animated:YES] to get this animated – Dulgan Apr 15 '13 at 08:49
  • You may also want to use setRegion: animated: instead of just setting the center coordinate, as you may want the map to zoom in / out as well as moving to where the user location marker is. – shim Oct 06 '14 at 04:43