MapKit giving wrong lat & lang - iOS 8
Asked Answered
D

3

0

When I try to use map in iOS 8, I get below error.

Trying to start MapKit location updates without prompting for location authorization. Must call -[CLLocationManager requestWhenInUseAuthorization] or -[CLLocationManager requestAlwaysAuthorization] first.

For this I tried solution from here, but still no luck.

Below is what I did.

Step 1. Have entry in Info.plist

NSLocationWhenInUseUsageDescription - This is test text

Step 2. Updated -(CLLocationCoordinate2D) getLocation

-(CLLocationCoordinate2D) getLocation{
    CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    // this is change
    if(IS_OS_8_OR_LATER) {
        [locationManager requestWhenInUseAuthorization];
    }
    [locationManager startUpdatingLocation];


    CLLocation *location = [locationManager location];
    CLLocationCoordinate2D coordinate = [location coordinate];

    return coordinate;
}

Step 3. Prefix.pch

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

Step 4. Delete the app again with below in viewDidLoad.

CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];

NSLog(@"ffffff===%f===%f", theCoordinate.latitude, theCoordinate.longitude);

Still I get output as

ffffff===0.000===0.000

Note:

I get warning, but that is just for half seconds and disappears and then I get message as

CLLocationCoordinate2D theCoordinate;
theCoordinate = [self getLocation];

NSLog(@"ffffff===%f===%f", theCoordinate.latitude, theCoordinate.longitude);

When I go in setting to check the status for Location, below is what I have.

enter image description here

Any idea why this is happening?

I am testing on iPad 2 iOS 8.0 & simulator iOS 8.0

Drier answered 14/10, 2014 at 8:56 Comment(0)
D
0

Not sure what was real problem, but putting below code in viewDidLoad solve problem.

mapView.delegate = self;
locationManager.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
#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];
}
#endif
[self.locationManager startUpdatingLocation];

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

Reference

Drier answered 14/10, 2014 at 13:2 Comment(0)
H
0

You need to implement the CLLocationManager Delegate ( -(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations ) to get location updates.

Here's a code snippet for your reference:

-(void) locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
     CLLocation *currentLocation = [locations lastObject];

     NSLog(@"ffffff===%f===%f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
}

Alternatively, you can implement the Mapkit's following delegate method:

- (void)mapView:(MKMapView *)mapView didUpdateUserLocation:(MKUserLocation *)userLocation
{
    MKUserLocation *myLocation = userLocation;
    NSLog(@"ffffff===%f===%f", myLocation.coordinate.latitude, myLocation.coordinate.longitude);
}
Hydrastine answered 14/10, 2014 at 12:57 Comment(1)
did that too.. still its same.... I found the solution, will post as answer now...Drier
D
0

Not sure what was real problem, but putting below code in viewDidLoad solve problem.

mapView.delegate = self;
locationManager.delegate = self;
self.locationManager = [[CLLocationManager alloc] init];
#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];
}
#endif
[self.locationManager startUpdatingLocation];

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

Reference

Drier answered 14/10, 2014 at 13:2 Comment(0)
M
0

The requestWhenInUseAuthorization runs asynchronously, so you should not be calling startUpdatingLocation until you get a change in authorization status in your delegate.

Maura answered 14/10, 2014 at 21:13 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.