Search code examples
iphoneiosmkmapview

How to center my current location in MKMapView?


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];
}

Solution

  • 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.