IOS development notes collation 49 detail positioning CLLocation

  • 2020-05-13 03:21:03
  • OfStack

There is a requirement of positioning CLLocation in the project function, and I have encountered some knowledge difficulties. With the help of all heroes, the problem has been solved. I hereby share this for you to learn and hope you can make progress together.

1. Simple instructions

1.CLLocationManager

Common operations and properties of CLLocationManager

Start user positioning - (void)startUpdatingLocation;

Stop user location - (void) stopUpdatingLocation;

Note: when the startUpdatingLocation method is called, the user's location is continually located, with the following methods of the agent frequently called along the way

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

Position once every several meters

@ property (assign nonatomic) CLLocationDistance distanceFilter;

Positioning accuracy (the more accurate, the more power consumption)

@ property (assign nonatomic) CLLocationAccuracy desiredAccuracy;

To use the location function, first import the framework, follow the CLLocationManagerDelegate protocol, and then create the location manager CLLocationManager

After iOS8.0, the location function requires the addition of NSLocationWhenInUseUsageDescription and NSLocationAlwaysUsageDescription NSString type fields in info.plist to use the location function

Code posted with you common encouragement, you read their own research


{
  self.locationManager = [[CLLocationManager alloc] init];
  _locationManager.delegate = self;
  if([CLLocationManager locationServicesEnabled] == NO) {
   //  NSLog(@" There is no GPS service ");
  }
  // Geographic location accuracy 
  _locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
  // Set the distance filter ,double type , As long as the distance changes , The principal agent is called 
  self.locationManager.distanceFilter = kCLDistanceFilterNone; // meters
  [_locationManager requestWhenInUseAuthorization];//  The front desk to locate 
  [_locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
   didUpdateLocations:(NSArray *)locations
{
  NSLog(@"longitude = %f", ((CLLocation *)[locations
                       lastObject]).coordinate.longitude);
  NSLog(@"latitude = %f", ((CLLocation *)[locations lastObject]).coordinate.latitude);
    CGFloat longTI=((CLLocation *)[locations
                    lastObject]).coordinate.longitude;
    CGFloat latTI=((CLLocation *)[locations lastObject]).coordinate.latitude;
    // Show the longitude to label on 
    _longitudeLabel.text = [NSString stringWithFormat:@"%f",longTI];
    // Make latitude realistic label on 
    _latitudeLabel.text = [NSString stringWithFormat:@"%f",latTI];
  //  Gets the name of the current city 
  CLGeocoder *geocoder = [[CLGeocoder alloc] init];
  // The address information is compiled according to the latitude and longitude reverse geography 
  [geocoder reverseGeocodeLocation:locations.lastObject completionHandler:^(NSArray *array, NSError *error)
   {
     if (array.count > 0)
     {
       CLPlacemark *placemark = [array objectAtIndex:0];
//       // Displays all the information obtained to label on 
//       self.location.text = placemark.name;
       // Getting the cities 
       NSString *city = placemark.locality;
       if (!city) {
         //4 City information for large municipalities is not available locality Obtain, can only be obtained by acquiring the province method (if city (city directly under the central government) 
         city = placemark.administrativeArea;
       }
      // 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 1 Update the data until you choose to stop updating, because you only need to get it 1 Sublatitude and longitude, so stop updating after you get it 
  [manager stopUpdatingLocation];
}

The above is about this site this site for you to collate the IOS development of CLLocation detailed location, will continue to update, I hope you can like.


Related articles: