0

I am trying to make my app find a user's location in an address-like form (located after the "Reverse Geocoding" comment). Below is my code:

- (IBAction)getuserlocation:(id) sender{
    //Getting Location
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;

    [locationManager startUpdatingLocation];
    NSLog(address);
}
#pragma mark - CLLocationManagerDelegate

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
    NSLog(@"didFailWithError: %@", error);
    UIAlertView *errorAlert = [[UIAlertView alloc]
                               initWithTitle:@"Error" message:@"Failed to Get Your Location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles:nil];
    [errorAlert show];
}

- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    NSLog(@"didUpdateToLocation: %@", newLocation);
    CLLocation *currentLocation = newLocation;

    if (currentLocation != nil) {
        longitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.longitude];
        latitude = [NSString stringWithFormat:@"%.8f", currentLocation.coordinate.latitude];
    }

    // Stop Location Manager
    [locationManager stopUpdatingLocation];

    // Reverse Geocoding
    NSLog(@"Resolving the Address");
    [geocoder reverseGeocodeLocation:currentLocation completionHandler:^(NSArray *placemarks, NSError *error) {
        NSLog(@"Found placemarks: %@, error: %@", placemarks, error);
        if (error == nil && [placemarks count] > 0) {
            placemark = [placemarks lastObject];
            address = [NSString stringWithFormat:@"%@ %@\n%@ %@\n%@\n%@",
                                 placemark.subThoroughfare, placemark.thoroughfare,
                                 placemark.postalCode, placemark.locality,
                                 placemark.administrativeArea,
                                 placemark.country];
        } else {
            NSLog(@"%@", error.debugDescription);
        }
    } ];

}

My issue is that the compiler only prints out "(null)". (The line where I do this is located in the "getuserlocation" IBAction.) I'm not sure why this is. I've tested the app on an actual iPhone 6 Plus, which unlike the simulator, has access to location services.

If anyone would be able to point out where the error/problem in my code is, causing it to print out null, I would greatly appreciate it. Thanks in advance to all who reply.

***BTW: The following lines are in my viewDidLoad section: locationManager = [[CLLocationManager alloc] init]; geocoder = [[CLGeocoder alloc] init];

iProgramIt
  • 54
  • 1
  • 2
  • 11
  • FYI - It's not the compiler that logs anything. It's your running app that is logging the message. – rmaddy Feb 23 '15 at 21:47
  • Ok. That is what I meant to type. Sorry for any confusion this may have caused. – iProgramIt Feb 23 '15 at 21:48
  • In addition to accessing the address variable before you have set a value, you need to make sure you have put the appropriate reason string in your info.plist and called `requestWhenInUseAuthorization` . You are also using a deprecated location delegate method. You should use `didUpdateLocations:`. Finally, disabling location updates after you receive the first location isn't a good idea as the first location probably won't be very accurate. You should continue to receive updates, perhaps waiting for a low value in the `horizonalAccuracy` property of the `CLLocation` – Paulw11 Feb 23 '15 at 21:58
  • @Paulw11 Thanks for your quick reply! I am fairly new to Objective-C programming so where exactly would I put this in my info.plist file? I don't see a reason string option anywhere in this file. Also, how do I have my app request permission to receive access to the user's current location? – iProgramIt Feb 23 '15 at 22:01
  • See http://stackoverflow.com/questions/24062509/location-services-not-working-in-ios-8/24063578#24063578 – Paulw11 Feb 24 '15 at 00:54

1 Answers1

0

You are logging address long before you get a location and convert it to an address.

Move the NSLog(address); to just after where you actually assign a value to address.

BTW - it should be more like: NSLog(@"Address: %@, address);.

rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Ok. So how can I access address from the methods before assigning the values to them if I need to use it in my IBAction? Thank you for the reply by the way. :) – iProgramIt Feb 23 '15 at 21:47
  • I tried moving the NSLog like you told me, but nothing ever logged to the console. Is this code not being reached? – iProgramIt Feb 23 '15 at 21:52
  • Are you getting any of the other log messages in your code? Does you app have permission to use location services? Did you setup your location manager to request permission? – rmaddy Feb 23 '15 at 22:00
  • I am not getting other logs in my console either. Also, I'm not sure how to request permission to get location. Could this be my problem? I couldn't find out how to do this online. – iProgramIt Feb 23 '15 at 22:03
  • I did and added the lines @paulw11 said I should, yet it is still not getting my location. Not sure what to do. – iProgramIt Feb 23 '15 at 22:13
  • Update your question with relevant code and details about how you have setup your location manager. – rmaddy Feb 23 '15 at 22:14