CLLocationManager 没有收到更新
Posted
技术标签:
【中文标题】CLLocationManager 没有收到更新【英文标题】:CLLocationManager don't receive updates 【发布时间】:2013-09-20 20:09:08 【问题描述】:我是 Objective-C 世界的新手。我尝试在模拟器上的 ios 6.1 上开发简单的定位应用程序。但是当我尝试模拟位置时,它根本不起作用。我没有收到任何事件,但delegate
对象已正确注册并且PositionProvider
是startUpdate
。
怎么了?
我有以下代码:
位置提供者
#import <Foundation/Foundation.h>
#import <CoreLocation/CoreLocation.h>
#import "LocationListener.h"
@interface PositionProvider : NSObject
@public
LocationListener<CLLocationManagerDelegate> *listener;
@protected
CLLocationManager *manager;
#pragma mark -
#pragma mark on/off
-(void) startUpdate;
-(void) stopUpdate;
@end
实施
@implementation PositionProvider
@private
CLLocation *lastLocation;
-(void) startUpdate
manager = [[CLLocationManager alloc] init];
LocationListener *listener2 = [[LocationListener alloc]init];
manager.delegate = listener2;
manager.desiredAccuracy = kCLLocationAccuracyBest;
if(manager == nil)
NSLog(@"Manager is NULL");
if(listener == nil || manager.delegate == nil)
NSLog(@"Listener is NULL");
NSLog(@"startUpdate");
if ([CLLocationManager locationServicesEnabled])
NSLog(@"Location Services Enabled");
switch ([CLLocationManager authorizationStatus])
case kCLAuthorizationStatusAuthorized:
NSLog(@"We have access to location services");
break;
case kCLAuthorizationStatusDenied:
NSLog(@"Location services denied by user");
break;
case kCLAuthorizationStatusRestricted:
NSLog(@"Parental controls restrict location services");
break;
case kCLAuthorizationStatusNotDetermined:
NSLog(@"Unable to determine, possibly not available");
break;
default:
break;
else
NSLog(@"Location Services Are Disabled");
[manager startUpdatingHeading];
[manager startUpdatingLocation];
-(void) stopUpdate
@end
位置监听器
@interface LocationListener : NSObject<CLLocationManagerDelegate>
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation;
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations;
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status;
@end
实施
@implementation LocationListener
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
// newLocation.coordinate;
NSLog(@"Logging Location");
- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
NSLog(@"Logging Location");
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus) status
NSLog(@"Status");
@end
【问题讨论】:
为什么他是一个名为 listener2 的变量和另一个 (ivar) 名为 listener 的变量? @tdelepine 仅出于调试目的而更改。但是还是不行。 您是否使用wifi网络有时用户位置错误?在这种情况下,有必要进入模拟器菜单并设置更多次位置 @tdelepin 位置没有错,根本没有更新。我尝试多次更改位置,更改 GPS“路线”。它根本不起作用。 【参考方案1】:你试过了吗
[manager setDelegate:listener2];
或者,您可能遇到内存管理问题。尝试使用 debugger-breakpoint 添加 dealloc 方法,看看它是否在您预期之前被调用:
- (void)dealloc
NSLog(@"Dealloc called");
self.locationManager = nil;
【讨论】:
以上是关于CLLocationManager 没有收到更新的主要内容,如果未能解决你的问题,请参考以下文章
iOS7 CLLocationManager 暂停更新并且不会恢复
带有外部 GPS 的 CLLocationManager/MKMapView