用户位置未显示

时间:2015-12-23 13:11:27

标签: ios objective-c mkmapview

我是ios编程的新手。我在尝试显示一个显示当前用户位置的图钉的地图时遇到了问题。

@implementation ViewController 
@synthesize mapView;

- (void)viewDidLoad {
    [super viewDidLoad];
    // Do any additional setup after loading the view.

    self.locationManager = [[CLLocationManager alloc]init];
    self.locationManager.delegate = self;
    if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
        [self.locationManager requestWhenInUseAuthorization];
    }
    CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

    if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
        authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
        authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

        [self.locationManager startUpdatingLocation];
        mapView.showsUserLocation = YES;
        [self.mapView delegate];

    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

@end

info.plist具有NSLocationWhenIsUseUsageDescription属性,但它没有显示引脚,也没有缩放到用户位置。

2 个答案:

答案 0 :(得分:0)

请尝试使用此代码,您要求它开始更新if块中的位置,这对我来说似乎不对。

而且我也不确定你想在if块中使用这一行做什么。

除此之外,您的方法可能存在多个问题。我建议您查看一个很好的教程。

[self.mapView委托];

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
    [self.locationManager startUpdatingLocation];

if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}

CLAuthorizationStatus authorizationStatus= [CLLocationManager authorizationStatus];

if (authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedAlways ||
    authorizationStatus == kCLAuthorizationStatusAuthorizedWhenInUse) {

    mapView.showsUserLocation = YES;
    [self.mapView delegate];

}

答案 1 :(得分:0)

您可能希望查看didUpdateUserLocation方法。在您要求地图显示用户位置后,可能需要一些时间来回复。当系统返回有关用户位置的数据时,将访问didUpdateUserLocation方法。现在,只要您拥有用户位置,就可以didUpdateUserLocation放置代码以执行您想要对地图执行的任何操作。

相关问题