CLLocation, getting new locations - iphone

In More iPhone Programming book, the author does:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([newLocation.timestamp timeIntervalSince1970] < [NSDate timeIntervalSinceReferenceDate] - 60)
locationCoordinate = newLocation.coordinate;
return;
...
To make sure the data was taken in the last minute. Two questions:
1) What is the if statement doing. It seems like on the left hand side, you are getting the time difference in seconds between when this method fires and the 1970 date. Then on the right hand side, you get the difference in seconds between the 2001 date and now minus 60 seconds. So to me, the if statement would never be valid since the data on the left is always going to be a greater amount of seconds. Or am I understanding it wrong?
2) What does return in a void function do? Is that considered good coding here? Thx.

I don't know what is going on here, its confusing statement, I have use something like this
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if( abs(howRecent) > 1.0 )
return;
////process your event here
}
It is right to put return in void method, as I don't want to execute the statement next for some conditions..the same code can be written as
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
NSDate* eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if( abs(howRecent) < 1.0 ){
///process your event
}
}
it just depends upon your need.

for the if block :
the value return by timeIntervalSinceReferenceDate could be negative. see the description .
If the receiver is earlier than the
reference date, the value is negative.
So if condition could be false.
For the return statement.
if you want your function to return the control to the calling function before reaching the function closed bracket on some condition. See the eg.
-(void) SomeFunction
{
if(Condition1)
{
return;
}
}

Related

Objective-C - Errors when passing a variable into function

What is the easiest way to pass a var into another function ?
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"%#", started);
}
I tried:
Defined a global vars:
extern NSString *started;
When I set the NSString directly and passing into another function, it works well:
-(void) startTracking:(CDVInvokedUrlCommand*)command {
started = #"testing";
}
But it doesn't work:
-(void) startTracking:(CDVInvokedUrlCommand*)command {
NSString* myarg = [command.arguments objectAtIndex:0]; // http://docs.phonegap.com/en/2.5.0/guide_plugin-development_ios_index.md.html#Developing%20a%20Plugin%20on%20iOS_writing_an_ios_cordova_plugin
started = myarg;
}
(I'm a objective-C beginner, don't understand it well)
EDIT: Seems like it only crashed when I put the app into background.
Depending on wether you are using ARC, you have to retain the object. You'll probably want to use a property on your class:
in your header:
#property(strong) NSString *started;
in implementation:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
NSLog(#"%#", self.started);
}
-(void) startTracking:(CDVInvokedUrlCommand*)command {
self.started = #"testing";
}
-(void) startTracking:(CDVInvokedUrlCommand*)command {
NSString* myarg = [command.arguments objectAtIndex:0];
self.started = myarg;
}
Mate, seems like you want to track the date you started receiving location information.
How about doing it like this:
// Your .h file
#interface MyClass <CLLocationManagerDelegate>
{
BOOL hasStartedUpdatingLocation;
NSDate *startDate;
CLLocationManager *locationManager;
}
...
// Your .m file
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
// ---------------------------------------------------------------
// if has NOT started updating location, record start date
// otherwise, do not execute this if statement
// ---------------------------------------------------------------
if(!hasStartedUpdatingLocation)
{
hasStartedUpdatingLocation = YES;
// this if statement should only execute once
startDate = [NSDate date]; // get the current date and time
}
else
{
// do something else
}
}

Preventing sporadic coordinates using CoreLocation

Basically I'm making an app that is supposed to record a path and data that someone travels along. I'm having some trouble with accuracy and determining inaccurate reports. Here is an image of the path that my iPhone recorded. The red line is the recorded path, the pink is how I actually walked (apologies for the huge screenshot):
My delegate for recording the data looks like this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (newLocation.horizontalAccuracy < 0) return;
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
// The "> 50.0f" is to hopefully throw out inaccurate points
if (locationAge > 5.0 || [newLocation distanceFromLocation:oldLocation] > 50.0f) return;
[[self locationPoints] addObject:newLocation];
[self updateView];
}
Some advice that I've seen is to use filters to check if the data is appropriate for the projected path. Is this the best way to go?
You could add checking for newLocation.horizontalAccuracy:
if ((newLocation.horizontalAccuracy > 0.0) && (newLocation.horizontalAccuracy < 10.0))
Or, for your case:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (newLocation.horizontalAccuracy < 0) return;
    NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
   
    if ((locationAge > 5.0)||(newLocation.horizontalAccuracy < 0.0)||(newLocation.horizontalAccuracy > 10.0)) return;
    [[self locationPoints] addObject:newLocation];
    [self updateView];
}
This (locationAge > 5.0) condition seems weird. Do you really want it? If you keep it in your app will stop updating if device looses proper GPS signal for >5.0s.
One more thing: did you remeber to setup your locationManager with:
locationManager.desiredAccuracy=kCLLocationAccuracyBest;
You should also know that this eats up quit a lot of battery, but for testing...

how to perform if else statement on altitude

This is my first time posting here so I would greatly appreciate the help. I have searched Google as well as this site for help and have not found anything quite like what I am looking for. I am trying to take the altitude
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
altitudeField.text = [NSString stringWithFormat: #"%.2f ft", newLocation.altitude * 3.2808399];
}
-(void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
altitudeField.text = #"0.00 ft";
}
Which gives me something like "500.00 ft" for instance. I have another textfield that I would like to fill in based on the altitude value in altitudeField.text. Where if the altitude is <1000 ft it equals 1.08 in textField6, from 1000 to 2000 ft it equals 1.04 in textField6, and so on in 1000 ft increments...
The original altitude is in meters, but I just multiply it to get it into feet, so must I look at the original value in meters? Or can I look at the actual value in the altitudeField?
I have been trying to manipulate some standard if-else statements I have found but I get nothing but errors. Any help or direction would be greatly appreciated.
Why not just do this:
-(void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
double altitudeFt = newLocation.altitude * 3.2808399;
altitudeField.text = [NSString stringWithFormat: #"%.2f ft", altitudeFt];
double otherValue = 1.08 - 0.4 * floor(altitudeFt / 1000);
otherField.text = [NSString stringWithFormat:#"%.2f", otherValue];
}
It sounds like you need to create a variable to store the numeric value of your altitude. You can then make what ever adjustments/conversions/formatting necessary when you set a particular field's text.

best way to get changed location iPhone sdk?

My ultimate goal is that i need to send lat and logitude to web server, every time when location changes.
I am using below code for sending lat and longitude of a device to a web server after every 2 minutes but it is not giving sometimes correct latitude and longitude as location changes.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation
*)newLocation fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval locationAge = -[newLocation.timestamp timeIntervalSinceNow];
NSLog(#"Location Age%#",[NSString stringWithFormat:#"%d",locationAge]);
if (locationAge > 5) return;
// test that the horizontal accuracy does not indicate an invalid measurement
if (newLocation.horizontalAccuracy < 0) return;
NSString *stringUrl;
// BOOL check = FALSE;
NSLog(#"Before condition condition");
if(bestEffortAtLocation == nil || bestEffortAtLocation.horizontalAccuracy > newLocation.horizontalAccuracy){
NSLog(#"condition");
self.bestEffortAtLocation = newLocation;
if (newLocation.horizontalAccuracy <= locmanager.desiredAccuracy) {
// we have a measurement that meets our requirements, so we can stop updating the location
// IMPORTANT!!! Minimize power usage by stopping the location manager as soon as possible.
[locmanager stopUpdatingLocation];
locmanager.delegate = nil;
}
//check = TRUE;
}
stringUrl = [NSString stringWithFormat:URLSAVELAT,stringUserId,[NSString stringWithFormat:#"%g",self.bestEffortAtLocation.coordinate.latitude],[NSString stringWithFormat:#"%g",self.bestEffortAtLocation.coordinate.longitude]];}
For location manager i am using below code
{
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
locmanager.distanceFilter = 10.0;
//locmanager.distanceFilter = kCLDistanceFilterNone;
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
[locmanager startUpdatingLocation];
}
Any,even small help would be appreciated, Thank u in advance
You should use the location update call backs from the API and use updateLocation method:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([self.delegate conformsToProtocol:#protocol(CoreLocationControllerDelegate)]) {
[self.delegate locationUpdate:newLocation];
}
}
Then in the viewcontroller do this:
- (void)locationUpdate:(CLLocation *)location {
//DO WHATEVER YOU WANT HERE, INCLUDING SENDING TO SERVER
}
You also need to define two protocol methods, one of which is the locationUpdate:
#protocol CoreLocationControllerDelegate
#required
- (void)locationUpdate:(CLLocation *)location;
- (void)locationError:(NSError *)error;
#end
I would not recommend doing all that you do in the didUpdateLocation: method.

CLLocationManager didUpdateToLocation checking failing

I am trying to get the location coordinates using CLLocationManager. Here is my code
- (void)viewDidLoad {
[super viewDidLoad];
//instantiate location manager and set delegate
self.locationManager=[[CLLocationManager alloc]init];
locationManager.delegate=self;
// can be set to 100m,1km,3km etc.
//locationManager.distanceFilter=10.0f;
locationManager.desiredAccuracy=kCLLocationAccuracyNearestTenMeters;
//start updating the delegate
[locationManager startUpdatingLocation];
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
// Check if the coordinates are different from the old ones
if (newLocation.coordinate.latitude != oldLocation.coordinate.latitude &&
newLocation.coordinate.longitude != oldLocation.coordinate.longitude) {
NSLog(#"not equal");
} else {
NSLog(#"equal");
}
}
However i find the condition is called twice. First time the condition is satisfied and prints not equal and immediately its called again and prints "equal". Can some1 help me out ? What am i doing wrong ?
Thanks
This is because CoreLocation cache your last location and return it immediately after you called startUpdatingLocation so you have to validate coordinate timestamp and it is too old, you can ignore old coordinate.
UPDATE:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if([newLocation horizontalAccuracy] < 0.0f) return;
if(fabs([[newLocation timestamp] timeIntervalSinceNow]) > kCLLocationMaximumLocationDataAge) return;
if(fabs([[oldLocation timestamp] timeIntervalSinceNow]) < kCLLocationMaximumLocationDataAge && [newLocation getDistanceFrom:oldLocation] < 0.1f && [newLocation horizontalAccuracy] == [oldLocation horizontalAccuracy])
return;
if(((runningHighPreciseLocationDetectionService||runningLowPowerLocationDetectionService) && ([newLocation horizontalAccuracy] <= kCLLocationAccuracyHundredMeters))){
NSLog(#"---> \n%#\n%#\nHorizontal accurecy: %f\nLocation age: %fs\nOld location age: %fs", NSStringFromSelector(_cmd), newLocation, newLocation.horizontalAccuracy, fabs([[newLocation timestamp] timeIntervalSinceNow]), fabs([[oldLocation timestamp] timeIntervalSinceNow]));
} else {
NSLog(#"\n-------------- BAD ---------------\n%#\n%#\nHorizontal accurecy: %f\nLocation age: %fs\nOld location age: %fs\n----------------------------------", NSStringFromSelector(_cmd), newLocation, newLocation.horizontalAccuracy, fabs([[newLocation timestamp] timeIntervalSinceNow]), fabs([[oldLocation timestamp] timeIntervalSinceNow]));
}
if(((runningHighPreciseLocationDetectionService||runningLowPowerLocationDetectionService) && ([newLocation horizontalAccuracy] <= kCLLocationAccuracyHundredMeters))){
[self stopLocationImprovementTimer];
} else [self createLocatinoImprovementTimer];
}
createLocatinoImprovementTimer method used to launch timer, which will work for certain amount of time and if it wasn't stopped it will send and update notification with location, which was last. This timer will help to wait for coordinate with better accuracy before notify controllers.