LKM
LKM

Reputation: 2450

How to get GPS coordinate debugging with simulator in iOS?

I try to get coordinate of my location.

I stackoverflowed so what I coded is below:

locationManager = [[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
[locationManager startUpdatingLocation];

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

self.longitude=coordinate.longitude;
self.latitude=coordinate.latitude;

NSLog(@"dLongitude : %f",self.longitude);
NSLog(@"dLatitude : %f", self.latitude);

But I'm always getting 0 all the time. Is the code above wrong? or I didn't set my simulator for GPS location?

I don't understand why I'm having trouble in getting coordinate.

Upvotes: 4

Views: 1514

Answers (3)

user2680961
user2680961

Reputation: 1

- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status{
    BOOL shouldIAllow = FALSE;
    NSString* locationStatus = @"";

    switch (status) {
        case kCLAuthorizationStatusRestricted:
            locationStatus = @"Restricted Access to location";
            break;
        case kCLAuthorizationStatusDenied:
            locationStatus = @"User denied access to location";
            break;
        case kCLAuthorizationStatusNotDetermined:
            locationStatus = @"Status not determined";
        default:
            locationStatus = @"Allowed to location Access";
            shouldIAllow = TRUE;
            break;
    }

    if (shouldIAllow) {
        NSLog(@"Location to Allowed");
        // Start location services
        [_locationManager startUpdatingLocation];
    } else {
        NSLog(@"Denied access: \(locationStatus)");
    }
}

Upvotes: 0

JP Illanes
JP Illanes

Reputation: 3675

[CLLocationManager location] will return you the most recently retrieved user location, but as the documentation says:

The value of this property is nil if no location data has ever been retrieved.

At the beginning your location is still unknown. You should use the delegates methods to react when the CLLocationManager find out your location. Implement this method:

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
    CLLocation *location = [locations lastObject];
    //User your location...
}

Take a look at the documentation.

Upvotes: 1

Azat
Azat

Reputation: 6795

Firstly, here are several issues in your code:

  1. You should retain locationManager somewhere to keep it running
  2. Implement -locationManager:didUpdateLocations: and -locationManager:didFailWithError: delegate methods
  3. Also, if you are in iOS 8, you should add [locationMamager requestWhenInUseAuthorization]; or [locationMamager requestAlwaysAuthorization];
  4. Specify NSLocationWhenInUseUsageDescription or NSLocationAlwaysUsageDescription accordingly in your Info.plist

You can simulate location using Xcode, look at the information from Apple: https://developer.apple.com/library/ios/recipes/xcode_help-debugger/articles/simulating_locations.html

Upvotes: 6

Related Questions