CLLocationManager SignificantLocationChanges强制刷新
我有一个使用CLLocation manager的应用程序,我需要能够强制传送位置事件。 Apple的文档状态...
返回当前位置修正后,只有在检测到用户位置发生重大变化时,接收器才会生成更新事件。 例如,当设备与不同的蜂窝塔关联时,它可能会生成新的事件。 它不依赖于distanceFilter属性中的值来生成事件。 连续多次调用此方法不会自动导致生成新事件。 然而,在中间调用stopMonitoringSignificantLocationChanges会导致在下次调用此方法时发送新的初始事件。
所以我读到,因为我可以调用stopMonitoringSignificantLocationChanges,然后启动监视SignificantLocationChanges并接收位置事件,但是,实际上这是行不通的。 请参阅下面的代码(我已经删除了部分内容,以便不公开专用反向地理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
显然,这只有在你每次都重新初始化CLLocationManager时才有效。 文档似乎不支持,但我的测试肯定会。 如果有人有更好的答案,我很乐意听到它。
你是对的。 只有重新初始化CLLocationManager才会导致发生SignificantLocationChanges更新。 如果您需要全新的位置更新,那么为什么不使用标准的位置服务? 使用该标准,您还可以获得更准确的修复。
链接地址: http://www.djcxy.com/p/20349.html上一篇: CLLocationManager SignificantLocationChanges Force Refresh