2
votes

I am getting the current location using CLLocationManager and i am getting the latitude and longitude values of the current location. I haven't used the Mapview in my app. So whenever the current location is changed, that time i want to update the current location to the server. And every 5 minutes i need to call the web service and update the current location to the server in the both background mode and the foreground mode.

-(void) getCurrentLocation
{
    // To get the current location of the place
    locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;
    locationManager.distanceFilter = kCLDistanceFilterNone; //whenever we move
    locationManager.desiredAccuracy = kCLLocationAccuracyBest; //100 m
    //Start the location manager, then only updates the current location
    [locationManager startUpdatingLocation];
}

/In Location manager delegate method, if the location is updated it will call and get the values from the newLocation using CLLocation  
-(void) locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    CLLocation *location = [locationManager location];

    // Configure the new event with information from the location
    CLLocationCoordinate2D coordinate = [location coordinate];

    NSString *lat = [NSString stringWithFormat:@"%f", coordinate.latitude];
    NSString *lon = [NSString stringWithFormat:@"%f", coordinate.longitude];
    NSLog(@"dLatitude : %@", lat);
    NSLog(@"dLongitude : %@",lon);

    self.reverseGeocoder = [[[MKReverseGeocoder alloc] initWithCoordinate:newLocation.coordinate] autorelease];
    reverseGeocoder.delegate = self;
    [reverseGeocoder start];


    //Stop the location manager
    [locationManager stopUpdatingLocation];

}

Whenever the location is changed, that time i want to get the latitude and longitude for the particular location.

Thanks!

3

3 Answers

0
votes

if you need to use the standard location service to provide continuous location updates to the user (even when in the background), you can declare your app as needing background location services by including the UIBackgroundModes key (with the "location" value) in your app’s Info.plist file.

0
votes

Use the following code for backgound and foreground task.

    UIApplication *app = [UIApplication sharedApplication];

bgTask = [app beginBackgroundTaskWithExpirationHandler:^{ [app endBackgroundTask:bgTask];
    bgTask = UIBackgroundTaskInvalid;
}];

[NSTimer scheduledTimerWithTimeInterval:300 target:self selector:@selector(updateLocation) userInfo:nil repeats:YES];

-(void)updateLocation{

    // write the code to update location here. this will execute even if in background or foreground
}
-1
votes

First , if you need your program to run in foreground and background, add the Required background mode as location service in info.plist of your project. Get the location as NSString and try posting the URL to the URL to the server using the synchronous or Asynchronous type.