Location is shown 0.0 in iPad and iPod

162 Views Asked by At

Hello What is the best way to get the location on the iPad, iPod as well in iPhone.

I have integrated the following but I'm still not able to get the correct current location. On iPod I only get the location for the first time:

+ (GPS*)get 
{
    if (!g_) 
    {
        NSLog(@"Creating instance of GPS!");
        g_ = [GPS new];
    }
    return g_;
}

@synthesize manager;

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

    //Setup the manager
    manager = [[CLLocationManager alloc] init];
    if (!manager) 
    {
        return nil;
    }
    manager.distanceFilter = kCLDistanceFilterNone;
    manager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
    manager.desiredAccuracy = kCLLocationAccuracyBest;
    manager.delegate = self;

    if ([manager respondsToSelector:@selector(pausesLocationUpdatesAutomatically)]) 
    {
        manager.pausesLocationUpdatesAutomatically = NO;
    }
    if ([manager respondsToSelector:@selector(requestAlwaysAuthorization)])
    {
        [manager requestAlwaysAuthorization];
    }

    [manager startUpdatingLocation];

    return self;
}

- (void)start
{
    NSLog(@"Start Tracking");

    [manager startUpdatingLocation];
 }

-(void)stop
{
    [manager stopUpdatingLocation];
}

#pragma mark CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
    CLLocation *currentLocation = [locations lastObject];
    NSLog(@"%f : %f", currentLocation.coordinate.latitude, currentLocation.coordinate.longitude);
    //[self writeToFile];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"locationManager - didFailWithError: %@", [error localizedDescription]);
    NSLog(@"domain:%@", [error domain]);
    //NSLog(@"code:%i", [error code]);
    //[self writeToFile];
}
1

There are 1 best solutions below

0
SanitLee On

There's a location authorization issue on iOS8. Per your recent comment that you're using iOS 8.4, that might be your problem. So try this below code before startUpdatingLocation:

// This part was added due to a location authorization issue on iOS8
// See more at: http://nevan.net/2014/09/core-location-manager-changes-in-ios-8/
if ([manager respondsToSelector:@selector(requestWhenInUseAuthorization)]) {
    [manager requestWhenInUseAuthorization];
}
self.mapView.showsUserLocation = YES; //?

Prior to this, please make sure you set up Info.plist correctly by adding one or both of the following keys to it:

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription