Jason Renaldo
Jason Renaldo

Reputation: 2822

Getting lat/long from locationManager:didUpdateLocations using CoreLocation

I've looked at similar questions and examples but I can't seem to get the latitude and longitude from CoreLocation, as it says it is 0/0 if I look at the location directly in viewDidLoad. I have a breakpoint in the delegate method to access location, but I don't ever hit it. I am conforming to the CLLocationmanagerDelegate as well.

Code (ViewController.m):

- (void)viewDidLoad
{
    [super viewDidLoad];

    _locationManager = [[CLLocationManager alloc]init];
    _locationManager.delegate = self;
    _locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
    _locationManager.distanceFilter = kOneMile; //custom defined this one
    [_locationManager startUpdatingLocation];
}

//Actual update of the location
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    //Not sure if this is the best way to grab it?
    CLLocation *location = [locations objectAtIndex:0];

    //Breakpoint shows this as zero here
    [Utilities GetCityAndState:location.coordinate.latitude andLong:location.coordinate.longitude];
}

In my Utilities class

+(NSString *)GetCityAndState:(float)withlat andLong:(float)longitude{
NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init];
NSString *urlString = @"http://maps.googleapis.com/maps/api/geocode/json?latlng=";
//[urlString ] I am going to append the lat and lont to the urlString here 
[request setHTTPMethod:@"GET"];
[request setURL:[NSURL URLWithString:urlString]];
NSHTTPURLResponse *responseCode = nil;
NSError *error = nil;
NSData *response = [NSURLConnection sendSynchronousRequest:request returningResponse:&responseCode error:&error];
NSString *get = [[NSString alloc] initWithData:response encoding:NSUTF8StringEncoding];

//Once I have that I am going to take the results from the reverse geocoding and return a string like LasAngelesCA
}

Upvotes: 1

Views: 1369

Answers (2)

Jason Renaldo
Jason Renaldo

Reputation: 2822

I'm not entirely sure why, but deleting the app in the simulator and rerunning it fixed the issue.

Upvotes: 1

Michael Dautermann
Michael Dautermann

Reputation: 89509

Change the declaration to this:

+ (NSString *)getCityAndState:(CLLocationDegrees)withlat andLong:(CLLocationDegrees)longitude;

Instead of using "floats" for the parameters.

Upvotes: 0

Related Questions