Getting Location Update only when app is in Background - iphone

I am trying to get the location updates Particularly when the application is in background. When the application enters background it should send the location updates for every n minutes
First I tried with the Foreground, it is working fine. I copied the same code to the applicationDidEnterBackground, I am unable to print the Location Updates. I don't know where I am struck please help me.
in viewDidLoad
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
[locationManager startUpdatingLocation];
}
and
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longLabel.text = longt;
}
I tried pasting these methods in the AppDelegate. It is not working :( . Help me

You can't simply paste the code there and expect your application to run in the background. You have to register it as an app that runs in the background. See the section Implementing Long-Running Background Tasks in this article

As per discussion with comments I suggested you to create entry in plist.
UIBackgroundModes for location in plist
And this helped.
EDIT
When you log the coordinate you need to save time somewhere probably in NSUserDefaults. After that when next time you come to log you need to find difference between current time and stored in NSUserDefaults. If interval is what you want than do log the coordinate.
Hope this helps.

Related

How can i achieve the accurate and same lat long even when i revisit the same place

I want this functionality to achieve, that when the user repeats the visit to same place suppose a person moves one meter forward then one meter back. He should get the same lat long value what he gets his previous time. I mean to accuracy for every footstep the user takes with device. How can i achieve this. Currently i am using CLLocationManager but it is giving the difference of 20 to 30 points at last in lat long when i moves at same place again.
Please help. Thanks in advance.
locationManager = [[CLLocationManager alloc] init];
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // 100 m
locationManager.distanceFilter = 0.1f;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSDate *locationTimeStamp = newLocation.timestamp;
NSTimeInterval delta = [locationTimeStamp timeIntervalSinceNow];
NSLog(#"The location timestamp interval was %f seconds.", delta);
_latitude.text = [NSString stringWithFormat:#"%f",newLocation.coordinate.latitude];
_longitude.text = [NSString stringWithFormat:#"%f",newLocation.coordinate.longitude];
_time.text = [NSString stringWithFormat:#"%f",delta];
}

How to get device current latitude and longitude for IPhone device

I'm developing an app were I would like to get device current latitude and longitude for IPhone. Is it possible to obtain this information without using GPS.
Thanks
you can use CoreLocation framework
The Core Location framework has one important class CLLocationManager.
The CLLocationManager class handles sending out updates to a delegate anytime the location is changed. it uses the delegate CLLocationManagerDelegate protocol.
you can use it like
#import <CoreLocation/CoreLocation.h>
#interface ViewController : UIViewController
<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
}
set up locationManager like this in viewDidLoad
- (void)viewDidLoad {
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
implement the below delegate mehod for getting changed location
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSInteger degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lattitudeStr = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
lattitudeLabel.text = lattitudeStr;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longitudeStr = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longitudeLabel.text = longitudeStr;
}
You can use CLLocationManager to get current lat and long.
create an iVar of CLLocationManager and use it.
Here is the code:
- (void)viewDidLoad
{
[super viewDidLoad];
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
NSLog(#"OldLocation %f %f", oldLocation.coordinate.latitude, oldLocation.coordinate.longitude);
NSLog(#"NewLocation %f %f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
currentLat = [NSString stringWithFormat:#"%f",newLocation.coordinate.latitude];
currentLong = [NSString stringWithFormat:#"%f",newLocation.coordinate.longitude];
}
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
CLLocation* currentLocation = [locationManager location];
NSString *loc = [NSString stringWithFormat:#"%f,%f",currentLocation.coordinate.latitude, currentLocation.coordinate.longitude];
NSLog(#"location:%#", loc);
You can get current location without GPS by using CLLocationManager if the device has WiFi or cellular data service. The location will not be as accurate as you would get from GPS but you get a result faster than from GPS.
You can request a non-GPS location from CLLocationManager by setting the desiredAccuracy property to kCLLocationAccuracyKilometer.
Using Swift
import CoreLocation
Get current location
let locationManager = CLLocationManager()
locationManager.distanceFilter = kCLDistanceFilterNone
locationManager.desiredAccuracy = kCLLocationAccuracyBest
//get the most recently retrieved user location
if let currentLocation = locationManager.location {
let longitude = currentLocation.coordinate.longitude
let latitude = currentLocation.coordinate.latitude
//work with the received data
}
Note that locationManager.location is an optional and hence should be safely unwrapped.

Terminating app due to uncaught exception on ipad/iphone

I'm getting a crash from this routine (IBAction)curr:(id)sender.
So I could determine which button called it.
The crash occurs when i press the button which calls the above method(curr).
It has to display current user location eventually it does but it crashes immediately after showing latitude and longitude.
received data as string
{
Error = "Lat must be provided";
Success = 0;
}
It throws exception:Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFDictionary objectAtIndex:]: unrecognized selector sent to instance 0x6e20000'
Here is my code:
-(IBAction)curr:(id)sender
{
count=0;
NSLog(#"fhfg");
NSLog(#"%#",latu);
NSLog(#"%#",longs);
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if (count==0)
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
latu= [NSString stringWithFormat:#"%d° %d' %1.4f\"", degrees, minutes, seconds];
//a=latu;
NSLog(#" Current Latitude : %#",latu);
//latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
longs = [NSString stringWithFormat:#"%d° %d' %1.4f\"", degrees, minutes, seconds];
NSLog(#" Current Longitude : %#",longs);
}
}
you can check your .xib file and check first responder by right click on first responder cube symbol.
IF there is any warning symbol in first responder connect outlets than that is the reason for your application crash.

Passing Current longitude & latitude on buttonclick event

Hello I am using CoreLocation to get latitude and longitude in my map application to get latitude and longitude by using following method
- (void)viewDidLoad {
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
[super viewDidLoad];
}
The _ (void)locationManager.... method is not getting call
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
}
I want to call lati & longi on Button click event to URL. But it is not working. it passes 0000.0 lati & longi to my url. Can some one tell me how to rectify this code
I don't see any obvious problem with your init of CLLocationManager. If you are not getting callbacks perhaps it doesn't yet have a location available. You should also implement the error callback and see if it is getting called instead.
I suggest you try one of the CLLocationManager examples provided by Apple and see if you can get the example to run.
I have did this to pass latitude and longitude in to web service URL on button click...
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
if((fabs(newLocation.coordinate.latitude) > 0.001) || (fabs(newLocation.coordinate.longitude) > 0.001)) {
NSLog(#"Got location %f,%f", newLocation.coordinate.latitude, newLocation.coordinate.longitude);
if (currentLocation != nil) {
[currentLocation release];
}
currentLocation = newLocation;
[currentLocation retain];
}}
and On click event code I have just pass locationmanager.location.coordinate.latitude.....
NSString *url = [NSString stringWithFormat:#"http://.....Url..../hespdirectory/phpsqlsearch_genxml.php?lat=%f&lng=%f&radius=%f",locationManager.location.coordinate.latitude,locationManager.location.coordinate.longitude,radius];
U can directly change the CLLocation to NSString
NSString *lat=[NSString stringWithFormat:#"%f", newLocation.latitude];
NSString *lon=[NSString stringWithFormat:#"%f", newLocation.longitude];
The answers don't show it directly, but you should have assigned your controller as the CLLocationManagerDelegate to recieve the callbacks correct?

Using Map Kit how can I find my location?

I need to display my current position in Map Kit, how can I do this ?
self.map.showsUserLocation = TRUE;
is not showing my location instead a point somewhere else in the map? I need to find the location from where I am accessing the application.
Please help me in this..
Thanks in advance
You need to use location manager for getting your current location. And then get the lat long value and create a annonation and display on map. If you use map.showsUserLocation = TRUE; it will show your current location in actual device.
Note - In simulator map kit shows apple's head quarter location somewhere in Cupertino - I don't remember exactly.
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{
int degrees = newLocation.coordinate.latitude;
double decimal = fabs(newLocation.coordinate.latitude - degrees);
int minutes = decimal * 60;
double seconds = decimal * 3600 - minutes * 60;
NSString *lat = [NSString stringWithFormat:#"%d° %d' %1.4f\"",degrees, minutes, seconds];
latLabel.text = lat;
degrees = newLocation.coordinate.longitude;
decimal = fabs(newLocation.coordinate.longitude - degrees);
minutes = decimal * 60;
seconds = decimal * 3600 - minutes * 60;
NSString *longt = [NSString stringWithFormat:#"%d° %d' %1.4f\"",
degrees, minutes, seconds];
longLabel.text = longt;
MKCoordinateRegion region = { {0.0, 0.0 }, { 0.0, 0.0 } };
region.center.latitude = newLocation.coordinate.latitude;
region.center.longitude = newLocation.coordinate.longitude;
region.span.longitudeDelta = 0.01f;
region.span.latitudeDelta = 0.01f;
[mapView setRegion:region animated:YES];
[mapView setDelegate:self];
}