Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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];
}
like image 484
vipul Avatar asked Nov 24 '11 11:11

vipul


2 Answers

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.

like image 130
Denis Avatar answered Sep 22 '22 14:09

Denis


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.

like image 23
iamsult Avatar answered Sep 19 '22 14:09

iamsult