It shows me (null) (null),Ahmedabad Gujarat (null) (null) when following code executes.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
_CurrentLocation = newLocation.coordinate;
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error)
{
for (CLPlacemark * placemark in placemarks)
{
NSString *addressTxt = [NSString stringWithFormat:#"%# %#,%# %# %# %#",
[placemark subThoroughfare],[placemark thoroughfare],
[placemark locality], [placemark administrativeArea], [placemark subLocality],[placemark subAdministrativeArea]];
NSLog(#" >> %#",addressTxt);
self.strLocation = [placemark locality];
}
}];
}
How to get other values ? Is location service will not provide these data in my area ?
And if not then any alternative to get this data ?
Related
When trying to use CLGeocoder, I got this example from an online tutorial:
https://www.appcoda.com/how-to-get-current-location-iphone-user/
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(#"didUpdateToLocation: %#", newLocation);
CLLocation *currentLocation = newLocation;
if (currentLocation != nil) {
longitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.longitude];
latitudeLabel.text = [NSString stringWithFormat:#"%.8f", currentLocation.coordinate.latitude];
}
// 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];
addressLabel.text = [NSString stringWithFormat:#"%# %#\n%# %#\n%#\n%#",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
} else {
NSLog(#"%#", error.debugDescription);
}
} ];
}
However, I don't quite understand why it takes an array of "placemarks" in the completion handler part and why we should use the last object of "placemarks" (I also see people using the first object?).
I've read through the apple document but didn't find any good explanation in terms of use:
https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLGeocoder_class/
You are reverse-geocoding a lat/long. It might yield more than one address. If you get more than one and don't care which one you use, it's just as valid to extract the first or last placemark object in the array.
(You'll probably only get one match most of the time, in which case the first and last element are the same thing.)
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];
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);.
I am trying to get the user's updated location by using core location framework.It is working in simulator, but not in ipad/iphone.I am using the following code to get it. In simulator, i am using freeway drive.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:
(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
CLLocation *currentLocation=newLocation;
NSLog(#"location : %#",currentLocation);
if(currentLocation !=nil)
{
self.latitudelabel.text = [NSString
stringWithFormat:#"%.8f",currentLocation.coordinate.latitude];
self.longitudelabel.text = [NSString
stringWithFormat:#"%.8f",currentLocation.coordinate.longitude];
}
CLLocationDistance distance = [newLocation distanceFromLocation:oldLocation];
NSLog(#"Calc.distance%f",distance);
[geocoder reverseGeocodeLocation:currentLocation completionHandler:^
(NSArray *placemarks, NSError *error)
{
if(error == nil && [placemarks count]>0)
{
placemark = [placemarks lastObject];
NSLog(#"Country %#",placemark.country);
NSLog(#"Area %#",placemark.administrativeArea);
NSLog(#"City %#",placemark.locality);
NSLog(#"Code %#",placemark.postalCode);
NSLog(#"Road %#",placemark.thoroughfare);
NSLog(#"Number %#",placemark.subThoroughfare);
self.addresslabel.text = [NSString stringWithFormat:#"%# %#
\n %# %# \n %# \n %#",
placemark.subThoroughfare, placemark.thoroughfare,
placemark.postalCode, placemark.locality,
placemark.administrativeArea,
placemark.country];
}
else
{
NSLog(#"tktk%#",error.debugDescription);
}
}];
}
Go in settings > Privacy > Location services > Your app > Always.
You can also check other alternatives posted in the following question Core Location not working in iOS 8
I'm trying to get the city name of my location, I use this code:
- (void)viewDidLoad
{
[super viewDidLoad]; CLLocationManager *locationManager =[[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
// Do any additional setup after loading the view.
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
//implementamos los metodos delegados de CLLocationManager.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// this creates a MKReverseGeocoder to find a placemark using the found coordinates
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSString * location = ([placemarks count] > 0) ? [[placemarks objectAtIndex:0] locality] : #"Not Found";
_ciudadLabel.text=location;
}];
}
// this delegate method is called if an error occurs in locating your current location
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSLog(#"locationManager:%# didFailWithError:%#", manager, error);
}
But when I execute the application, the message that says if I want to allow to get my location disapear suddenly and don't execute:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// this creates a MKReverseGeocoder to find a placemark using the found coordinates
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
NSString * location = ([placemarks count] > 0) ? [[placemarks objectAtIndex:0] locality] : #"Not Found";
_ciudadLabel.text=location;
}];
}
why the location doesn't start?
Thanks
i am using this code to get the Location ,using simulator , but its not giving me any output .
also if someone suggest me a solution of this or a better alternative solution.\
-(void)viewDidAppear:(BOOL)animated
{
_locationManager.delegate=self;
[_locationManager startUpdatingLocation];
[self.geoCoder reverseGeocodeLocation: _locationManager.location completionHandler:
^(NSArray *placemarks, NSError *error) {
if (error) {
return;
}
if (placemarks && placemarks.count > 0)
{
CLPlacemark *placemark = placemarks[0];
NSDictionary *addressDictionary =
placemark.addressDictionary;
NSString *address = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStreetKey];
NSString *city = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCityKey];
NSString *state = [addressDictionary
objectForKey:(NSString *)kABPersonAddressStateKey];
NSString *zip = [addressDictionary
objectForKey:(NSString *)kABPersonAddressZIPKey];
NSString *Countrynsme = [addressDictionary
objectForKey:(NSString *)kABPersonAddressCountryKey];
_requestorAddressText.Text = address;
_requestorCityText.text = city;
_requestorPostalText.text = zip;
_CountryrequestorText.text = Countrynsme;
_requestorStateText.text = state;
}
}];
[_locationManager stopUpdatingLocation];
}
CLLocationManager is an asynchronous API. You need to wait for the result of CLLocationManager before you geocode the location.
Start listening for location manager updates using the CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSTimeInterval interval = [newLocation.timestamp timeIntervalSinceNow];
if (interval < 0) {
interval = -interval;
}
// Reject stale location updates.
if (interval < 30.0) {
// Start geocoding
[geoCoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
// Use geocoded results
...
}];
}
// If you're done with getting updates then do [manager stopUpdatingLocation]
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
// Handle error. Perhaps [manager stopUpdatingLocation]
}
Then viewDidAppear just bootstrap's the location lookup:
- (void)viewDidAppear {
// PS: You're missing a call to [super viewDidAppear]
[super viewDidAppear];
// Start lookup for location
_locationManager.delegate=self;
[_locationManager startUpdatingLocation];
}
PS: In dealloc don't forget to stop updating location, cancel geocode and nil the delegates for locationManager.