iOS 8 Map Kit Obj-C : Get Users Location

Map View | User Location
Map View | User Location

iOS 8 Map Kit Obj-C : Get Users Location

In your .plist Add a new row with the key name:

NSLocationWhenInUseUsageDescription

Or

NSLocationAlwaysUsageDescription

Define the header:

#define IS_OS_8_OR_LATER ([[[UIDevice currentDevice] systemVersion] floatValue] >= 8.0)

Update your files with following code:

ViewController.h

#import <MapKit/MapKit.h>
#import <MapKit/MKAnnotation.h>

@interface YourViewController : UIViewController <MKMapViewDelegate,  CLLocationManagerDelegate> {

}

@property(nonatomic, retain) IBOutlet MKMapView *mapView;
@property(nonatomic, retain) CLLocationManager *locationManager;

ViewController.m

- (void)viewDidLoad
{
    [super viewDidLoad];
    // Do any additional setup after loading the view.


    mapView.delegate = self;
    self.locationManager = [[CLLocationManager alloc] init];
    self.locationManager.delegate = self;
    #ifdef __IPHONE_8_0
    if(IS_OS_8_OR_LATER) {
         // Use one or the other, not both. Depending on what you put in info.plist
        [self.locationManager requestWhenInUseAuthorization];
        [self.locationManager requestAlwaysAuthorization];
    }
    #endif
    [self.locationManager startUpdatingLocation];

    mapView.showsUserLocation = YES;
    [mapView setMapType:MKMapTypeStandard];
    [mapView setZoomEnabled:YES];
    [mapView setScrollEnabled:YES];
}

-(void)viewDidAppear:(BOOL)animated {
    [super viewDidAppear:YES];

    self.locationManager.distanceFilter = kCLDistanceFilterNone;
    self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
    [self.locationManager startUpdatingLocation];
    NSLog(@"%@", [self deviceLocation]);

    //View Area
    MKCoordinateRegion region = { { 0.0, 0.0 }, { 0.0, 0.0 } };
    region.center.latitude = self.locationManager.location.coordinate.latitude;
    region.center.longitude = self.locationManager.location.coordinate.longitude;
    region.span.longitudeDelta = 0.005f;
    region.span.longitudeDelta = 0.005f;
    [mapView setRegion:region animated:YES];

}

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKCoordinateRegion region = MKCoordinateRegionMakeWithDistance(userLocation.coordinate, 800, 800);
    [self.mapView setRegion:[self.mapView regionThatFits:region] animated:YES];
}

- (NSString *)deviceLocation {
    return [NSString stringWithFormat:@"latitude: %f longitude: %f", self.locationManager.location.coordinate.latitude, self.locationManager.location.coordinate.longitude];
}

Helping, Learning, Coding 🙂

  • ralf

    hi,
    just wanted to let you know that in theregion.span you set the longitudeDelta two times instead of longitudeDelta and latitudeDelta.

    Btw, nice iOS 8 update tutorial. Thanks!

    Cheers

    • Ok. thanks for mentioned my mistake.

      Thanks for your comments and watching the blog.

  • Can you post the completed app, I have copied the code and the location of the user works, but does not show on map as blue annotation, am I missing something?

  • Lancewise

    I have been searching for the past hour for a simple tutorial to grab the Latitude and Longitude of a device for iOS8. Your tutorial had everything I needed and then some. Thanks for sharing!

  • Dante

    How to do this in Swift 1.2? Thanks.

  • Thanks for your post !

  • Great tutorial, everything works super! Are you thinking about doing more tutorials such as MapView annotation etc.

  • imran

    Hi ashikh, Thanks for the fantastic post bro. Was really helpful we were been scratching our head from last two days.