Take user to location when they get their location

33 views Asked by At

I have code that gets user location and displays it on the mapView, but they have to zoom out and find themselves on the map, what I want is when the button is pressed it automatically takes them to where they are. Heres my code so far:

- (void)viewDidLoad
{
    [super viewDidLoad];


    MKCoordinateRegion bombersRegion = { {0.0,0.0}, {0.0,0.0} };
    bombersRegion.center.latitude = 42.812548;
    bombersRegion.center.longitude = -73.940815;
    bombersRegion.span.latitudeDelta = 0.01f;
    bombersRegion.span.longitudeDelta = 0.01f;
    [bombersMapView setRegion:bombersRegion animated:YES];


    MapPin *ann = [[MapPin alloc] init];
    ann.title = @"Bombers Burrito Bar";
    ann.subtitle = @"447 State Street Schenectady, New York 12305";
    ann.coordinate = bombersRegion.center;
    [bombersMapView addAnnotation:ann];

}


-(IBAction)getLocation:(id)sender{
    bombersMapView.showsUserLocation = YES;


}

Thanks for the help.

I would really appreciate some sample code.

2

There are 2 answers

0
Craig On BEST ANSWER

If you want the the map to stay centered on user's location try this

[bombersMapView setUserTrackingMode:MKUserTrackingModeFollow animated:YES];
0
Milo On

Take a look at this MKMapViewDelegate method. This will give you the users location which you can use to zoom in on. The method you're looking for is mapView:didUpdateUserLocation:.