Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first. Actually i have called this method in Didload and even added in info.plist still getting this error.
- (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");
}
and in Display class for Annotation
<MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
}
@property (nonatomic, assign) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
the code here doesn't show the prompt for permission from user to access its location and because of this we don't get the location of desired place. Kindly, help me been on it since yesterday.
