设备的相关定位设置会影响定位服务的定位结果,例如“定位服务”开关处于关闭状态时,将无法获取到位置信息。因此,建议您在获取位置信息之前,先检查设备设置是否满足定位条件。
BOOL result = [HMSLocationManager locationServicesEnabled]; NSString *resString = [NSString stringWithFormat:@"当前设备%@支持定位服务\n",result?@"":@"不"];
BOOL result = [CLLocationManager headingAvailable]; NSString *resString = [NSString stringWithFormat:@"当前设备%@支持罗盘相关航向\n",result?@"":@"不"];
下面代码示例将展示单次定位功能的使用方法。
[HMSLocationManager sharedInstance].delegate = self; [[HMSLocationManager sharedInstance] setDesiredAccuracy:kCLLocationAccuracyBest]; [[HMSLocationManager sharedInstance] setDistanceFilter:kCLDistanceFilterNone];
__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];
}
}]; [[HMSLocationManager sharedInstance] stopUpdatingLocation];
下面代码示例将展示连续定位功能的使用方法。
[[HMSLocationManager sharedInstance] startUpdatingLocation];
- (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateLocation:(HMSLoc *)location {
[self doSomethingWith:location];
} - (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];
} [[HMSLocationManager sharedInstance] stopUpdatingLocation];
下面代码示例将展示后台定位功能的使用方法。
[[HMSLocationManager sharedInstance] setAllowsBackgroundLocationUpdates:YES]; [[HMSLocationManager sharedInstance] startUpdatingLocation];
- (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateLocation:(HMSLoc *)location {
[self doSomethingWith:location];
} - (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];
} [[HMSLocationManager sharedInstance] stopUpdatingLocation];
下面代码示例将展示朝向获取功能的使用方法。
[[HMSLocationManager sharedInstance] startUpdatingHeading];
- (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
[self doSomethingWith:newHeading];
} - (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];
} [[HMSLocationManager sharedInstance] stopUpdatingHeading];