How to center my current location in MKMapView?

vipul picture vipul · Nov 24, 2011 · Viewed 32.8k times · Source

I am showing the current location in MKMapView by using showUserLocation enables. I also want to center the mapview to the user current location and also the zoomed map of location. Please help me regarding this as I don't get any help from other similar questions on stackoverflow.

My code is below:

- (void)viewDidLoad {
  [super viewDidLoad];
  [mapView setMapType:MKMapTypeStandard];
  [mapView setZoomEnabled:YES];
  [mapView setScrollEnabled:YES];
  [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
  [mapView setDelegate:self];
}

Answer

Denis picture Denis · Nov 24, 2011

For centering on user location, you can use the following code:

[mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];

For zooming on special locations, you should study how the regions (MKCoordinateRegion) are counted and work, count your values for the region and display it using call:

[mapView setRegion:myRegion animated:YES];

This sample WorldCities shows basics of region displaying.