Show an UIAlert when delegate in LocationManager takes too long - iphone

I'm trying to create an alert when my LocationManager takes too long. In its delegate method, I check the newLocation's timestamp to make sure it's something recent:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 1.0) { // process time if time is less than 1.0 seconds old.
timer = [NSTimer scheduledTimerWithTimeInterval:1.0 target:self selector:#selector(updateCount:) userInfo:nil repeats:YES];
return;
}
After some debugging, I realize that putting timer in that if block isn't right. What ends up happening is, the timer never invalidates, or rather my updateCount: method keeps running. I think what's happening is the delegate keeps getting called back with a bad time, then it keeps running more timers, so I get UIAlert after UIAlert.
I've used the same updateCount: method in a test project to create a 30 second timer and invalidate it and it works fine. Now however, I am stuck since I basically want to find the person's location, but if it takes too long (> 30 seconds), throw an alert. I'm not really sure where I should put this kind of code. It seemed to me to put it in the delegate where I'm making the check of the timestamp since that's the error condition I'm looking to keep track of. But that doesn't seem to be working well for me.
Is there a better place to put this kind of check? Or is there a better way to do this kind of task altogether? I'm pretty new to programming, so my knowledge is limited. TIA.

I think what you should be doing is- set the 30 second timer when you call [locationManager startUpdatingLocation]; Now, you can update your method to look something like-
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSDate *eventDate = newLocation.timestamp;
NSTimeInterval howRecent = [eventDate timeIntervalSinceNow];
if (abs(howRecent) < 1.0) { //good time found
//use this location
//either remove the 30 second timer or set a boolean
self.goodLocationFound = YES;
[locationManager stopUpdatingLocation];
}
In your timer method, you could do-
{
[locationManager stopUpdatingLocation];
if(self.goodLocationFound == NO)
{
//display alert
}
}

Related

Calculate Accurate Speed Using GPS

I am using following code to update location after every 0.1 second -
- (void)viewDidLoad
{
[super viewDidLoad];
CLController = [[CoreLocationController alloc] init];
CLController.delegate = self;
// set auto update timer
currentTimer = [NSTimer scheduledTimerWithTimeInterval:0.1 target:self
selector:#selector(updatestart) userInfo:nil repeats:YES];
}
-(void)updatestart
{
[CLController.locMgr startUpdatingLocation];
}
And then I am trying to calculate speed using
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
[locMgr stopUpdatingLocation];
NSLog(#"%f",newLocation.speed);
}
I am getting speed in meter/sec but after some time the process slows down and even when I stop traveling the speed is being calculated.
What I want is to get accurate speed and show alert if speed is above 12km/hr.
Is there any other approach which I can use to find the same?
Thanks..
Starting and stopping the location manager like that isn't the way to do it. You can't force it to give more location updates. Leave it running and average the location updates over time. To get the best accuracy, use kCLLocationAccuracyBestForNavigation for the location manager's desiredAccuracy.

Any side effect of CLLocationManager with NSTimer using such approach?

I am working on an iPhone app which needs Location Updates with intervals specified by the user.
Here is the code sample, which I am using to do this:
#implementation TestLocation
- (void)viewDidLoad{
if ([Utils getDataWithKey:TIMER_INTERVAL] == nil) {
[Utils saveDataWithKey:TIMER_INTERVAL withValue:#"60.0"];
}
locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
[locationManager startUpdatingLocation];
}
- (void)startLocationManager:(NSTimer *)timer{
[locationManager startUpdatingLocation];
[timer invalidate];
timer = nil;
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
// Here is code to handle location updates...
[manager stopUpdatingLocation];
// Timer will start getting updated location.
NSTimeInterval timeInterval = [[Utils getDataWithKey:TIMER_INTERVAL] doubleValue];
[NSTimer scheduledTimerWithTimeInterval:timeInterval
target:self
selector:#selector(startLocationManager:)
userInfo:nil
repeats:NO];
}
// other implementations ...
#end
The code is working like a charm.
The question is:
I'm using CLLocationManager along with NSTimer, does this effect memory or battery consumption? I mean any negative effect on User Experience?
If so, any suggestion, helping links to do such task with optimization will be much appreciated.
Note: Utils is my class to store or retrieve data.
Yes this will have some side effects, you will not get the desired accuracy. Since it will call the locationManager:didUpdateToLocation:fromLocation: every time the GPS signal will
be come more accurate.
That isn't a good strategy because you can receive multiple asynchronous location events before the first call to [manager stopUpdatingLocation]. That will lead to exponential number of timers getting created.
Instead, just start the repeating timer after creating your location manager and still stop the location manager after each received event.

I can't get the gps coordinates for a persistent period of time

I have implemented the standard method of retrieving the coordinates from the gps using - (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation.
The problem is that this function is called only on initialization and not throughout the life of the program. Is this normal?
In android you would implement a listener and you would get data instantly.
Is this not the wright way how i'm doing it? If it is, what could be the problem? (btw i've checked, i don't stopUpdatingLocation)
I have a CLLocationManager inherited class named testing and initialize it
testing* cllm = [[testing alloc] init];
cllm.delegate = self;
i later start the updating
[cllm startUpdatingLocation];
self.locationManagerDelegate = delegate;
and later is called
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
but after that it isn't called anymore. I need it to be called frequently so that i may calculate the distance to a certain point X from where i am.
Agreed with #Matt, without more code the best solution I can offer is this to tell it to update every time the device is moved with:
[self.locationManager setDistanceFiler:kCLDistanceFilterNone]
Update
I went through past projects and found the code I believe you are looking for assuming your location manager subclass is working properly
- (void)viewDidLoad {
[super viewDidLoad];
//Location
// create new location manager
self.locationManager = [[CLLocationManager alloc] init];
self.locationManager.delegate = self;
self.locationManager.desiredAccuracy = kCLLocationAccuracyBest;
self.locationManager.distanceFilter = kCLDistanceFilterNone;
// start location manager
[self.locationManager startUpdatingLocation];
}
-(void) distanceBetweenUserandPin {
CLLocation *currentUserLocation = [[CLLocation alloc] initWithLatitude:_currentLocation.latitude longitude:_currentLocation.longitude];
CLLocation *currentPinLocation = [[CLLocation alloc] initWithLatitude:_pinLocation.latitude longitude:_pinLocation.longitude];
CLLocationDistance distanceBetweenUserAndPinMeters = [currentUserLocation distanceFromLocation:currentPinLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//This successfully saves Lat, Long Data to a point location
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(newLocation.coordinate.latitude, newLocation.coordinate.longitude);
NSLog(#"%f, %f", location.latitude, location.longitude);
//This assigns the value of location to the ivar _currentLocation
_currentLocation = CLLocationCoordinate2DMake(location.latitude, location.longitude);
NSLog(#"%f, %f", _currentLocation.latitude, _currentLocation.longitude);
}
First, it seems strange to me that you would use a subclass of CLLocationManager, since I'm not sure what benefit that provides you. Assuming that's not the problem, however...
From the CLLocationManager documentation:
This method returns immediately. Calling this method causes the
location manager to obtain an initial location fix (which may take
several seconds) and notify your delegate by calling its
locationManager:didUpdateToLocation:fromLocation: method. After that,
the receiver generates update events primarily when the value in the
distanceFilter property is exceeded. Updates may be delivered in other
situations though. For example, the receiver may send another
notification if the hardware gathers a more accurate location reading.
What's happening is that it is being called once for the initial position fix, but it isn't calling again because other conditions haven't changed. If a user doesn't move anywhere, then new location data won't be provided since it will be the same as last time (with a few exceptions as mentioned in the docs).
When you're testing your app, make sure that you try moving around and changing your location to produce an update. If that doesn't work, try experimenting with the desiredAccuracy and distanceFilter properties:
You start standard location services by calling the
startUpdatingLocation method. This service is most appropriate for
applications that need more fine-grained control over the delivery of
location events. Specifically, it takes into account the values in the
desiredAccuracy and distanceFilter property to determine when to
deliver new events.
Other than that, I'd guess it might have to do with how you're subclassing CLLocationManager. Providing some of that code might help.

Iphone core location

I have an application where I have to check that whether user's mobile location has changed or for 30 consecutive seconds or not?If it has not changed in 30 seconds then user will be navigated to the other view and if it has changed then user will have a message that your mobile location's coordinates have changed in these 30 seconds.
I am using these code but it does nothing ...
-(void)time
{
for(int i = 0; i<= timeremaining ;i++)
{
if (new1 == old)
{
if(timeremaining == 1)
{
Timer90 *timerview = [[Timer90 alloc] initWithNibName:#"Timer90" bundle:nil];
[self.navigationController pushViewController:timerview animated:YES];
}
}
else
{
[NSTimer cancelPreviousPerformRequestsWithTarget:self selector:#selector(time) object:nil];
}
timeleft.text = [NSString stringWithFormat:#"%d",timeremaining];
}
timeremaining--;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
new1 = newLocation;
old=oldLocation;
latitude = [[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.latitude];
longitude = [[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.longitude];
accuracy = [[NSString alloc] initWithFormat:#"%g",newLocation.horizontalAccuracy];
if(oldLocation == NULL)
{
oldLocation == newLocation;
}
}
here timer90 is name of my view.Please help me out friends...Any help will be appreciated.
You could possibly do something like this (after adding counter and initialLocation to your properties) it will compare location after 30 seconds following the first update, but I think it would be helpful if you added an age check to the entire method so that you aren't comparing to some very old data point.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if(self.counter==0){
self.intialLocation=newLocation;
self.counter=1;
}
NSTimeInterval age = [initialLocation.timestamp timeIntervalSinceNow];
if(age>30)
{
self.counter=0;
if(newLocation.latitude==initialLocation.latitude&&newLocation.longitude==initialLocation.longitude){
Timer90 *timerview = [[Timer90 alloc] initWithNibName:#"Timer90" bundle:nil];
[self.navigationController pushViewController:timerview animated:YES];
}
}
}
Two things that I notice. May be the whole issue, or only part of it.
Number 1.
if(oldLocation == NULL)
{
oldLocation == newLocation;
}
oldLocation == newLocation will not set the two equal to each other (you need one equals sign) and your are assigning the parameter pointers to the same object, not your member pointers. For example, if oldLocation points to Object A and newLocation points to Object B at the start of didUpdateToLocation:fromLocation:, then:
new1 is set to point at Object B
old is set to point at Object A
if(Object A is NULL)
oldLocation and newLocation both point at Object B
The values of new1 and old are not set to the same object in the NULL case. Which is what you are checking for in time
Number 2.
In time you are checking if new1 and old are pointing at the same object. No necessarily if the two objects represent the same location. In the case where oldLocation is null and they are set together, this check may pass...however, it is more likely that CoreLocation will call the update method a few times while accuracy gets better, and you will have two separate objects that represent the same lat/long location.
You should compare these two locations for proximity using the CLLocation getDistanceFromLocation: method.
Hope that Helps!

CLLocation speed

I am developing GPS application.
Do you know about how to detect speed of mobile device ?
Actually, I need to detect the speed every 2 seconds.
I know didUpdateToLocation method is called when location changed.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
But I think this method is not suitable for my issue.
So, Do I need to check speed of [CLLocationManager location] in 2 seconds ?
Any suggestion ?
Thanks in advance.
How about the code below which works from the delegate method.
Alternatively, if you did want to poll, then keep your previous location and check the distance changed from the last poll and use the manual method (also shown below) to calculate the speed.
Speed is calculated/provided in m/s so multiply by 3.6 for kmph or 2.23693629 for mph.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//simply get the speed provided by the phone from newLocation
double gpsSpeed = newLocation.speed;
// alternative manual method
if(oldLocation != nil)
{
CLLocationDistance distanceChange = [newLocation getDistanceFrom:oldLocation];
NSTimeInterval sinceLastUpdate = [newLocation.timestamp timeIntervalSinceDate:oldLocation.timestamp];
double calculatedSpeed = distanceChange / sinceLastUpdate;
}
}
You can only really use the delegate method you have suggested in your question.
Even if you access the [CLLocationManager location] every 2 seconds, you will only receive the coordinate you last received in the delegate method above.
Why the need to poll every two seconds? The iphone can update it's coordinates in less time on some cases.
HTH