2013-06-18 44 views
1

我有一个使用CLLocation管理器的应用程序,我需要能够强制传送位置事件的功能。 Apple文档状态...CLLocationManager显着位置更改强制刷新

返回当前位置修复程序后,只有在检测到用户位置发生重大变化时,接收器才会生成更新事件。例如,当设备与不同的蜂窝塔关联时,它可能会生成新的事件。它不依赖于distanceFilter属性中的值来生成事件。连续多次调用此方法不会自动导致生成新事件。然而,在中间调用stopMonitoringSignificantLocationChanges会导致在下次调用此方法时发送新的初始事件。

所以我读了,我可以叫stopMonitoringSignificantLocationChanges然后startMonitoringSignificantLocationChanges和接收位置的事件,但是,在实践中这是行不通的。请参阅下面的代码(我已经删除了部分内容,以便不公开专用反向地理API)。这是一个将反转地理与CLLocationManager结合的包装类。在我的演示应用程序中,我调用了beginMonitoringSignificantChanges,然后停止监视重要更改,并且我只看到 - (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)当我更改位置或第一次安装应用程序时调用的位置,而不是当我停止并开始。有什么想法吗?

// 
// TTGeoWrapper.m 
// LocationManagementDemoApp 
// 
// Created by Kyle Jurick on 12/17/12. 
// Copyright (c) 2012 Kyle Jurick. All rights reserved. 
// 

#import "TTGeoWrapper.h" 
#import "OpenXML.h" 

@implementation TTGeoWrapper 
-(id)initWith//removed code 
{ 
    self = [super init]; 
    if (self) { 
     //removed code 
     _transactionTimeout=30; 
     //removed code 
     locationManager = [[CLLocationManager alloc] init]; 
     locationManager.delegate = self; 
     authorizationStatus = [CLLocationManager authorizationStatus]; 
     if (authorizationStatus != kCLAuthorizationStatusAuthorized) { 
      ableToMonitorLocation = false; 
     } 
     else 
     { 
      ableToMonitorLocation = true; 
     } 

    } 

    return self; 
} 

-(void)dealloc 
{ 
    locationManager.delegate = nil; 
} 

-(void)beginMonitoringSignificantChanges 
{ 
    //if (ableToMonitorLocation) { 
     [locationManager startMonitoringSignificantLocationChanges]; 
    /*} 
    NSMutableDictionary *requestFailureUserInfo = [[NSMutableDictionary alloc] init]; 
    [requestFailureUserInfo setValue:@"NOT AUTHORIZED" forKey:@"FAILURE REASON"]; 
    [requestFailureUserInfo setValue:[NSString stringWithFormat:@"%d", authorizationStatus] forKey:@"FAILURE REASON"]; 
    NSError *requestFailure = [[NSError alloc] initWithDomain:@"TTGeoWrapper" code:0 userInfo:requestFailureUserInfo]; 
    [_delegate requestDidFail:requestFailure TTGeoWrapper:self];*/ 
} 

-(void)stopMonitoringSignificantChanges 
{ 
    [locationManager stopMonitoringSignificantLocationChanges]; 
} 

-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
//per Apple documentation, the last item in the array is the most current location 
     //http://developer.apple.com/library/ios/#documentation/CoreLocation/Reference/CLLocationManagerDelegate_Protocol/CLLocationManagerDelegate/CLLocationManagerDelegate.html 
    int latestPollIndex = locations.count-1; 
    CLLocation *location = [locations objectAtIndex:latestPollIndex]; 
    _timeStamp = location.timestamp; 
    CLLocationCoordinate2D coordinate = [location coordinate]; 
    [self updateLocationWithLatAsync:coordinate.latitude Long:coordinate.longitude]; 
} 

-(void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus: (CLAuthorizationStatus)status 
{ 
    if (status != kCLAuthorizationStatusAuthorized) { 
     ableToMonitorLocation = false; 
    } 
    authorizationStatus = status; 
} 

-(NSString *)updateLocationWithLatAsync:(float)latitude Long:(float)longitude; 
{ 
    webRequest = [ASIHTTPRequest requestWithURL:[[NSURL alloc] initWithString:_URL]]; 
    [webRequest setDelegate:self]; 
    [webRequest setTimeOutSeconds:_transactionTimeout]; 
    [self buildPacketLat:latitude Long:longitude]; 
    PostGUID = [self generateUuidString]; 
    [self getPostResponseAsync]; 
    return PostGUID; 
} 

-(NSString *)updateLocationWithLatSync:(float)latitude Long:(float)longitude 
{ 
    webRequest = [ASIHTTPRequest requestWithURL:[[NSURL alloc] initWithString:_URL]]; 
    [webRequest setDelegate:self]; 
    [webRequest setTimeOutSeconds:_transactionTimeout]; 
    [self buildPacketLat:latitude Long:longitude]; 
    PostGUID = [self generateUuidString]; 
    [self getPostResponse]; 
    return PostGUID; 
} 

-(void)buildPacketLat:(float)latitude Long:(float)longitude 
{ 
//removed code 
    [webRequest appendPostData:[requestXML dataUsingEncoding:NSASCIIStringEncoding]]; 
} 

- (NSString *)generateUuidString 
{ 
    CFUUIDRef uuid = CFUUIDCreate(kCFAllocatorDefault); 
    CFStringRef str = CFUUIDCreateString(kCFAllocatorDefault, uuid); 
    NSString *uuidString = (__bridge NSString *)str; 
    CFRelease(uuid); 
    CFRelease(str); 
    return uuidString; 
} 

-(void)getPostResponse 
{ 
    NSLog(@"Beginning Synchronous POST"); 
    [webRequest startSynchronous]; 
} 

-(void)getPostResponseAsync 
{ 
    NSLog(@"Beginning Asynchronous POST"); 
    [webRequest startAsynchronous]; 
} 

-(void)cancelAsyncRequest 
{ 
    NSLog(@"Cancelling Asynchronous POST"); 
    [webRequest cancel]; 
} 

-(void)requestFinished:(ASIHTTPRequest *)request 
{ 
    OpenXML *geoResponse = [[OpenXML alloc] initWithData:[request responseData]]; 
    _LocationToXMLString = [geoResponse openXMLToString]; 
    _LocationToOpenXML = geoResponse; 
//removed code 
else 
{ 
    NSMutableDictionary *requestFailureUserInfo = [[NSMutableDictionary alloc] init]; 
    //removed code 
    NSError *requestFailure = [[NSError alloc] initWithDomain:@"TTGeoWrapper" code:0 userInfo:requestFailureUserInfo]; 
    [_delegate requestDidFail:requestFailure TTGeoWrapper:self]; 
} 
} 

-(void)requestFailed:(ASIHTTPRequest *)request 
{ 
    NSError *requestFailure = [request error]; 
    [_delegate requestDidFail:requestFailure TTGeoWrapper:self]; 
} 
@end 

回答

1

显然,这只有在您每次都重新启动CLLocationManager时才有效。文档似乎不支持,但我的测试肯定会。如果有人有更好的答案,我很乐意听到它。

+0

在相同的问题上挣扎2天以上。停止SLC后,该应用程序无法正常工作。所以我需要修改我的包装,并在用户每次设置ON/OFF选项时进行初始化。 – BlackM

0

你说得对。只有重新初始化CLLocationManager才会导致发生SignificantLocationChanges更新。如果您需要全新的位置更新,那么为什么不使用标准的位置服务?使用该标准,您还可以获得更准确的修复。

+0

我正在努力使这个多用途。对于某些流程,我依法要求进行第二次GPS轮询。对于其他人,我只是在跟踪位置,重大变化已经足够。为了代码可读性,我更喜欢一个位置管理器的想法。 –

+0

对于我的基于GPS的应用程序,当应用程序位于前台时运行标准,当应用程序进入后台时运行该应用程序。 – sangony