How to get current geo points only once in iphone? - iphone

I am using following code in viewDidLoad method
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation;
[locationManager startUpdatingLocation];
Then below method is called continuously when location changed.
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
But I want current geo points only once. Is there any method which gives you directly current geo points? I want to calculate distance between current geo points and other location when I click some button. please help me if any one knows.

In - (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation you can call
[manager stopUpdatingLocation];

Related

CLLocationManager not returning location after temporarily disabling in settings

I temporarily disabled the location services, and the permissions for my app, so that I could test some code which dealt with the scenario where they're not available. Upon turning them on again, my location now can't be fetched, using this code:
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
[locationManager startUpdatingLocation];
CLLocation *currentLocation = locationManager.location;
[locationManager stopUpdatingLocation];
The locationManager.location is equal to nil after running this code.
I'm running this on an iPad running iOS 6.
Set the delegate for CLLocationManager
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startUpdatingLocation];
Try the delegates of CLLocationManager.
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
self.currentLocation = newLocation;
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
// The location "unknown" error simply means the manager is currently unable to get the location.
// We can ignore this error for the scenario of getting a single location fix, because we already have a
// timeout that will stop the location manager to save power.
if ([error code] != kCLErrorLocationUnknown) {
[self stopUpdatingLocation:NSLocalizedString(#"Error", #"Error")];
}
}

Getting longitude,latitude without setting it all app life

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.

Tracing the co ordinates of current location

I want to trace the latitude and longitude coordinates of the current location..I use tis code to trace it...
-(void)ViewDidLoad{
map = [[MKMapView alloc] initWithFrame:CGRectMake(0,46,320,370)];
[map setDelegate:self];
map.showsUserLocation=YES;
[map setZoomEnabled:YES];
[self.view addSubview:map];
[map setMapType:MKMapTypeStandard];
[self locationManager:locationManager didUpdateToLocation:newlocation fromLocation:oldlocation];}
- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)NewLocation
fromLocation:(CLLocation *)OldLocation
{
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters;
[locationManager startUpdatingLocation];
NSLog(#"%f %f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.latitude);
}
But my app crashes
My log report:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[Map locationManager:didUpdateToLocation:fromLocation:]: unrecognized selector sent to instance 0x5863890'
You're accessing the location too early. Depending on the actual state of the device, there might not be a location information there yet (as you NSLog is milli- or microconds after the startUpdatingLocation). The proper way to get location information is to set a delegate on you locationManager, e.g. self and implement this:
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
}
This way your app will be notified, as soon as there is a known location available.
you need to implement this delegate function ...
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation{
userLocation=newLocation.coordinate;
NSLog(#"updated location is %f",newLocation.coordinate.longitude);
}
and also implement CLLocationManagerDelegate in your .h
At the bottom of your viewDidLoad method you are calling didUpdateToLocation. You shouldn't do that. By setting the delegate of the map to be this class, it will call the method when the location is updated.
You also need to add space in the method name
- (void)locationManager:(CLLocationManager *)managerdidUpdateToLocation:(CLLocation *)NewLocation fromLocation:(CLLocation *)OldLocation
should be
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)NewLocation
fromLocation:(CLLocation *)OldLocation

How do I get a background location update every n minutes in iOS app?

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];
}

CLLoacationManager class returning cached data

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);