CJ IOS Developer CJ IOS Developer - 3 months ago 26
Objective-C Question

Get User Current location using CLLocation in IOS

I want to get the user current location & address which I did using the

CLLocationManager
and added the core location framework. But the response is nothing. Please share your comments.

locationManager = [[CLLocationManager alloc] init];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.delegate = self; [locationManager startUpdatingLocation];


and the delegates are

(void)locationManager:(CLLocationManager *)manager didFailWithError: (NSError *)error {
NSLog(@"didFailWithError: %@", error);
UIAlertView *errorAlert = [[UIAlertView alloc] initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
[errorAlert show];
}

(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *currentLocation = locations;
[locationManager setDelegate:nil];
if (currentLocation != nil) {
NSString *longitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
NSString *latitudeLabel = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
}

Answer

Check My code you can get address from current location

- (void)viewDidLoad {
CLGeocoder *ceo;

self.locationManager = [[CLLocationManager alloc]init];
self.locationManager.delegate = self;
[locationManager startUpdatingLocation];
}



-(void)viewDidAppear:(BOOL)animated{
ceo= [[CLGeocoder alloc]init];
[self.locationManager requestWhenInUseAuthorization];
if ([self.locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [self.locationManager requestWhenInUseAuthorization];
}
    CLLocationCoordinate2D coordinate;

coordinate.latitude=locationManager.location.coordinate.latitude;
coordinate.longitude=locationManager.location.coordinate.longitude;
//CLLocationCoordinate2D  ctrpoint;
//  ctrpoint.latitude = ;
//ctrpoint.longitude =f1;
//coordinate.latitude=23.6999;
//coordinate.longitude=75.000;
MKPointAnnotation *marker = [MKPointAnnotation new];
marker.coordinate = coordinate;
NSLog(@"%f",coordinate.latitude);
//[self.mapView addAnnotation:marker];


CLLocation *loc = [[CLLocation alloc]initWithLatitude:coordinate.latitude longitude:coordinate.longitude
                   ];
[ceo reverseGeocodeLocation:loc
          completionHandler:^(NSArray *placemarks, NSError *error) {
              CLPlacemark *placemark = [placemarks objectAtIndex:0];
              NSLog(@"placemark %@",placemark);
              //String to hold address
              NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
              NSLog(@"addressDictionary %@", placemark.addressDictionary);

              NSLog(@"placemark %@",placemark.region);
              NSLog(@"placemark %@",placemark.country);  // Give Country Name
              NSLog(@"placemark %@",placemark.locality); // Extract the city name
              NSLog(@"location %@",placemark.name);
              NSLog(@"location %@",placemark.ocean);
              NSLog(@"location %@",placemark.postalCode);
              NSLog(@"location %@",placemark.subLocality);

              NSLog(@"location %@",placemark.location);
              //Print the location to console
              NSLog(@"I am currently at %@",locatedAt);


              _City.text=[placemark.addressDictionary objectForKey:@"City"];
              [locationManager stopUpdatingLocation];
          }

 ];
}





#pragma mark - CLLocationManagerDelegate

Please add this two method

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation

Hope This work

Thank you