ios - CLLocationManager SignificantLocationChanges 强制刷新

标签 ios cllocationmanager

我有一个使用 CLLocation 管理器的应用程序,我需要能够强制传递位置事件。 Apple 文档说明...

After returning a current location fix, the receiver generates update events only when a significant change in the user’s location is detected. For example, it might generate a new event when the device becomes associated with a different cell tower. It does not rely on the value in the distanceFilter property to generate events. Calling this method several times in succession does not automatically result in new events being generated. Calling stopMonitoringSignificantLocationChanges in between, however, does cause a new initial event to be sent the next time you call this method.

所以我读到,因为我可以调用 stopMonitoringSignificantLocationChanges 然后调用 startMonitoringSignificantLocationChanges 并接收位置事件,但是,在实践中这是行不通的。请参阅下面的代码(我删除了部分以免泄露私有(private)反向地理 API)。这是一个将反向地理与 CLLocationManager 结合起来的包装类。在我的演示应用程序中,我调用 beginMonitoringSignificantChanges,然后调用 stopMonitoringSignificantChanges,我只看到 -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 在我更改位置或首次安装应用程序时调用,而不是在我停止和开始。有什么想法吗?

//
//  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

最佳答案

显然,这只有在您每次都重新初始化 CLLocationManager 时才有效。文档似乎不支持这一点,但我的测试确实支持。如果有人有更好的答案,我很乐意听到。

关于ios - CLLocationManager SignificantLocationChanges 强制刷新,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/17175867/

相关文章:

objective-c - 写入文件末尾

ios - 由于我的 View 层次结构,viewWillAppear 显然没有被调用

ios - Swift Xcode6 UIActivityIndi​​catorView 显示缓慢

ios - 当我重新启动我的设备时,我的 IOS 应用程序没有获得任何区域更新有什么想法吗?

ios - 我可以从 "Pending Developer Release"中删除应用程序吗

iphone - 在页面加载时显示 UItableview 滚动条几秒钟

ios - 向 iBeacon 广告包添加数据

ios - 检查坐标矩形是否包含 CLLocationCoordinate2D

ios - 如何判断 CLLocationManager 何时锁定​​了用户位置

ios - startUpdatingLocation 在 XCode 6.1 中不再有效