iOS 百度地图定位

关于配置官方文档写的很全,简单描述2点

1、建议pod


platform :ios, '7.0'
target 'myApp' do  #工程名字
  pod 'BMKLocationKit'
end

2、plist.info 配置


NSLocationWhenInUseUsageDescription:表示应用在前台的时候可以搜到更新的位置信息;
NSLocationAlwaysUsageDescription:表示应用在前台和后台(suspend 或 terminated)都可以获取到更新的位置数据;
NSLocationAlwaysAndWhenInUseUsageDescription:申请永久定位权限,以便应用在前台和后台都可以获取位置数据;

直接上代码

引头文件


#import 

添加属性, 配置授权代理,持续定位代理


@interface AppDelegate ()

@property (nonatomic, strong) BMKLocationManager *locationManager;
@property (nonatomic ,assign) BMKLocatingCompletionBlock completionBlock;

@end

配置百度地图key,并且初始信息,这里采取的是 百度推荐的 kCLLocationAccuracyHundredMeters ,偏差在百米左右,超时时间设置在2s-3s左右即可,allowsBackgroundLocationUpdates 此时设置NO(不允许后台定位)

// 地图配置
- (void)setLocationConfig
{
    // 百度地图key
    [[BMKLocationAuth sharedInstance] checkPermisionWithKey:@"百度开发者申请的地图key" authDelegate:self];
    
    //初始化实例
    _locationManager = [[BMKLocationManager alloc] init];
    //设置delegate
    _locationManager.delegate = YES;
    //设置返回位置的坐标系类型
    _locationManager.coordinateType = BMKLocationCoordinateTypeBMK09LL;
    //设置距离过滤参数
    _locationManager.distanceFilter = kCLDistanceFilterNone;
    //设置预期精度参数
    _locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
    //设置应用位置类型
    _locationManager.activityType = CLActivityTypeAutomotiveNavigation;
    //设置是否自动停止位置更新
    _locationManager.pausesLocationUpdatesAutomatically = NO;
    //设置是否允许后台定位
    _locationManager.allowsBackgroundLocationUpdates = NO;
    //设置位置获取超时时间
    _locationManager.locationTimeout = 2;
    //设置获取地址信息超时时间
    _locationManager.reGeocodeTimeout = 2;
    
}

单次定位block


// 单次定位block
- (void)singleLocationBlock
{
    
    self.completionBlock = ^(BMKLocation *location, BMKLocationNetworkState state, NSError *error)
    {
        if (error)
        {
            NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
        }
        if (location) {//得到定位信息,添加annotation
            
            if (location.location) {
                NSLog(@"LOC = %@",location.location);
            }
            if (location.rgcData) {
                NSLog(@"rgc = %@",[location.rgcData description]);
            }
            
            if (location.rgcData.poiList) {
                for (BMKLocationPoi * poi in location.rgcData.poiList) {
                    NSLog(@"poi = %@, %@, %f, %@, %@", poi.name, poi.addr, poi.relaiability, poi.tags, poi.uid);
                }
            }
            
            if (location.rgcData.poiRegion) {
                NSLog(@"poiregion = %@, %@, %@", location.rgcData.poiRegion.name, location.rgcData.poiRegion.tags, location.rgcData.poiRegion.directionDesc);
            }
            
        }
    };
}

持续定位代理


- (void)BMKLocationManager:(BMKLocationManager * _Nonnull)manager didUpdateLocation:(BMKLocation * _Nullable)location orError:(NSError * _Nullable)error

{
    if (error)
    {
        NSLog(@"locError:{%ld - %@};", (long)error.code, error.localizedDescription);
    } if (location) {//得到定位信息,添加annotation
        
        if (location.location) {
            NSLog(@"LOC = %@",location.location);
        }
        if (location.rgcData) {
            NSLog(@"rgc = %@",[location.rgcData description]);
        }
        
        if (location.rgcData.poiList) {
            for (BMKLocationPoi * poi in location.rgcData.poiList) {
                NSLog(@"poi = %@, %@, %f, %@, %@", poi.name, poi.addr, poi.relaiability, poi.tags, poi.uid);
            }
        }
        
        if (location.rgcData.poiRegion) {
            NSLog(@"poiregion = %@, %@, %@", location.rgcData.poiRegion.name, location.rgcData.poiRegion.tags, location.rgcData.poiRegion.directionDesc);
        }
        
    }
}

单次定位方法


// 单次定位
- (void)singleLocationMethod
{
    [self.locationManager requestLocationWithReGeocode:YES withNetworkState:YES completionBlock:self.completionBlock];
}

持续定位方法


// 持续定位
- (void)continuedLocationMethod
{
    // 持续定位返回地址信息
    [self.locationManager setLocatingWithReGeocode:YES];
    [self.locationManager startUpdatingLocation];
}

didFinishLaunchingWithOptions调用单次定位或者持续定位方法

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
    // Override point for customization after application launch.

    --------------------
    其他初始化代码
    --------------------
    
    // 地图配置
    [self setLocationConfig];
    // 单次定位block
    [self singleLocationBlock];
    // 单次定位
    [self singleLocationMethod];
    
    return YES;
}

如果定位配置信息里面的 _locationManager.allowsBackgroundLocationUpdates = NO;(不允许后台定位)

  • 成功获取定位信息

如果定位配置信息里面的 _locationManager.allowsBackgroundLocationUpdates = YES;(允许后台定位)

  • 配置1个 出现报错,得不到定位信息


    截屏2021-07-30 14.35.09.png
  • 配置3个 出现报错,得不到定位信息


    截屏2021-07-30 14.33.51.png
  • 配置1或者3个都会出现报错,并且得不到定位信息

截屏2021-07-30 14.30.42.png

解决方法

方法1、Targets -> Siging&Capability - >Background Modes 勾选Location updates, 但是开启后台定位模式。此选项App审核会被拒,需要说明后台使用定位的原因

截屏2021-07-30 14.47.53.png
方法 1 请注意,程序切换到后台,如果是持续定位,打印台每过几秒就会地址打印信息

方法2、创建 CLLocationManager分类, pch文件直接导入即可

#import "CLLocationManager+Extension.h"
#import 
@implementation CLLocationManager (Extension)

+ (void)load {
    if ([UIDevice currentDevice].systemVersion.floatValue >= 9.0) {
        method_exchangeImplementations(class_getInstanceMethod(self.class, NSSelectorFromString(@"setAllowsBackgroundLocationUpdates:")), class_getInstanceMethod(self.class, @selector(swizzledSetAllowsBackgroundLocationUpdates:)));
    }
}

- (void)swizzledSetAllowsBackgroundLocationUpdates:(BOOL)allow {
    if (allow) {
        NSArray *backgroundModes = [[NSBundle mainBundle].infoDictionary objectForKey:@"UIBackgroundModes"];
        if( backgroundModes && [backgroundModes containsObject:@"location"]) {
            [self swizzledSetAllowsBackgroundLocationUpdates:allow];
        } else {
            NSLog(@"APP想设置后台定位,但APP的info.plist里并没有申请后台定位");
        }
    } else {
        [self swizzledSetAllowsBackgroundLocationUpdates:allow];
    }
}
@end

方法 2 请注意,程序切换到后台,如果是持续定位,打印台不会出现地址打印信息,但是切回前台会直接出现地址打印信息

你可能感兴趣的:(iOS 百度地图定位)