我一直在尝试在我的设备上实现以下代码:-
#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
应该只在设备移动超过 5 米时调用
。实际上,设备卡在初始数据上(标签不会在 View 中更新)。
我做错了什么..这不是正确的方法吗??
最佳答案
iPod touch 使用 WiFi 网络提供定位服务。所以可能无法获得一个好的位置或更新 5 米移动?!它的行为与同样使用 GPS 信号定位的 iPhone 或 iPda 完全不同。
关于ios - 在我的 iPod touch 上使用 Core Location,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21814645/