Getting longitude,latitude without setting it all app life - iphone

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.

Related

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

Time interval between location updates, iphone

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.

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

Returning a users lat lng as a string iPhone

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

CLLocationManger gives old location. How to get the new location?

I am using the CLLocationManger to update the current location. Though its giving me the location but the location I am getting is of much older timestamp. I am facing the problem in, how to force the locationcontroller to update the fresh location not the cached location.
Currently I am using this tutorial......
http://www.mobileorchard.com/hello-there-a-corelocation-tutorial/
Anybody know how to update location at current timestamp not the cached one?
Are you running your project on simulator or on iPhone? If you are running project on simulator then
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
will be called only once.
If you are testing on device then it should be called everytime your location is changed.
Make sure you have set the location manager property,
locationManager.delegate=self;
[locationManager startUpdatingLocation];
Hope that helps...
you can use this methods
- (IBAction)update {
locmanager = [[CLLocationManager alloc] init];
[locmanager setDelegate:self];
[locmanager setDesiredAccuracy:kCLLocationAccuracyBest];
NSLog(#"*********this is location update method of login view controller");
[locmanager startUpdatingLocation];
}
-(void)awakeFromNib {
[self update];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
if (wasFound) return;
wasFound = YES;
CLLocationCoordinate2D loc = [newLocation coordinate];
lat2 = [[NSString stringWithFormat: #"%f", loc.latitude]retain];
logg2= [[NSString stringWithFormat: #"%f", loc.longitude]retain];
NSLog(#"latitude is %#",lat2);
NSLog(#"longitude is %#",logg2);
}
call the update method using perform selector in viewdidload method.Here lat2 and logg2 are string values.
if problem can't be solved then type how to find current location in google.you will get many examples source code with explanation
The best you can do is throw out locations that are too old by whatever standard you decide to set for your app. Restarting the location manager is supposed to tell the system you want a new location, but usually the first location you get back may be whatever it had last. Checking the timestamp may be all you can do, but be prepared for the possibility that you may never get a newer location. I've seen in my experience that if the device hasn't moved, it never bothers to update the system with a new location if it detects that the current location is good enough.