mkmapviewios8mkcoordinateregion

MKMapView not centering on location iOS8


I have my app on two devices, one on an iOS7 enabled device and another on an iOS8 enabled device. My MKMapView is not center on the users location on the iOS8 device but is on the iOS7.

Here is my code, I have requested the use of the users location on another screen.

-(void)viewDidLoad{

[LocationManager sharedInstance];
CLLocation * location = [[LocationManager sharedInstance] getCurrentLocation];
NSLog(@"Location %f %f",location.coordinate.latitude,location.coordinate.longitude);
MKCoordinateRegion mapRegion;
mapRegion.center = location.coordinate;
mapRegion.span.latitudeDelta = 0.01;
mapRegion.span.longitudeDelta = 0.01;
 NSLog(@"Map Region Lat %f",mapRegion.center.latitude);
NSLog(@"Map Region Long %f ",mapRegion.center.longitude);

self.getDirectionsMap.delegate = self;
[self.getDirectionsMap setRegion:mapRegion animated: NO];
self.getDirectionsMap.showsUserLocation=YES;
self.getDirectionsMap.showsPointsOfInterest=YES;




}

My Location Manager object .m

+ (LocationManager *)sharedInstance {
if (nil == kLocationManager) {

    kLocationManager = [[LocationManager alloc] init];
}

return kLocationManager;
}

-(CLLocation *)getCurrentLocation{
CLLocation *loc = [_locationManager location];

return loc;
}

- (id)init {
    self = [super init];
    if (!self) return nil;

    /* setup location manager */
    _locationManager = [[CLLocationManager alloc] init];
    [_locationManager setDelegate:self];

    //iOS8 check
    if ([_locationManager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {

        [_locationManager requestWhenInUseAuthorization];
    }
    [_locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
    [_locationManager setDistanceFilter:DISTANCE_FILTER];


    return self;
 }

My map successful shows the users position but map is not centered. My location also gets logged correctly with the correct coordinates, and this also matches the logged Map region lat and long.


Solution

  • You viewDidLoad method has not checked that there is actually a location to use yet. If you used a CLLocationManager you could set it going and then it would call the delegate's methods when it has an actual location. I suspect that the iOS device is taking longer to get a GPS reading and it is giving you nil in viewDidLoad.