gpt4 book ai didi

ios - 如何在iPhone的背景模式下获取当前位置?

转载 作者:行者123 更新时间:2023-12-01 17:10:10 26 4
gpt4 key购买 nike

我正在使用CLLocationManager获取当前位置,并且正在获取当前位置的纬度和经度值。我尚未在应用程序中使用过Mapview。因此,无论何时更改当前位置,那一次我都想将当前位置更新到服务器。每隔5分钟,我需要调用Web服务并以后台模式和前景模式的形式将当前位置更新到服务器。

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

}

每当位置更改时,那个时候我都想获取特定位置的纬度和经度。

谢谢!

最佳答案

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

关于ios - 如何在iPhone的背景模式下获取当前位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10553847/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com