How to get current location using CLLocationManager in iOS

后端 未结 4 1368
一个人的身影
一个人的身影 2020-12-09 13:03

I can already find the current location using latitude and longitude, but I would also like to be able to find the current location given a zip code.

Here is what I

相关标签:
4条回答
  • 2020-12-09 13:46

    Step 1: Import MobileCoreServices framework in .h File

    #import <MobileCoreServices/MobileCoreServices.h>

    Step 2: Add delegate CLLocationManagerDelegate

    @interface yourViewController : UIViewController<CLLocationManagerDelegate>
    {
        CLLocationManager *locationManager;
        CLLocation *currentLocation;
    }
    

    Step 3: Add this code in class file

    - (void)viewDidLoad
    {
        [super viewDidLoad];
        [self CurrentLocationIdentifier]; // call this method
    }
    

    Step 4: Method to get location

    //------------ Current Location Address-----
    -(void)CurrentLocationIdentifier
    {
        //---- For getting current gps location
        locationManager = [CLLocationManager new];
        locationManager.delegate = self;
        locationManager.distanceFilter = kCLDistanceFilterNone;
        locationManager.desiredAccuracy = kCLLocationAccuracyBest;
        [locationManager startUpdatingLocation];
        //------
    }
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
        currentLocation = [locations objectAtIndex:0];
        [locationManager stopUpdatingLocation];
        CLGeocoder *geocoder = [[CLGeocoder alloc] init] ;
        [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error)
         {
             if (!(error))
             {
                 CLPlacemark *placemark = [placemarks objectAtIndex:0];
                NSLog(@"\nCurrent Location Detected\n");
                 NSLog(@"placemark %@",placemark);
                 NSString *locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
                 NSString *Address = [[NSString alloc]initWithString:locatedAt];
                 NSString *Area = [[NSString alloc]initWithString:placemark.locality];
                 NSString *Country = [[NSString alloc]initWithString:placemark.country];
                 NSString *CountryArea = [NSString stringWithFormat:@"%@, %@", Area,Country];
                 NSLog(@"%@",CountryArea);
             }
             else
             {
                 NSLog(@"Geocode failed with error %@", error);
                 NSLog(@"\nCurrent Location Not Detected\n");
                 //return;
                 CountryArea = NULL;
             }
             /*---- For more results 
             placemark.region);
             placemark.country);
             placemark.locality); 
             placemark.name);
             placemark.ocean);
             placemark.postalCode);
             placemark.subLocality);
             placemark.location);
              ------*/
         }];
    }
    
    0 讨论(0)
  • 2020-12-09 13:52

    if location permissions not asking when you first launch then you cant get your current location .

    iOS8 has got us major API changes with the LocationsServices

    Assuming [CLLocationManager locationServicesEnabled] return YES,

    With the First Launch of the iOS App [both iOS7 and iOS8] - locationMangers(CLLocationManager)

    0 讨论(0)
  • 2020-12-09 13:56

    You can call Google APIs Maps service to get the location using zipcode:

    Just enter your zipcode in address={your zipcode}.

    0 讨论(0)
  • 2020-12-09 14:02

    Here is a code to get user current location using block

    1) #import <CoreLocation/CoreLocation.h>

    2) <CLLocationManagerDelegate>

    3) in .h file

    //Location
    typedef void(^locationBlock)();
    
    //Location
    -(void)GetCurrentLocation_WithBlock:(void(^)())block;
    
    @property (nonatomic, strong) locationBlock _locationBlock;
    @property (nonatomic,copy)CLLocationManager *locationManager;
    @property (nonatomic)CLLocationCoordinate2D coordinate;
    @property (nonatomic,strong) NSString *current_Lat;
    @property (nonatomic,strong) NSString *current_Long;
    

    4) in .m file

    #pragma mark - CLLocationManager
    -(void)GetCurrentLocation_WithBlock:(void(^)())block {
        self._locationBlock = block;
        _locationManager = [[CLLocationManager alloc] init];
        [_locationManager setDelegate:self];
        [_locationManager setDistanceFilter:kCLDistanceFilterNone];
        [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
        if (IS_OS_8_OR_LATER) {
            if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
                [_locationManager requestWhenInUseAuthorization];
                [_locationManager requestAlwaysAuthorization];
            }
        }
        [_locationManager startUpdatingLocation];
    }
    
    - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
        CLLocation *currentLoc=[locations objectAtIndex:0];
        _coordinate=currentLoc.coordinate;
        _current_Lat = [NSString stringWithFormat:@"%f",currentLoc.coordinate.latitude];
        _current_Long = [NSString stringWithFormat:@"%f",currentLoc.coordinate.longitude];
        NSLog(@"here lat %@ and here long %@",_current_Lat,_current_Long);
        self._locationBlock();
        [_locationManager stopUpdatingLocation];
        _locationManager = nil;
    }
    
    - (void)locationManager:(CLLocationManager *)manager
           didFailWithError:(NSError *)error {
    }
    

    5) Call function

    - (void)viewDidLoad {
        [super viewDidLoad];
        [self GetCurrentLocation_WithBlock:^{
            NSLog(@"Lat ::%f,Long ::%f",[self.current_Lat floatValue],[self.current_Long floatValue]);
        }];
    }
    

    6) And add below line to .plist

    Any one or both of below keys needs to be added in Info.plist file.

    NSLocationWhenInUseUsageDescription
    NSLocationAlwaysUsageDescription
    
    0 讨论(0)
提交回复
热议问题