gpt4 book ai didi

iphone - 当 iphone 在后台时使用 CLLocationManager 将位置发送到服务器

转载 作者:塔克拉玛干 更新时间:2023-11-02 09:33:37 27 4
gpt4 key购买 nike

当应用程序位于后台时,我无法发送我的位置。我正在使用 CLLocationManager 和 startMonitoringSignificantLocationChanges。 posision didUpdateToLocation 委托(delegate)方法执行一次,但不会多次执行。我尝试四处走走,但没有新位置发送到服务器。

我在 info.plist 文件中设置了“Required background modes” -> “App registers for location updates”。

有人知道哪里出了问题吗?

开始跟踪的代码:

CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = appDelegate;
[appDelegate setLocationManager:locationManager withDistanceFilter:kCLDistanceFilterNone];
[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];

代码(来自 CLLocationManagerDelegate):

- (void)setLocationManager:(CLLocationManager*)locationManager withDistanceFilter:(CLLocationDistance)distanceFilter {

// create a new manager and start checking for sig changes
self.theLocationManager.delegate = nil;
[theLocationManager release];

self.theLocationManager = locationManager;
self.theLocationManager.delegate = self;
self.theLocationManager.distanceFilter = distanceFilter;
}


- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {

NSDate *newLocationTimestamp = newLocation.timestamp;
NSDate *oldLocationTimestamp = oldLocation.timestamp;

int locationUpdateInterval = 15;//15 sec

if (!([newLocationTimestamp timeIntervalSinceDate:oldLocationTimestamp] < locationUpdateInterval)) {
//NSLog(@"New Location: %@", newLocation);
[self updateToLocation:newLocation];
}

}

- (void)updateToLocation:(CLLocation *)newLocation {
NSLog(@"update location!!");

NSString *latitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].latitude];
NSString *longitude = [NSString stringWithFormat:@"%f", [newLocation coordinate].longitude];

[currentUser updatePositionWithLongitude:longitude andLatitude:latitude];
}

最佳答案

正如 Bill Brasky 所说,您为位置管理器设置的准确度可能不会记录您步行的距离。尝试将您的位置管理器精度设置得更高,只是为了看看是否有效,然后将其调低至精度和电池效率之间的一个令人满意的中间值。只是为了测试,一路往上走:

[appDelegate.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];

然后代替:

[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];

尝试:

[appDelegate.theLocationManager startUpdatingLocation];

关于iphone - 当 iphone 在后台时使用 CLLocationManager 将位置发送到服务器,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8213611/

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