SoFunction
Updated on 2025-04-07

IOS Development Notes Collection 49 Detailed explanation of positioning CLLocation

There is a need for positioning CLLocation in the project function. I encountered some knowledge difficulties. With the help of all heroes, the problem has been solved. I will share it for everyone to learn. I hope everyone can learn and make progress together.

1. Simple explanation

Common operations and properties of CLLocationManager

Start User Location- (void)startUpdatingLocation;

Stop user positioning- (void) stopUpdatingLocation;

Note: After calling the startUpdatingLocation method, you start to continuously locate the user's location, and the following methods of the proxy will be frequently called in the middle.

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;

How many meters are located every

@property(assign, nonatomic) CLLocationDistance distanceFilter;

Positioning accuracy (the more accurate the more power is, the more power is consumed)

@property(assign, nonatomic) CLLocationAccuracy desiredAccuracy;

Using the positioning function, you must first import the framework, comply with the CLLocationManagerDelegate protocol, and then create the location manager CLLocationManager

After iOS 8.0, the positioning function needs to add two NSString type fields: NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription to use the positioning function

Post the code and encourage everyone, please study it yourself

{
   = [[CLLocationManager alloc] init];
  _locationManager.delegate = self;
  if([CLLocationManager locationServicesEnabled] == NO) {
   // NSLog(@"No GPS service");  }
  //Geographical location accuracy  _locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
  //Set the distance filter, double type, as long as the distance changes, the delegate agent will be called as long as the distance changes a lot.   = kCLDistanceFilterNone; // meters
  [_locationManager requestWhenInUseAuthorization];// Front desk positioning  [_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
   didUpdateLocations:(NSArray *)locations
{
  NSLog(@"longitude = %f", ((CLLocation *)[locations
                       lastObject]).);
  NSLog(@"latitude = %f", ((CLLocation *)[locations lastObject]).);
    CGFloat longTI=((CLLocation *)[locations
                    lastObject]).;
    CGFloat latTI=((CLLocation *)[locations lastObject]).;
    //Show longitude to label    _longitudeLabel.text = [NSString stringWithFormat:@"%f",longTI];
    //Realize the latitude to label    _latitudeLabel.text = [NSString stringWithFormat:@"%f",latTI];
  // Get the current city name  CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  //Create address information based on latitude and longitude reverse geographic  [geocoder reverseGeocodeLocation: completionHandler:^(NSArray *array, NSError *error)
   {
     if ( > 0)
     {
       CLPlacemark *placemark = [array objectAtIndex:0];
// //Show all the information obtained on the label//        = ;
       //Get the city       NSString *city = ;
       if (!city) {
         //The city information of the four municipalities directly under the central government cannot be obtained through locality, and can only be obtained by obtaining provinces (if the city is empty, it can be known as a municipality directly under the central government)         city = ;
       }
      // NSLog(@"city = %@", city);
       _cityName=city;
     }
     else if (error == nil && [array count] == 0)
     {
      // NSLog(@"No results were returned.");
     }
     else if (error != nil)
     {
      // NSLog(@"An error occurred = %@", error);
     }
   }];
  //The system will keep updating the data until it is selected to stop updating, because we only need to obtain the latitude and longitude once, so we stop updating after obtaining it  [manager stopUpdatingLocation];
}