【发布时间】:2016-07-04 19:30:27
【问题描述】:
必须先致电-[CLLocationManager requestWhenInUseAuthorization] 或-[CLLocationManager requestAlwaysAuthorization]。实际上我已经在 Didload 中调用了这个方法,甚至在info.plist 中添加了这个方法,仍然出现这个错误。
- (void)viewDidLoad {
[super viewDidLoad];
self.navigationItem.title = @"MapView";
[MapView setMapType:MKMapTypeStandard];
[MapView setZoomEnabled:YES];
[MapView setScrollEnabled:YES];
[MapView setShowsUserLocation:YES];
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = 23.0804 ;
region.center.longitude = 72.5241;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[MapView setRegion:region animated:YES];
[MapView setDelegate:self];
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager requestAlwaysAuthorization];
[locationManager requestWhenInUseAuthorization];
[locationManager startUpdatingLocation];
Display *ann = [[Display alloc] init];
ann.title = @" Gujarat";
ann.subtitle = @"High Court";
ann.coordinate = region.center;
[MapView addAnnotation:ann];
}
-(MKAnnotationView *)mapView:(MKMapView *)mV viewForAnnotation:
(id <MKAnnotation>)annotation {
MKPinAnnotationView *pinView = nil;
if(annotation != MapView.userLocation)
{
static NSString *defaultPinID = @"com.invasivecode.pin";
pinView = (MKPinAnnotationView *)[MapView dequeueReusableAnnotationViewWithIdentifier:defaultPinID];
if ( pinView == nil ) pinView = [[MKPinAnnotationView alloc]
initWithAnnotation:annotation reuseIdentifier:defaultPinID];
pinView.pinColor = MKPinAnnotationColorRed;
pinView.canShowCallout = YES;
pinView.animatesDrop = YES;
pinView.draggable = YES;
}
else {
[MapView.userLocation setTitle:@"I am here"];
}
return pinView;
}
-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
if (status == kCLAuthorizationStatusAuthorizedWhenInUse)
{
[locationManager startUpdatingLocation];
}
else if (status == kCLAuthorizationStatusDenied)
{
//Alert to show for user if any when status is declined
}
else
NSLog(@"Wrong location status");
}
并在 Annotation 的 Display 类中
<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
这里的代码没有显示提示用户是否允许访问其位置,因此我们无法获得所需位置的位置。请帮我从昨天开始。
【问题讨论】:
-
仍然收到此错误。 ...?
-
是的还是不知道我卡在哪里了
-
调用 -[CLLocationManager requestWhenInUseAuthorization] 或 -[CLLocationManager requestAlwaysAuthorization],但不能同时调用,因为它没有意义
-
由于 nick 的回答由于某种原因对您没有帮助,请提供简化的示例应用程序以便我们保存
标签: ios objective-c dictionary