0

I trying to get users current location using MapKit and CoreLocation. I'm really new to Objective-C. As for my research the implementation is slightly different from older iOS to iOS 8.0. I have followed everything correctly. Still it is getting current location. My actual target is to get pin user location on the mapKit during run time when the user gives the location in the text field.

filename.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface LocationSelectionViewController : UIViewController <CLLocationManagerDelegate>
@property (weak, nonatomic) IBOutlet UITextField *insertLocation;
@property (strong, nonatomic) IBOutlet MKMapView *serviceLocation;
- (IBAction)next:(id)sender;

@end

#import "LocationSelectionViewController.h"
#import <CoreLocation/CoreLocation.h>

@interface LocationSelectionViewController () <CLLocationManagerDelegate>

@end

@implementation LocationSelectionViewController
CLLocationManager *locationManager;
@synthesize serviceLocation;

- (void)viewDidLoad {
    [super viewDidLoad];
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    [locationManager requestAlwaysAuthorization];
    [locationManager startUpdatingLocation];
    // Do any additional setup after loading the view.
    self.serviceLocation = [[MKMapView alloc] initWithFrame:self.view.bounds];
    self.serviceLocation.autoresizingMask = UIViewAutoresizingFlexibleWidth | UIViewAutoresizingFlexibleHeight;
    [self.view addSubview:self.serviceLocation];
    [self.serviceLocation.delegate self];
    //[self.serviceLocation setShowsUserLocation:YES];
}

/*- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation{
    CLLocationCoordinate2D loc=[userLocation coordinate];
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(loc, 100, 100);
    [self.serviceLocation setRegion:region animated:YES];
}*/

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations{
    [locationManager requestAlwaysAuthorization];
    [locationManager startUpdatingLocation];

}
-(void) locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if(status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
        self.serviceLocation.showsUserLocation = YES;
    }
}

- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}

/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

- (IBAction)next:(id)sender {
}
@end

As in the image I have added the two properties as per iOS 8 requirement.

image

Can anyone advise me in archiving this:

  • getting users current location.
  • getting the location from text field and pinning on the MapKip during the run time.
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • FYI - your `locationManager` variable is a file global variable. Is that what you really want (probably not)? – rmaddy Feb 09 '16 at 06:01
  • 1. getting users current location and drop a pin:- http://stackoverflow.com/questions/23922625/drop-pin-on-current-location 2. getting the location from text field and pinning -- http://stackoverflow.com/questions/7459801/mkmapview-place-pin-at-location-long-lat – Vizllx Feb 09 '16 at 06:08

1 Answers1

1

Your flow for requesting and starting location update is not proper. First of all the requestAlwaysAuthorization will not change the authorization status immediately because it needs user to press Allow or Don't Allow button in the alert. So you should start location update only after getting its result. Then in didUpdateLocations no need to request/start again.

Do it in following way,

Dn your viewDidLoad

 BOOL isAuthorized = [CLLocationManager authorizationStatus] == kCLAuthorizationStatusAuthorizedAlways;
 if (isAuthorized) {
     [locationManager startUpdatingLocation];
 }
 else {
     [locationManager requestAlwaysAuthorization];
 }

And implement the delegate methods like

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
   CLLocation *current = locations.firstObject;
}

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    if(status == kCLAuthorizationStatusAuthorizedAlways || status == kCLAuthorizationStatusAuthorizedWhenInUse){
        self.serviceLocation.showsUserLocation = YES;
       [locationManager startUpdatingLocation];
    }
}

In iOS 8 for requesting permission you should add the usage description string in the info.plist. In your case the key is NSLocationAlwaysUsageDescription.

Johnykutty
  • 12,091
  • 13
  • 59
  • 100