All Products
Search
Document Center

Access iOS

Last Updated: May 25, 2021

The Location-based Service (LBS) SDK is a set of simple LBS interfaces. You can use this set of LBS API to obtain positioning results.

Prerequisite

The project already gets access to mPaaS. For more information, see the following content: Connect to mPaaS based on an existing project and CocoaPods.

Add the SDK

Use CocoaPods plugin to add the Location-based Service SDK. Complete the following steps:

  1. In the Podfile file, use mPaaS_pod "mPaaS_LBS" to add mobile gateway component dependencies.
  2. In the terminal, run pod install to complete access.

Use the SDK

This topic describes how to use the LBS SDK in baseline 10.1.32 later versions on the official LBS demo.

The APMobileLBS module provides a method for obtaining the latitude and longitude of the current location.

Note: The LBS does not support reverse geographic query. To use reverse geocoding, you can call the AMAP API.

API description

To learn LBS API operations, see the following code and the parameter descriptions in the comments.

Use MPLBSConfiguration to set parameters

 
  1. /**
  2. Configurations of the LBS
  3. */
  4. @interface MPLBSConfiguration : NSObject
  5. /** The expected precision of a single positioning operation, in meters. We recommend that you specify an acceptable positive number based on the business scenario. For example, set the parameter to 500 to indicate a range within 500 m. */
  6. @property (nonatomic, assign) CLLocationAccuracy desiredAccuracy;
  7. /** The time for caching received data of a single positioning operation, counting from the current time backward. We recommend that you specify a time longer than 30s. */
  8. @property (nonatomic, assign) APCoreLocationCacheAvaliable cacheTimeInterval;
  9. /** The timeout interval for a single positioning operation or reverse geographical query, in seconds. Default value: 2s. Minimum value: 2s. */
  10. @property (nonatomic, assign) NSTimeInterval timeOut;
  11. /** The level of the information obtained through reverse geographical query. Default value: APCoreLocationReGeoLevelDistrict. */
  12. @property (nonatomic, assign) LBSLocationReGeoLevel reGeoLevel;
  13. /** The location information obtained through reverse geographical query based on the latitude and longitude. */
  14. @property (nonatomic, strong) CLLocation *reGeoLocation;
  15. /** Specifies whether coordinates used for reverse geographical query are coordinates in AMAP. Default value: YES. This parameter is valid only when the reGeoLocation parameter is specified. */
  16. @property (nonatomic, assign) BOOL reGeoCoordinateConverted;
  17. /** Specifies whether to enable check-in. Default value: NO. If check-in is required, set this parameter to YES. */
  18. @property (nonatomic, assign) BOOL needCheckIn;
  19. /**
  20. * Specifies whether high-precision positioning is required. This parameter is not required for versions earlier than iOS 14. For iOS 14 and later, this parameter is set to NO (low precision) by default. If high-precision positioning is required, the business personnel must modify this parameter.
  21. */
  22. @property (nonatomic,assign) BOOL highAccuracyRequired;
  23. @end

Use MPLBSLocationManager to initiate a positioning request

 
  1. /**
  2. Callback blocks that are triggered based on positioning results
  3. @param success Whether positioning is successful
  4. @param locationInfo Location information
  5. @param error Error information of positioning failure
  6. */
  7. typedef void(^MPLBSLocationCompletionBlock)(BOOL success,
  8. MPLBSLocationInfo *locationInfo,
  9. NSError *error);
  10. /**
  11. LBS
  12. */
  13. @interface MPLBSLocationManager : NSObject
  14. /**
  15. Initialize
  16. @param configuration Parameter settings
  17. @return Instance
  18. */
  19. - (instancetype)initWithConfiguration:(MPLBSConfiguration *)configuration;
  20. /**
  21. Initiate a single positioning operation
  22. @param needReGeocode Indicates whether reverse geographic query is required. The LBS does not support reverse geographic query. You must set this parameter to NO.
  23. @param block The callback block that is triggered after positioning ends.
  24. */
  25. - (void)requestLocationNeedReGeocode:(BOOL)needReGeocode
  26. completionHandler:(MPLBSLocationCompletionBlock)block;

MPLBSLocationInfo in callbacks

 
  1. /**
  2. Reverse geographic information
  3. */
  4. @interface MPLBSReGeocodeInfo : NSObject
  5. @property (nonatomic, strong) NSString* country; // The country.
  6. @property (nonatomic, strong) NSString* countryCode; // The country code.
  7. @property (nonatomic, strong) NSString* province; // The province.
  8. @property (nonatomic, strong) NSString* city; // The city.
  9. @property (nonatomic, strong) NSString* district; // The district.
  10. @property (nonatomic, strong) NSString* street; // The street.
  11. @property (nonatomic, strong) NSString* streetCode; // The street code.
  12. @property (nonatomic, strong) NSString* cityCode; // The city code.
  13. @property (nonatomic, strong) NSString* adCode; // The administrative district code.
  14. @property (nonatomic, strong) NSArray* poiList; // The POI information list.
  15. @end
  16. /**
  17. Data structure of location information in the positioning result
  18. */
  19. @interface MPLBSLocationInfo : NSObject
  20. @property (nonatomic, strong) CLLocation* location; // The location information.
  21. @property (nonatomic, strong) MPLBSReGeocodeInfo* rgcInfo; // The reverse geographic information.
  22. @end

Sample code

 
  1. - (void)getLocation {
  2. MPLBSConfiguration *configuration = [[MPLBSConfiguration alloc] init];
  3. configuration.desiredAccuracy = kCLLocationAccuracyBest;
  4. self.locationManager = [[MPLBSLocationManager alloc] initWithConfiguration:configuration];
  5. [self.locationManager requestLocationNeedReGeocode:NO completionHandler:^(BOOL success, MPLBSLocationInfo * _Nonnull locationInfo, NSError * _Nonnull error) {
  6. NSString *message;
  7. if (success) {
  8. message = [NSString stringWithFormat:@"Positioning success. Longitude: %.5f. Latitude: %.5f. Precision: %.3f. Whether high precision is required: %d", locationInfo.location.coordinate.longitude, locationInfo.location.coordinate.latitude, locationInfo.location.horizontalAccuracy, !locationInfo.location.ap_lbs_is_high_accuracy_close];
  9. } else {
  10. message = [NSString stringWithFormat:@"%@", error];
  11. }
  12. dispatch_async(dispatch_get_main_queue(), ^{
  13. AUNoticeDialog *alert = [[AUNoticeDialog alloc] initWithTitle:@"Positioning result" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  14. [alert show];
  15. });
  16. }];
  17. }

Adaptation in iOS 14

In iOS 14, precise location is a permission option. You can turn on the option when you apply for positioning permissions. On the positioning permissions setting page, you can adjust the setting, as shown in the following figure.

Adaptation of input parameters

In MPLBSConfiguration, add the highAccuracyRequired parameter. If highAccuracyRequired = YES is passed in, but high-precision positioning is disabled, a callback error occurs.

 
  1. /**
  2. Configurations of the LBS
  3. */
  4. @interface MPLBSConfiguration : NSObject
  5. /**
  6. * Specifies whether high-precision positioning is required. This parameter is not required for versions earlier than iOS 14. For iOS 14 and later, this parameter is set to NO (low precision) by default. If high-precision positioning is required, the business personnel must modify this parameter.
  7. */
  8. @property (nonatomic,assign) BOOL highAccuracyRequired;
  9. @end
 
  1. //If highAccuracyRequired = YES is passed in, but high-precision positioning is disabled, a callback error occurs.
  2. Errorcode: APCoreLocationErrorCodeHighAccuracyAuthorization

Callback adaptation

If highAccuracyRequired = NO is passed in or high-precision positioning is not specified, the callback object CLLocation contains the ap_lbs_is_high_accuracy_close field. This field indicates whether high-precision positioning is disabled.

 
  1. // Reconstructs the output parameter.
  2. @interface CLLocation (APMobileLBS)
  3. /*
  4. * Specifies whether to disable high-precision positioning. Default value: NO
  5. */
  6. @property(nonatomic,assign)BOOL ap_lbs_is_high_accuracy_close;
  7. @end

Sample code

 
  1. - (void)getLocationWithHighAccuracy {
  2. MPLBSConfiguration *configuration = [[MPLBSConfiguration alloc] init];
  3. configuration.desiredAccuracy = kCLLocationAccuracyBest;
  4. configuration.highAccuracyRequired = YES;
  5. self.locationManager = [[MPLBSLocationManager alloc] initWithConfiguration:configuration];
  6. [self.locationManager requestLocationNeedReGeocode:NO completionHandler:^(BOOL success, MPLBSLocationInfo * _Nonnull locationInfo, NSError * _Nonnull error) {
  7. NSString *message;
  8. if (success) {
  9. message = [NSString stringWithFormat:@"Positioning success. Longitude: %.5f. Latitude: %.5f. Precision: %.3f. Whether high precision is required: %d", locationInfo.location.coordinate.longitude, locationInfo.location.coordinate.latitude, locationInfo.location.horizontalAccuracy, !locationInfo.location.ap_lbs_is_high_accuracy_close];
  10. } else {
  11. message = [NSString stringWithFormat:@"%@", error];
  12. }
  13. dispatch_async(dispatch_get_main_queue(), ^{
  14. AUNoticeDialog *alert = [[AUNoticeDialog alloc] initWithTitle:@"Positioning result" message:message delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
  15. [alert show];
  16. });
  17. }];
  18. }