iOS8 地图定位

 在xcode6中 苹果地图得定位方法修改了,以前得不能用

 首先在在Supporting Files下得Info.plis里面添加NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 两个String字段,字段不能为空

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface ViewController ()<CLLocationManagerDelegate>
@property (nonatomic,strong) CLLocationManager  *locMgr;
@end

@implementation ViewController
-(CLLocationManager *)locMgr
{
    if (_locMgr == nil) {
        // 1.创建位置管理器(定位用户的位置)
        self.locMgr = [[CLLocationManager alloc] init];
        
        // 2.设置代理
        self.locMgr.delegate = self;
    }
    return _locMgr;
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    if ([CLLocationManager locationServicesEnabled]) { //定位服务打开
        if ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0){
            if ([CLLocationManager authorizationStatus]==kCLAuthorizationStatusNotDetermined){
                // 开始定位用户的位置
                [self.locMgr startUpdatingLocation]; 
            }
        }
    }
    else
    {
        [self.locMgr startUpdatingLocation];
    }
}



#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *loc=[locations firstObject];

    CLLocationCoordinate2D coordinate=loc.coordinate;
    
    NSLog(@"经度:%f,纬度:%f,海拔:%f,航向:%f,行走速度:%f",coordinate.longitude,coordinate.latitude,loc.altitude,loc.course,loc.speed);

     [_locMgr stopUpdatingLocation];
}

/**
 *    __IPHONE_4_2  Invoked when the authorization status changes for this application.
 */
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status
{
    // kCLAuthorizationStatusAuthorizedWhenInUse 4 用户获得授权使用他们的位置,只有当你的应用程序是可见的(它将看到他们如果你继续接收位置更新在背景)。授权使用发布api没有被授予。
    // kCLAuthorizationStatusAuthorizedAlways 3 用户在任何时候获得授权使用其位置,包括监测区域,访问,或显著位置的变化。
    // kCLAuthorizationStatusNotDetermined 0 关于这个应用程序用户尚未选择
    // kCLAuthorizationStatusDenied 2 用户已经明确拒绝授权对于这个应用程序,或者在设置位置服务被禁用。
    switch (status) {
        case kCLAuthorizationStatusDenied:
        {
            UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"请在系统设置中打开\"定位服务\"来允许\"百度地图\"确定您的位置" message:nil delegate:nil cancelButtonTitle:@"知道了" otherButtonTitles: nil];
            [alertView show];
        }
            break;
            
        case kCLAuthorizationStatusNotDetermined:
        {
            if ([self.locMgr respondsToSelector:@selector(requestAlwaysAuthorization)])
            {
                [self.locMgr requestWhenInUseAuthorization];
                [self.locMgr startUpdatingLocation];
            }
        }
            break;
        case kCLAuthorizationStatusRestricted:
        {
            UIAlertView *tempA = [[UIAlertView alloc]initWithTitle:@"提醒" message:@"定位服务无法使用!" delegate:nil cancelButtonTitle:@"确定" otherButtonTitles:nil, nil];
            [tempA show];
        }
            break;
        default:
            break;
    }
}

@end

 

遇到的错误和解决方法:

错误:Terminating app due to uncaught exception 'NSInvalidUnarchiveOperationException', reason: 'Could not instantiate class named MKMapView'

 解决方法:添加MapKit.framework

错误:Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

解决办法:

在Supporting Files下得Info.plis里面添加NSLocationAlwaysUsageDescription 或 NSLocationWhenInUseUsageDescription 两个String字段,

If the NSLocationAlwaysUsageDescription key is not specified in your Info.plist, this method will do nothing, as your app will be assumed not to support Always authorization.

 
 
 
 
posted @ 2015-04-30 15:57  浅忆0  阅读(262)  评论(0编辑  收藏  举报