【发布时间】:2014-02-16 17:19:20
【问题描述】:
我一直在尝试在我的设备上实现以下代码:-
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface Whereami1ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *locationManager;
__weak IBOutlet UILabel *coordLabel;
}
@end
@implementation Whereami1ViewController
- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
if (self) {
// Custom initialization
locationManager = [[CLLocationManager alloc] init];
[locationManager setDelegate:self];
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
locationManager.distanceFilter= 5.00;//5 meters
[locationManager startUpdatingLocation];
}
return self;
}
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{//location manager sends this message to self
NSLog(@"%@",NSStringFromSelector(_cmd));
NSLog(@"new location–– %@",[locations lastObject]);
NSLog(@"latitude= %f, longitude= %f",[[locations lastObject] coordinate].latitude, [[locations lastObject] coordinate].longitude);
CGSize coordData= {[[locations lastObject] coordinate].latitude, [[locations lastObject] coordinate].longitude};
coordLabel.text= NSStringFromCGSize(coordData);//setting the label's text
}
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{
NSLog(@"%@",NSStringFromSelector(_cmd));
NSLog(@"location not found: %@", error);
}
@end
在代码中,我将距离过滤器设置为 5 米。这意味着在设备移动 5 米后,它应该将标签的文本更新到新坐标(latitude, longitude)。但这在现实中并没有发生。我的意思是
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
should get called only when the device has moved more than 5 meters.实际上,设备卡在初始数据上(视图中标签没有更新)..
我做错了什么..这不是正确的方法吗??
【问题讨论】:
-
是否调用了委托方法?
标签: ios objective-c cocoa-touch core-location