How to make a button to show location in iOS 8?

44 views Asked by At

This seems like very basic question but I didnt find anything, and I have tried all!! I just want to make a button to zoom in into users location! This is what I've got so far:

CLLocationCoordinate2D userLocationCoordinate;

- (void)viewDidLoad {
    [super viewDidLoad];
    [[UIApplication sharedApplication] setStatusBarHidden:YES withAnimation:UIStatusBarAnimationSlide];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
        [self.locationmanager requestWhenInUseAuthorization];}
    [locationManager startUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
    NSLog(@"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
    NSLog(@"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
    userLocationCoordinate.latitude = newLocation.coordinate.latitude;
    userLocationCoordinate.longitude = newLocation.coordinate.longitude;

}

-(IBAction)showUserLocation:(id)sender{

    [self.mapview setCenterCoordinate:userLocationCoordinate animated:YES];

}

Please I need help, If theres a way in which I dont use CLLocationManager it would be better.

Thanks.

1

There are 1 answers

2
Paulw11 On BEST ANSWER

You still need CLLocationManager to request location permission from the user, but you can use the setUserTrackingMode method of MKMapView to ask the map view to centre on the user's location.

If you don't want to continue to update the map location as the user moves then you should set the tracking mode to Follow and then set it to None after a few seconds.