I have implemented core location in my app.
Everything works perfect when user allows sharing the location at start of the app.
But when user does not share his location “South atlantic” is shown in the location field in the app.
What should be done to avoid this and what should be displayed when the user does not share his location.
I am using this code for getting the location:
- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions
{
self.window = [[UIWindow alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
// Override point for customization after application launch.
self.viewController = [[ViewController alloc] initWithNibName:@"ViewController" bundle:nil];
[self.window makeKeyAndVisible];
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
CLGeocoder *ceo = [[CLGeocoder alloc]init];
CLLocation *loc = [[CLLocation alloc]initWithLatitude:locationManager.location.coordinate.latitude longitude:locationManager.location.coordinate.longitude]; //insert your coordinates
[ceo reverseGeocodeLocation: loc completionHandler:
^(NSArray *placemarks, NSError *error)
{
CLPlacemark *placemark = [placemarks objectAtIndex:0];
NSLog(@"placemark %@",placemark);
//String to hold address
locatedAt = [[placemark.addressDictionary valueForKey:@"FormattedAddressLines"] componentsJoinedByString:@", "];
NSLog(@"addressDictionary %@", placemark.addressDictionary);
NSLog(@"placemark %@",placemark.region);
NSLog(@"placemark %@",placemark.country); // Give Country Name
NSLog(@"placemark %@",placemark.locality); // Extract the city name
NSLog(@"location %@",placemark.name);
NSLog(@"location %@",placemark.ocean);
NSLog(@"location %@",placemark.postalCode);
NSLog(@"location %@",placemark.subLocality);
NSLog(@"location %@",placemark.location);
//Print the location to console
NSLog(@"I am currently at %@",locatedAt);
}];
return YES;
}
- (NSString *)deviceLocation
{
NSString *theLocation = [NSString stringWithFormat:@"latitude: %f longitude: %f", locationManager.location.coordinate.latitude, locationManager.location.coordinate.longitude];
return theLocation;
}
See the Location Awareness Programming Guide for information on how to get the user’s location. Bottom line, you have to create the
CLLocationManagerlocation manager (like you are), but then start a location service (either the low-power significant-change location service or the standard location service) and wait for theCLLocationManagerDelegatemethoddidUpdateLocationsto return a location (or fordidFailWithErrorto report an error, either because the app failed authorization, or because the device couldn’t satisfy the location request for some reason). You can’t, though, just grab the location from theCLLocationManager. You have to wait for the service to calldidUpdateLocationsbefore you reliably start using longitudes and latitudes.The Location Awareness Programming Guide will walk you through all of this, including some very helpful code samples.