目录

融合定位开发

导入头文件

#import <HMSLocation/HMSLocation.h>

检查设备定位设置与相关能力

设备的相关定位设置会影响定位服务的定位结果,例如“定位服务”开关处于关闭状态时,将无法获取到位置信息。因此,建议您在获取位置信息之前,先检查设备设置是否满足定位条件。

  1. 调用locationServicesEnabled接口检查设备定位能力。
    BOOL result = [HMSLocationManager locationServicesEnabled];
    NSString *resString = [NSString stringWithFormat:@"当前设备%@支持定位服务\n",result?@"":@"不"]; 
  2. 调用下方代码检查设备朝向能力。
    BOOL result = [CLLocationManager headingAvailable];
    NSString *resString = [NSString stringWithFormat:@"当前设备%@支持罗盘相关航向\n",result?@"":@"不"];

调用单次定位能力

下面代码示例将展示单次定位功能的使用方法。

  1. 设置单次定位请求关联属性
    [HMSLocationManager sharedInstance].delegate = self;
    [[HMSLocationManager sharedInstance] setDesiredAccuracy:kCLLocationAccuracyBest];
    [[HMSLocationManager sharedInstance] setDistanceFilter:kCLDistanceFilterNone];
  2. 使用管理类单例发起单次定位请求,在block中接收位置信息与错误信息,并进行业务处理。
     
    __weak __typeof(self) weakSelf = self;
    [[HMSLocationManager sharedInstance] requestLocationWithCompletionBlock:^(HMSLoc * _Nullable hmsLocation, NSError * _Nullable error) {
        __typeof(weakSelf) strongSelf = weakSelf;
        if (error == nil) {
            [strongSelf doSomething];
        } else {
            [strongSelf doSomethingWithError:error];
        } 
    }];
  3. 停止正在进行的单次定位。
    [[HMSLocationManager sharedInstance] stopUpdatingLocation];

调用连续定位能力

下面代码示例将展示连续定位功能的使用方法。

  1. 使用管理类单例发起连续定位请求。
    [[HMSLocationManager sharedInstance] startUpdatingLocation];
  2. 在对应的代理方法中处理结果。
    - (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateLocation:(HMSLoc *)location {
        [self doSomethingWith:location];
    }
  3. 捕获错误。
    - (void)HMSLocationManager:(HMSLocationManager *)manager didFailWithError:(NSError *)error {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"Error%ld:%@ Occurred",error.code,error.domain] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // Todo: Perform some processing after the user clicks "Confirm"        
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
     }
  4. 停止连续定位。
    [[HMSLocationManager sharedInstance] stopUpdatingLocation];

调用后台定位能力

下面代码示例将展示后台定位功能的使用方法。

  1. 开启后台定位权限,在应用处于后台时,使用管理类单例发起定位请求或者发起定位请求后将应用切换至后台。
    [[HMSLocationManager sharedInstance] setAllowsBackgroundLocationUpdates:YES];
    [[HMSLocationManager sharedInstance] startUpdatingLocation];
  2. 在对应的代理方法中处理结果。
    - (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateLocation:(HMSLoc *)location {
        [self doSomethingWith:location];
    }
  3. 捕获错误。
    - (void)HMSLocationManager:(HMSLocationManager *)manager didFailWithError:(NSError *)error {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"Error%ld:%@ Occurred",error.code,error.domain] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // Todo: Perform some processing after the user clicks "Confirm"        
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
     }
  4. 停止后台定位。
    [[HMSLocationManager sharedInstance] stopUpdatingLocation];

调用朝向获取能力

下面代码示例将展示朝向获取功能的使用方法。

  1. 使用管理类单例发起朝向获取请求。
    [[HMSLocationManager sharedInstance] startUpdatingHeading];
  2. 在对应的代理方法中处理结果。
    - (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
        [self doSomethingWith:newHeading];
    }
  3. 捕获错误。
    - (void)HMSLocationManager:(HMSLocationManager *)manager didFailWithError:(NSError *)error {
        UIAlertController *alert = [UIAlertController alertControllerWithTitle:nil message:[NSString stringWithFormat:@"Error%ld:%@ Occurred",error.code,error.domain] preferredStyle:UIAlertControllerStyleAlert];
        UIAlertAction *action = [UIAlertAction actionWithTitle:@"Confirm" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
            // Todo: Perform some processing after the user clicks "Confirm".        
        }];
        [alert addAction:action];
        [self presentViewController:alert animated:YES completion:nil];
     }
  4. 停止朝向获取。
    [[HMSLocationManager sharedInstance] stopUpdatingHeading];