Through CoreLocation, the user's current location is obtained, which is different from the positioning in the map.
1. Import
2. #import <CoreLocation/>
3. Declare the proxy <CLLocationManagerDelegate>
4. Code implementation
1. Statement
CLLocationManager *locationManager;//Define Manager// Determine whether the positioning operation is allowedif([CLLocationManager locationServicesEnabled]) { CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease]; = self; }else { //The user is prompted to be unable to perform positioning operations} // Start positioning[locationManager startUpdatingLocation];
2. Update the proxy method after the location is updated, the method of iOS 6.0
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { //latitude and lontitude are NSString variables //latitude = [NSString stringWithFormat:@"%.4f", ]; //longitude = [NSString stringWithFormat:@"%.4f", ]; }
3. Recommended methods for iOS 6.0 or above
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations { // Here locations stores the location coordinate value that is continuously updated, and take the last value as the latest position. If you do not want it to continuously update the location, then after obtaining a value in this method, the locationManager stopUpdatingLocation CLLocation *currentLocation = [locations lastObject]; CLLocationCoordinate2D coor = ; = ; = ; //[ stopUpdatingLocation]; }
4. Methods for failed updates
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { if ( == kCLErrorDenied) { // Prompt the user for the reason of the error, press and hold the Option key to click KCLErrorDenied to view more error information, and print the value to find the reason. } }
The above is all the content of this article. I hope it will be helpful to everyone's study and I hope everyone will support me more.