Tracing the co ordinates of current location - iphone

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

Related

Location manager delegate not getting called

I am using location service to keep my app alive for longer, its working fine in my iOS6 (Device and Simulator), but in iOS7 its not calling this delegate function of CLLocationManager,
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
How to make this work in iOS7?
This method is Deprecated in iOS 6.0
Use this method
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
location_updated = [locations lastObject];
NSLog(#"updated coordinate are %#",location_updated);
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
{
location_updated = [locations lastObject];
NSLog(#"updated coordinate are %#",location_updated);
}

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

How to get current geo points only once in 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];

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.