IOS learning-Automatic Positioning and ios Positioning
The automatic positioning function is required in the project recently, that is, when you attend a meeting and sign in by scanning the QR code, the system automatically locates and uploads your location information in the sign-in process, this will avoid false sign-in. In this function, the built-in positioning module is mainly used. First, we need to configure the positioning function parameters, then, when the positioning is successful, we can call a specific method to perform the corresponding operation. Of course, when the positioning fails, we can also perform the corresponding operations, which all have corresponding callback methods, we only need to override the corresponding callback method to implement the corresponding function.
First, the built-in positioning module of the system we use is: <CoreLocation/CoreLocation. h> the methods for successful/failed locating are completed through the CLLocationManagerDelegate proxy. Therefore, we also need to inherit from the proxy in the class that requires location, and some corresponding methods can be implemented to successfully complete the callback.In the call of the positioning function, there are two main steps: 1. Positioning parameter configuration; 2. Proxy callback method implementation.
1. Positioning parameter configuration
The configuration of positioning parameters is mainly configured through the system's positioning manager CLLocationManager. The main points of parameter configuration areSet proxy, set addressing precision, and enable Positioning. Of course, the prerequisite for the configuration of these parameters is that our mobile phone has authorized the geographical location of our project, so we need to judge the current positioning permission before configuring it, you can use the [CLLocationManager locationServicesEnabled] method to directly check whether the permission is enabled. If you have the permission, you can configure the permission. Otherwise, you cannot configure the permission, in this case, a dialog box is displayed, prompting you how to enable the positioning permission. In our project, there is no corresponding code module because no prompt is required.
-(Void) viewDidLoad {[super viewDidLoad]; // configure the positioning information [self configLocation];}-(void) configLocation {self. place = @ ""; // determine whether the positioning permission is enabled if ([CLLocationManager locationServicesEnabled]) {locationManager = [[CLLocationManager alloc] init]; locationManager. delegate = self; // sets the addressing precision locationManager. desiredAccuracy = kCLLocationAccuracyBest; locationManager. distanceFilter = 100366f; // start locating [locationManager startUpdatingLocation];} else {// If the positioning permission is not enabled, You can prompt the corresponding code }}2. Callback of the proxy method
As we have mentioned earlier, our class must inherit the CLLocationManagerDelegate proxy and implement some of the methods to correctly call the corresponding proxy method when the location is successful or fails.There are two main proxy methods used: one is to call successfully and the other is to call upon failure.The Code is as follows. When locating a failure, we can first pop up a prompt to locate the cause of the failure, and then we can choose to locate again, alternatively, we can choose to limit the number of consecutive failures that will not be located again. You can configure and develop the specific implementation methods as needed.
# Pragma mark-CLLocationManagerDelegate // callback for successful locating. Here we configure our location information-(void) locationManager :( CLLocationManager *) manager didUpdateLocations :( NSArray <CLLocation *> *) locations {[locationManager stopUpdatingLocation]; CLLocation * currentLoc = [locations lastObject]; CLGeocoder * geoCoder = [[CLGeocoder alloc] init]; // obtain the location information by decompiling with longitude and latitude [geoCoder reverseGeocodeLocation: currentLoc completionHandler: ^ (NSArray <CLPlacemark *> * _ Nullable placemarks, NSError * _ Nullable error) {if (placemarks. count> 0) {// obtain the current location information CLPlacemark * placeMark = [placemarks firstObject]; if (placeMark) {self. place = [NSString stringWithFormat: @ "% @", placeMark. administrativeArea, placeMark. locality, placeMark. subLocality, ZYIsNullOrEmpty (placeMark. thoroughfare )? @ "": PlaceMark. thoroughfare] ;}}] ;}// call-(void) locationManager :( CLLocationManager *) manager didFailWithError :( NSError *) error {[locationManager startUpdatingLocation];}