必须先致电-[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;
此处的代码未显示用户访问其位置的提示,因此我们无法获取所需位置的位置。亲爱的,从昨天开始帮助我。
答案 0 :(得分:2)
检查您应用中的以下内容。
1)创建CLLocation Manager的类对象。 如果您创建CLLocation管理器的本地对象,则在用户响应对话框之前释放CLLocationManager对象。
2)转到设置&gt;隐私&gt;位置服务。并检查您的应用是否列在位置服务列表中。如果是,则点按您的应用程序并进入内部检查开关是否打开。
3)使用
检查是否支持requestAlwaysAuthorization :.if ([_locationManager respondsToSelector:@selector(requestAlwaysAuthorization)]) {
[_locationManager requestAlwaysAuthorization];
}
4)尝试使用
检查位置管理器的状态-(void)checkStatus{
CLAuthorizationStatus status = [CLLocationManager authorizationStatus];
if (status==kCLAuthorizationStatusNotDetermined) {
_status.text = @"Not Determined";
}
if (status==kCLAuthorizationStatusDenied) {
_status.text = @"Denied";
}
if (status==kCLAuthorizationStatusRestricted) {
_status.text = @"Restricted";
}
if (status==kCLAuthorizationStatusAuthorizedAlways) {
_status.text = @"Always Allowed";
}
if (status==kCLAuthorizationStatusAuthorizedWhenInUse) {
_status.text = @"When In Use Allowed";
}
}
有关详细信息,请访问
1)http://www.devfright.com/location-authorization-ios-8/
2)http://nshipster.com/core-location-in-ios-8/
答案 1 :(得分:1)
你需要做两件事:
将CLLocationManager *locationManager
声明为全局变量。随着范围结束,位置管理器将被释放。因此,一旦范围结束,弹出即可显示授权将隐藏。
[locationManager startUpdatingLocation]
。
@interface ViewController () <CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
@implementation ViewController
- (void)viewDidLoad {
[super viewDidLoad];
[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];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
if ([locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)])
{
[locationManager requestWhenInUseAuthorization];
}
Display *ann = [[Display alloc] init];
ann.title = @" Gujarat";
ann.subtitle = @"High Court";
ann.coordinate = region.center;
[MapView addAnnotation:ann];
}
-(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");
}
答案 2 :(得分:0)
我在 .plist 屏幕截图中可以看到的是
NSLocationAlwaysUsageDescription or NSLocationWhenInUseUsageDescription key in Info.plist
缺少。这是为了在您的应用首次启动时显示提示。 添加这些解决了问题。
答案 3 :(得分:0)