Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Go to/zoom to current location function (MapKit)

I've got a mapView which zooms to the current location using viewDidLoad :

#define METERS_PER_MILE 1609.344

// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    [super viewDidLoad];

    mapView.showsUserLocation=TRUE;

    // zoom to  a specific area
    CLLocationCoordinate2D zoomLocation;
    zoomLocation.latitude = -28.994167;
    zoomLocation.longitude = 134.866944;

    MKCoordinateRegion viewRegion = MKCoordinateRegionMakeWithDistance(zoomLocation, 1900*METERS_PER_MILE, 1900*METERS_PER_MILE);
    MKCoordinateRegion adjustedRegion = [mapView regionThatFits:viewRegion];    

    // make sure the Google water mark is always visible
    mapView.autoresizingMask =
    (UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight);

    [mapView setRegion:adjustedRegion animated:YES];        

    mapView.delegate=self;

    searchBar.delegate = self;
}

This works fine. I've added a search bar and a function to jump to a specific address location. This works fine, too. I now want to add a button to jump back to the current location. Can you give me a hand, please?

Cheers

like image 770
Jan Avatar asked Aug 11 '11 06:08

Jan


2 Answers

You need to set the center of your map to the current location on tap of that button. Say, like this:

- (IBAction)showCurrentLocation {        
    [mapView setCenterCoordinate:mapView.userLocation.location.coordinate animated:YES];
}
like image 95
Bushra Shahid Avatar answered Oct 01 '22 10:10

Bushra Shahid


you can also try:

mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
like image 21
Jonathan Gurebo Avatar answered Oct 01 '22 09:10

Jonathan Gurebo