9

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

Jan
  • 119
  • 1
  • 1
  • 4

5 Answers5

11

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];
}
Johannes Rudolph
  • 35,298
  • 14
  • 114
  • 172
Bushra Shahid
  • 3,579
  • 1
  • 27
  • 37
2

you can also try:

mapView.userTrackingMode=YES;
mapView.userTrackingMode=NO;
Jonathan Gurebo
  • 285
  • 1
  • 6
  • 19
  • Just make sure to call this in the viewDidAppear method or later. – lehn0058 Feb 21 '13 at 15:40
  • 1
    `userTrackingMode` is not a `BOOL`. It's a `MKUserTrackingMode` enum. Only reason setting it to `YES`/`NO` "works" is because `YES` is equal to `MKUserTrackingModeFollow` and `NO` is equal to `MKUserTrackingModeNone`. But `userTrackingMode` can also be a third value `MKUserTrackingModeFollowWithHeading`. –  Dec 03 '14 at 13:25
1

You can link this IBAction to your UIButton, it's going to move the map on the current location and zoom on it.

@IBOutlet weak var mapView: MKMapView!

@IBAction func zoomToUserCurrentLocation(sender: AnyObject) {
    if self.mapView != nil {
        self.mapView.setRegion(MKCoordinateRegionMake(
            self.mapView.userLocation.coordinate, 
            MKCoordinateSpanMake(0.1, 0.1)
        ), animated: true)
    }
}

MKCoordinateSpan defines the area spanned by a map region, smaller these values are, closer you zoom on the map.

Philippe
  • 1,567
  • 16
  • 18
0
- (void)showCurrentLocation{

    MKMapPoint annotationPoint = MKMapPointForCoordinate(self.mapView.userLocation.coordinate);
    MKMapRect zoomRect = MKMapRectMake(annotationPoint.x, annotationPoint.y, 0.0, 0.0);
    [self.mapView setVisibleMapRect:zoomRect animated:YES];
}
pvllnspk
  • 5,667
  • 12
  • 59
  • 97
0

FOR SWIFT

Add this line in button action yourMKMapView.setUserTrackingMode(.follow, animated: true)

make sure you add yourMKMapView.showsUserLocation = true in viewDidLoad()