CLLocationDistance: Cant get "getDistanceFrom" to work - iphone

view based app,
.m
- (void)viewDidLoad {
[super viewDidLoad];
self.locationManager = [[[CLLocationManager alloc] init] autorelease];
self.locationManager.delegate = self;
[[self locationManager] startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
CurrentLocation = newLocation;
[locationManager stopUpdatingLocation];
myActivity.hidden=YES;
[self reStart];
}
-(void)reStart{
[self checkAndCreateDatabase]; //Sucess
[self readFromDatabase]; //Success if I comment the get distance part
[self sort]; //Success
}
-(void) readFromDatabase {
...
CLLocationCoordinate2D cord;
cord.latitude = [[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 2)] doubleValue];
cord.longitude =[[NSString stringWithUTF8String:(char *)sqlite3_column_text(compiledStatement, 3)] doubleValue];
NSDate *today = [NSDate date];
CLLocation *objectLocation = [[CLLocation alloc] initWithCoordinate:cord altitude:1 horizontalAccuracy:1 verticalAccuracy:-1 timestamp:today];
CLLocationDistance distance = [objectLocation getDistanceFrom:CurrentLocation]/1000;
NSLog([NSString stringWithFormat:#"%.0f", distance]);
}
What is wrong?
debugger says:
obj_msgSend
0x912e8688 <+0024> mov 0x20(%edx),%edi
object from database:
long: 59.291114
lat: 17.816191

Use distanceFromLocation: instead

Is CurrentLocation an instance-variable/property? You are setting it directly without an accessor and you are not retaining it. If it is released before you use it in getDistance: it will cause a crash.
If it is a property use the self.CurrentLocation to ensure it is retained.
Also, the construction...
NSLog([NSString stringWithFormat:#"%.0f", distance]);
... is risky and redundant. Use...
NSLog(#"%.0f", distance);
... instead.

Related

CLLocationManager always locates London

I have the following lines of code to locate my position, but I always get the the same location regardless of where I am:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations {
CLLocation *location = [locations lastObject];
self.lati = [NSString stringWithFormat:#"%f",location.coordinate.latitude];
self.longi = [NSString stringWithFormat:#"%f",location.coordinate.longitude];
[locationManager stopUpdatingLocation];
locationManager.delegate = nil;
}
in .h write
<CLLocationManagerDelegate>
then this also
CLGeocoder *geocoder;
CLLocationManager *locationManager;
then in .m
locationManager = [[CLLocationManager alloc] init] ;
locationManager.delegate = self;
[CLLocationManager locationServicesEnabled];
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLHeadingFilterNone;
[locationManager startUpdatingLocation];
and then implement its delegates
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation(CLLocation*)newLocation fromLocation:(CLLocation *)oldLocation
{
[manager stopUpdatingLocation];
userCoordinate=newLocation.coordinate;
[[NSUserDefaults standardUserDefaults] setFloat:userCoordinate.latitude
forKey:#"userLat"];
[[NSUserDefaults standardUserDefaults] setFloat:userCoordinate.longitude
forKey:#"userLong"];
CLGeocoder * geoCoder = [[CLGeocoder alloc] init];
[geoCoder reverseGeocodeLocation:newLocation completionHandler:^(NSArray *placemarks, NSError *error) {
for (CLPlacemark * placemark in placemarks)
{
NSString *cityName = #"";
NSString *countryName = #"";
cityName = [placemark.addressDictionary valueForKey:#"City"];
countryName = [placemark.addressDictionary valueForKey:#"Country"];
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
[prefs setObject:cityName forKey:#"CityName"];
[prefs setObject:countryName forKey:#"CountryName"];
}
}];
[manager stopUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
[manager stopUpdatingLocation];
if(error.code == kCLErrorDenied)
{
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:#"Warning" message:#"You have to select \"Allow\" for current location." delegate:self cancelButtonTitle:#"Ok"
otherButtonTitles: nil];
alert.tag = 888;
[alert show];
}
[[NSUserDefaults standardUserDefaults] setFloat:0.0
forKey:#"userLat"];
[[NSUserDefaults standardUserDefaults] setFloat:0.0
forKey:#"userLong"];
[manager stopUpdatingLocation];
}
Use this delegate method in place of your delegate method. It will work for you.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
myLatitude = newLocation.coordinate.latitude;
myLongitude = newLocation.coordinate.longitude;
NSLog(#"LAT AND LON IS %f %f",myLatitude,myLongitude);
}

CLGeocoder not working

- (BOOL)application:(UIApplication *)application didFinishLaunchingWithOptions:(NSDictionary *)launchOptions {
// Override point for customization after app launch
[window addSubview:viewController.view];
[window makeKeyAndVisible];
[window addViewForTouchPriority:viewController.view];
if(self.locationManager==nil){
locationManager=[[CLLocationManager alloc] init];
locationManager.delegate=self;
locationManager.purpose = #"We will try to tell you where you are if you get lost";
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
locationManager.distanceFilter=500;
self.locationManager=locationManager;
}
geoCoder = [[CLGeocoder alloc] init];
if([CLLocationManager locationServicesEnabled]){
[self.locationManager startUpdatingLocation];
}
return YES;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
lati = [[NSString alloc] initWithFormat:#"%+.6f", newLocation.coordinate.latitude];
NSLog(#"address:%#",lati);
longi = [[NSString alloc] initWithFormat:#"%+.6f", newLocation.coordinate.longitude];
NSLog(#"address:%#",longi);
CLLocationCoordinate2D coord;
coord.latitude = [lati doubleValue];
coord.longitude = [longi doubleValue];
[geoCoder reverseGeocodeLocation: newLocation completionHandler: ^(NSArray *placemarks, NSError *error)
{
//Get nearby address
CLPlacemark *placemark = [placemarks objectAtIndex:0];
//String to hold address
NSString *locatedAt = [[placemark.addressDictionary valueForKey:#"FormattedAddressLines"] componentsJoinedByString:#", "];
//Print the location to console
NSLog(#"I am currently at %#",locatedAt);
address = [[NSString alloc]initWithString:locatedAt];
NSLog(#"address:%#",address);
}];
}
I am using above code to get address by giving latitude and longitude, but control is not entering in to geocoder method, it skips it. Can anybody help me in it.
Thanks in advance.
It doesn't matter, control does not enter in the method, but it works, note the output it will work.
that is normal, because the code inside the reverseGeocodeLocation is execute inside a block.
The execution of the code inside a block occurs in another thread, so is not execute in the main thread, that's why the control don't enter inside the geocoder method.

Error in getting current country

I am using following code to get current country
NSLocale *locale = [NSLocale currentLocale];
NSString *countryCodes = [locale objectForKey:NSLocaleCountryCode];
NSString *countryName = [locale displayNameForKey:NSLocaleCountryCode value:countryCodes];
NSLog(#"%#",countryName);
I am in UAE , but this is showing United States , I testing on my iPod , please help how Can I get current country ?
-(void)viewWillAppear:(BOOL)animated
{
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
[super viewWillAppear:animated];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
MKReverseGeocoder *geoCoder = [[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate];
geoCoder.delegate = self;
[geoCoder start];
}
- (void)reverseGeocoder:(MKReverseGeocoder *)geocoder didFindPlacemark:(MKPlacemark *)placemark
{
NSLog(#"Detail is:--> %#",placemark);
NSString *strCountry = [NSString stringWithFormat:#"Current Cuntry %#",[placemark.addressDictionary objectForKey:#"Country"]];
NSLog(#"label detail is:--> %#",strCountry);
lblAddress.text = strCountry;
}
Try this code...This will definately give you a current country detail.

Memory leak using location manager

I use the location manager with "didUpdateToLocation". I have a memory leak that I cannot explain:
- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation
fromLocation:(CLLocation *) oldLocation {
[newLocation retain];
NSString *lCoordinates = [[NSString alloc] initWithFormat:#"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];//Memory leak here!!!
[self setLocationCoordinates:lCoordinates];
[lCoordinates release];
NSString *lat = [[NSString alloc] initWithFormat:#"%f,%f", newLocation.coordinate.latitude,newLocation.coordinate.longitude];
[lm stopUpdatingLocation];
NSMutableString *s = [[NSMutableString alloc]initWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%#&radius=10&sensor=true&key=---MyKey---", lat];
NSLog(s);
id delegate = self;
AsyncConnectionController * connectionController = [[[AsyncConnectionController alloc] initWithDelegate:delegate
selSucceeded:#selector(currentLocationConnectionSucceeded:)
selFailed:#selector(currentLocationconnectionFailed:)] autorelease];
NSURL *url = [[NSURL alloc] initWithString:s];
[connectionController startRequestForURL:url];
[lat release];
[s release];
[url release];
[newLocation release];
}
Thanks all for your help!!!
You should be able to reduce your code substantially just by using convenience methods (makes it more readable as well):
- (void) locationManager:(CLLocationManager *) manager didUpdateToLocation:(CLLocation *) newLocation fromLocation:(CLLocation *) oldLocation {
NSString *lCoordinates = [NSString stringWithFormat:#"%f,%f",newLocation.coordinate.latitude,newLocation.coordinate.longitude];
[self setLocationCoordinates:lCoordinates];
NSString *lat = [NSString stringWithFormat:#"%f,%f", newLocation.coordinate.latitude,newLocation.coordinate.longitude];
[lm stopUpdatingLocation];
NSString *s = [NSString stringWithFormat:#"https://maps.googleapis.com/maps/api/place/search/json?location=%#&radius=10&sensor=true&key=---MyKey---", lat];
NSLog(s);
AsyncConnectionController * connectionController = [[[AsyncConnectionController alloc] initWithDelegate:self
selSucceeded:#selector(currentLocationConnectionSucceeded:)
selFailed:#selector(currentLocationconnectionFailed:)] autorelease];
NSURL *url = [NSURL urlWithString:s];
[connectionController startRequestForURL:url];
}
OP specifies that connectionController is autorelease.

Core Location returning 00

Here's the full method, I didn't put the second half originally because I know it works:
-(void)showDirections
{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDelegate:self];
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSNumber *myLatitude = [NSNumber numberWithDouble:coordinate.latitude];
NSNumber *myLongitude = [NSNumber numberWithDouble:coordinate.longitude];
double myLatitude2 = [myLatitude doubleValue];
double myLongitude2 = [myLongitude doubleValue];
NSArray *array = [dataHold objectForKey:#"Subtree"];
NSString *latitude = [NSString stringWithFormat:#"%#",[array objectAtIndex:4]];
NSString *longitude = [NSString stringWithFormat:#"%#",[array objectAtIndex:5]];
double clubLatitude = [latitude doubleValue];
double clubLongitude = [longitude doubleValue];
CLLocationCoordinate2D start = { myLatitude2, myLongitude2};
CLLocationCoordinate2D destination = { clubLatitude, clubLongitude};
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f",start.latitude, start.longitude, destination.latitude, destination.longitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}
Two issues:
1) You are doing a NSNumber box/unbox cycle that has no use whatsoever
2) Core Location is asynchronous, therefore you cannot return the user's location straight away, you must use the delegate method(s) to retrieve the location if/when it is available.
Here's an example of how it should work:
-(void)showDirections
{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDelegate:self];
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
CLLocationCoordinate2D coord = [newLocation coordinate];
NSArray *array = [dataHold objectForKey:#"Subtree"];
NSString *latitude = [NSString stringWithFormat:#"%#",[array objectAtIndex:4]];
NSString *longitude = [NSString stringWithFormat:#"%#",[array objectAtIndex:5]];
double clubLatitude = [latitude doubleValue];
double clubLongitude = [longitude doubleValue];
NSString *googleMapsURLString = [NSString stringWithFormat:#"http://maps.google.com/?saddr=%1.6f,%1.6f&daddr=%1.6f,%1.6f", coord.latitude, coord.longitude, clubLatitude, clubLongitude];
[[UIApplication sharedApplication] openURL:[NSURL URLWithString:googleMapsURLString]];
}
The fourth line of your code is where the problem begins. You cannot get the location instantly, you will need to wait until the location services notify the delegate that a location is available. I will add some comments to your code to explain:
-(void)showDirections
{
locationManager = [[CLLocationManager alloc] init];
[locationManager setDesiredAccuracy:kCLLocationAccuracyNearestTenMeters];
[locationManager setDelegate:self];
// this is not where you should be looking
CLLocation *location = [locationManager location];
// the rest of this function is not included in this example ...
}
Since you set the delegate as "self", the current class but implement the "CLLocationManagerDelegate" protocol, which defines the following two functions:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { /* */ }
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error { /* */ }
That is where you will get a location provided to you.
Dont forget to update your class definition like so:
#interface MyClass : NSObject <CLLocationManagerDelegate> {
And as a side note, it seems like you do not fully understand delegates and the purpose that they serve, which is fine if you are new to Objective-C, but getting more familiar with them will certainly make your life a bit easier.
Some other helpful stuff:
A CoreLocation Tutorial (covers what you are trying to do)