目录

Developing the Fused Location Service

Importing the Header File

#import <HMSLocation/HMSLocation.h>

Checking the Device Location Settings and Related Capabilities

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.

  1. Call the locationServicesEnabled API to check the device location capability.
    BOOL result = [HMSLocationManager locationServicesEnabled];
    NSString *resString = [NSString stringWithFormat:@"Current device%@Support the location function\n",result?@"":@"No"]; 
  2. Call the following code to check the device heading capability:
    BOOL result = [CLLocationManager headingAvailable];
    NSString *resString = [NSString stringWithFormat:@"Current device%@Support compass-based navigation\n",result?@"":@"No"];

Requesting a Single Location

The following describes how to request a single location.

  1. Set single location request parameters.
    [HMSLocationManager sharedInstance].delegate = self;
    [[HMSLocationManager sharedInstance] setDesiredAccuracy:kCLLocationAccuracyBest];
    [[HMSLocationManager sharedInstance] setDistanceFilter:kCLDistanceFilterNone];
  2. Use a singleton of the manager class to initiate a single location request, receive location and error information in block, and process the service.
     
    __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. Stop requesting a single location.
    [[HMSLocationManager sharedInstance] stopUpdatingLocation];

Requesting Locations Continuously

The following describes how to request locations continuously.

  1. Use a singleton of the manager class to initiate a continuous location request.
    [[HMSLocationManager sharedInstance] startUpdatingLocation];
  2. Handle the result in the corresponding delegate method.
    - (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateLocation:(HMSLoc *)location {
        [self doSomethingWith:location];
    }
  3. Capture errors.
    - (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. Stop requesting locations continuously.
    [[HMSLocationManager sharedInstance] stopUpdatingLocation];

Requesting Background Locations

The following describes how to request background locations.

  1. Enable the background location permission. Then, use a singleton of the manager class to initiate a location request when the app is running in the background, or initiate a location request and then switch the app to the background.
    [[HMSLocationManager sharedInstance] setAllowsBackgroundLocationUpdates:YES];
    [[HMSLocationManager sharedInstance] startUpdatingLocation];
  2. Handle the result in the corresponding delegate method.
    - (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateLocation:(HMSLoc *)location {
        [self doSomethingWith:location];
    }
  3. Capture errors.
    - (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. Stop requesting background location.
    [[HMSLocationManager sharedInstance] stopUpdatingLocation];

Requesting Heading Information

The following describes how to request heading information.

  1. Use a singleton of the manager class to initiate a heading information acquisition request.
    [[HMSLocationManager sharedInstance] startUpdatingHeading];
  2. Handle the result in the corresponding delegate method.
    - (void)HMSLocationManager:(HMSLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
        [self doSomethingWith:newHeading];
    }
  3. Capture errors.
    - (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. Stop requesting heading information.
    [[HMSLocationManager sharedInstance] stopUpdatingHeading];