The location function of Location Kit depends on the device location settings. For example, if the location function is disabled on a device, your app cannot obtain the device location. Therefore, it is recommended that your app check whether the device settings meet the location requirements before obtaining the device location.
BOOL result = [HMSLocationManager locationServicesEnabled]; NSString *resString = [NSString stringWithFormat:@"Current device%@Support the location function\n",result?@"":@"No"];
BOOL result = [CLLocationManager headingAvailable]; NSString *resString = [NSString stringWithFormat:@"Current device%@Support compass-based navigation\n",result?@"":@"No"];
The following describes how to request a single location.
[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];
The following describes how to request locations continuously.
[[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];
The following describes how to request background locations.
[[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];
The following describes how to request heading information.
[[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];