How to remove cached data which is sent by CLLocationManager and how to increase its accuracy.Every time i launch the app it gives me cached data and updates it after some time.I have seen several threads but i am not able to get a concrete sol .can anybody provide me the code?
I am using the following method to get position data....
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
I have set the delegate and accuracy here.....
- (void)viewDidLoad {
[super viewDidLoad];
bestEffortAtLocation = nil;
latLocationArray = [[NSMutableArray alloc]init];
longLocationArray = [[NSMutableArray alloc]init];
locationManager =[[CLLocationManager alloc]init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
Also can i use CLLocationManager to compute the distance travelled by person?
There doesn't seem to be a way to dump the cached data. The docs recommend using the timestamp property of the CLLocation object to determine how recent the data is, which is what I do.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
//wait until we get a recent location, no more than 2 minutes old
if([newLocation.timestamp timeIntervalSinceNow] <= (60 * 2)){
DLog(#"New location:\n\tLat: %f\n\tLon: %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude)
//turn off location updates
[self.locManager stopUpdatingLocation];
DLog(#"Stopping Location Updates");
//Do stuff
}
}
In my testing I haven't had to wait more than a second or two before I get new data.
On your second point, you could have a variable that you add to whenever you get a location update. The distance between updates can be pretty easy to get using CLLocation's distanceFromLocation: method.
Instead of set the distanceFilter to kCLDistanceFilterNone
i.e. locationManager.distanceFilter = kCLDistanceFilterNone;
You may give some value to distanceFilter and check
For e.g. locationManager.distanceFilter =0.5f;
And to calculate distance travel you have to implement the didUpdateToLocation: fromLocation:
where you have to find the new latitude and longitude
Finally
CLLocationObject = newLocation;
CLLocationDistance yourdistance = [newLocation getDistanceFrom:CLLocationObject];
NSString *distance = [[NSString alloc]
initWithFormat:#"%gm", yourdistance ];
NSLog(#"you have travel=%#",distance);
Related
I implement the following code in viewDidLoad
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
and i try to get my location every second via
CLLocation *location = [locationManager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSLog(#"[%f,%f]",coordinate.latitude,coordinate.longitude);
i try to set the custom location in the iOS simulator, but the location never get updated,
i had also tried it on my iphone device and it seems it only capture the first location and subsequent location is not been updated.
Please advise me on how to resolve it
Try this method to get continuous updated latitude and longitude
(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
CLLocation *location_updated = [locations lastObject];
NSLog(#"updated coordinate are %#",location_updated);
}
I want simple get lan,lon valus at the time application start,I dont want to update every second those values as I am changing my possition. I am using this code:
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
latValueNSString = [NSString stringWithFormat: #"%f",newLocation.coordinate.latitude];
lanValueNSString = [NSString stringWithFormat: #"%f",newLocation.coordinate.longitude];
}
But the problem that I cant transfer those values because every millisecond its setting it again and again... how can i set those values only ones and that's it??
If you only need 1 value, just get the first location and tell the location manager to stop updating the location.
[manager stopUpdatingLocation];
do this when the delegate method is called.
Call [locationManager stopUpdatingLocation]; once you get a result. You may want to check if the accuracy is what you need before calling stop.
I appreciate that this question already appears to have been answered here:
How do I get a background location update every n minutes in my iOS application?
However, although the solution is hinted at, I'd be really grateful if someone could post some sample code as to how this actually works.
Many thanks,
Jack
You need to subscribe to core location during application start.
some thing like this
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyThreeKilometers;
and add delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
Try this one you will get the lattitude and longitude updated..in didUpdateToLocation...
//in view did load
locationManager = [[CLLocationManager alloc]init];
if([CLLocationManager locationServicesEnabled])
{
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
locationManager.distanceFilter = 1000.0f;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
appDelegateObj.latitude= [[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.latitude];
appDelegateObj.longitude = [[NSString alloc]initWithFormat:#"%g",newLocation.coordinate.longitude];
}
I have a CLLocation Manager called "myLocation"
myLocation = [[CLLocationManager alloc] init];
myLocation.desiredAccuracy = kCLLocationAccuracyBestForNavigation ;
myLocation.delegate=self;
[myLocation startUpdatingLocation];
I need to know time interval between location updates. I am using timestamp to find when new location was found as follows in didUpdateToLocation method
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSTimeInterval locationAge = abs([oldLocation.timestamp timeIntervalSinceNow]);
NSString *timeinNSString = [NSString stringWithFormat:#"%f", locationAge];
NSLog(#"Location Age is %#", timeinNSString);
}
But NSlog always prints 0. any Idea what I am missing here?
Thanks for any help in advance.
abs() doesn't work with floating point numbers. Use fabs() instead. In the future, use breakpoints and step through your code in the debugger to check where it actually fails.
Is there a way to return the users location as a string from a model?
I have a model thats job is to download same JSON data from a web service. When sending in my request I need to add ?lat=LAT_HERE&lng=LNG_HERE to the end of the string.
I have seen tons of examples using the map or constantly updating a label. But I cant find out how to explicitly return the lat and lng values.
Im only 2 days into iPhone dev so go easy on me :)
You need to leverage Core Location, specifically CLLocationManager. Apple doesn't provide any CL programming guide, so just look at one of the samples like LocateMe to see how to do it.
You need to use CLLocationManager like this:
- (void)viewDidLoad
{
// this creates the CCLocationManager that will find your current location
CLLocationManager *locationManager = [[[CLLocationManager alloc] init] autorelease];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
}
// this delegate is called when the app successfully finds your current location
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
// retrieve lat and lng in a string from newLocation.coordinate
NSString *lat = [NSString stringWithFormat:#"%d", newLocation.coordinate.latitude];
NSString *lng = [NSString stringWithFormat:#"%d", newLocation.coordinate.longitude];
}
// 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);
}