当iphone处于后台时,使用CLLocationManager将位置发送到服务器

时间:2011-11-21 14:44:20

标签: iphone ios cllocationmanager

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

我设置了“必需的背景模式” - > “app注册位置更新”在info.plist文件中。

任何人都知道可能出现的问题?

跟踪开始的代码:

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

2 个答案:

答案 0 :(得分:3)

就像Bill Brasky所说,你设置位置经理的准确性很可能没有记录你走过的距离。尝试将位置管理器的准确度设置得更高,只是为了查看是否有效,然后将其拨回到准确性和电池效率之间的愉快介质。只是为了测试,一直把它带走:

[appDelegate.theLocationManager setDesiredAccuracy:kCLLocationAccuracyBestForNavigation];

然后代替:

[appDelegate.theLocationManager startMonitoringSignificantLocationChanges];

尝试:

[appDelegate.theLocationManager startUpdatingLocation];

答案 1 :(得分:2)

-startMonitoringForSignificantLocationChanges与手机信号塔连接直接相关。您可能需要行驶里程以连接到新塔并触发位置更改事件。我知道区域监控更准确,因为它使用来自Wifi,手机信号塔以及查询位置的其他应用程序的位置更新。您需要确定应用程序的准确性和频率。您可能需要主动监控后台位置(这肯定是电池杀手)。希望这会有所帮助。