我是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属性,但它没有显示引脚,也没有缩放到用户位置。
答案 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
放置代码以执行您想要对地图执行的任何操作。