如何在 iOS 8 中制作一个按钮来显示位置?

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

这似乎是一个非常基本的问题,但我没有找到任何东西,而且我已经试过了!!我只想做一个按钮来放大用户的位置!这是我到目前为止所得到的:

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];

}

拜托,我需要帮助,如果有我不使用的方法CLLocationManager,那会更好。

谢谢。

您仍然需要CLLocationManager向用户请求位置许可, 但您可以使用 MKMapViewsetUserTrackingMode 方法让地图视图以用户位置为中心。

如果您不想在用户移动时继续更新地图位置,那么您应该将跟踪模式设置为 Follow,然后在几秒钟后将其设置为 None