我一直在尝试在我的设备上实现以下代码: -
#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
。实际上,设备卡在初始数据中(标签在视图中不会更新)..
我做错了什么..这不是正确的方法吗?
答案 0 :(得分:2)
iPod touch使用WiFi网络进行定位服务。因此,可能无法获得5米移动的良好位置或更新?!它的表现与使用GPS信号定位的iPhone或iPda完全不同。