我在目标 C 中显示用户位置时遇到问题。我尝试了在 stackoverflow 中可以找到的所有内容,aaaand,但没有成功。
所以我有那个代码:
-(void)setLocation
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
MKPointAnnotation *myAnnotation = [[MKPointAnnotation alloc]init];
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
locationManager.distanceFilter = 10.0;
[locationManager startUpdatingLocation];
[locationManager requestWhenInUseAuthorization];
[locationManager requestAlwaysAuthorization];
myAnnotation.coordinate = mapView.userLocation.location.coordinate;
myAnnotation.title = @"Test";
myAnnotation.subtitle = @"I am a test Subtitle";
[self.mapView addAnnotation:myAnnotation];
}
-(void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation {
[self.mapView setCenterCoordinate:userLocation.coordinate animated:YES];
}
一切都在 ViewController.m 中,更准确地说是在我的 .h 文件中声明的 mapView 中:
@property (strong, nonatomic) IBOutlet MKMapView *mapView;
有人有什么主意吗 ?我的错误是:
Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.
谢谢 :)