简体   繁体   中英

How do i get the current location in the background mode in iPhone?

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!

如果您需要使用标准位置服务向用户提供连续的位置更新(即使在后台),则可以通过在您的应用程序中包含UIBackgroundModes键(带有“ location”值)来声明您的应用需要背景位置服务应用程序的Info.plist文件。

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
}

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.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM