19

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];
}
Girish
  • 4,692
  • 4
  • 35
  • 55
vipul
  • 426
  • 1
  • 5
  • 13

4 Answers4

54

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.

Denis
  • 6,313
  • 1
  • 28
  • 27
  • i am also doint the same thing but it doesnot work....Any other suggestions if possible....thanks in advance... – vipul Nov 24 '11 at 13:13
  • maybe you can post your code in your question, so it will be easy to guess what's wrong =) – Denis Nov 24 '11 at 13:59
  • I have updated my code in question and this will not centered the mapView to currentLocation.Please check it and help me....thanks in advance. – vipul Nov 25 '11 at 09:58
  • 12
    if you're trying to set this from viewDidLoad: it will fail, because map view doesn't know yet the user location (it needs some time to receive response from GPS); so it will be correct to call [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES] from the UIMapViewDelegate method - (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation – Denis Nov 25 '11 at 10:02
  • How do I add zoom in to user location? – SleepNot Mar 14 '14 at 12:12
  • i added profile pic on current location but it is not coming in center. – DJtiwari May 13 '16 at 08:26
  • Swift 4: `mapView.setCenter(mapView.userLocation.coordinate, animated: true)` – Jan Erik Schlorf Mar 22 '18 at 13:11
2
MKCoordinateRegion region;
    MKCoordinateSpan span;
    span.latitudeDelta = 0.2;     // 0.0 is min value u van provide for zooming
    span.longitudeDelta= 0.2;

    CLLocationCoordinate2D location = [self addressLocation];
    region.span=span;
    region.center =location;     // to locate to the center
    if(addAnnotation != nil) {
        [mapView removeAnnotation:addAnnotation];
        [addAnnotation release];
        addAnnotation = nil;
    }
    addAnnotation = [[AddressANnotation alloc] initWithCoordinate:location];
    [mapView addAnnotation:addAnnotation];
    [mapView setRegion:region animated:TRUE];
    [mapView regionThatFits:region];

This had help me to show the position at the center of the map view.

iamsult
  • 1,581
  • 1
  • 15
  • 21
0
-(void) removeZoom:(float)deltaValue
{
    MKCoordinateRegion region;
    region.center.latitude =  self.locationManager.location.coordinate.latitude;
    region.center.longitude = self.locationManager.location.coordinate.longitude;

    [UIView beginAnimations:nil context:nil];
    [UIView setAnimationDuration:0.5];
    region.span.latitudeDelta = deltaValue;
    region.span.longitudeDelta = deltaValue;
    region = [mapViewSelf regionThatFits:region];
    [mapViewSelf setRegion:region animated:YES];
    [UIView commitAnimations];

}
-1

for MKMapView

self.mapView.showsUserLocation = YES;
self.mapView.userTrackingMode = MKUserTrackingModeFollow;
Baryon Lee
  • 1,157
  • 11
  • 11