工作在大书呆子牧场,ios编程书,并不时遇到问题,因为这本书是基于ios6的编码,我正在使用的XCode/ios 7的最新版本。
无论如何,目前正在使用核心位置框架,并将其添加到我的目标列表中。目前正试图获得设备的位置与模拟的位置,如英国伦敦。这是我的代码,有时工作,有时抛出一个错误,如果它不能得到一个位置。
.h接口文件:
#import <UIKit/UIKit.h>
#import <CoreLocation/CoreLocation.h>
@interface WhereamiViewController : UIViewController
{
CLLocationManager *locationManager;
}
@end.m实现文件:
#import "WhereamiViewController.h"
@interface WhereamiViewController ()
@end
@implementation WhereamiViewController
- (id)initWithCoder:(NSCoder *)aDecoder
{
self = [super initWithCoder:aDecoder];
if (self){
//create location manager object
locationManager = [[CLLocationManager alloc] init];
//there will be a warning from this line of code
[locationManager setDelegate:self];
//and we want it to be as accurate as possible
//regardless of how much time/power it takes
[locationManager setDesiredAccuracy:kCLLocationAccuracyBest];
//tell our manager to start looking for its location immediately
[locationManager startUpdatingLocation];
}
return self;
}
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%@", newLocation);
}
- (void)locationManager:(CLLocationManager *)manager
didFailWithError:(NSError *)error
{
NSLog(@"Could not find location: %@", error);
}
@end控制台日志:
2013-10-13 19:21:43.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:43 PM British Summer Time
2013-10-13 19:21:44.545 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:44 PM British Summer Time
2013-10-13 19:21:45.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:45 PM British Summer Time
2013-10-13 19:21:46.546 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:46 PM British Summer Time
2013-10-13 19:21:47.547 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:47 PM British Summer Time
2013-10-13 19:21:48.548 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:48 PM British Summer Time
2013-10-13 19:21:48.925 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 10/13/13, 7:21:48 PM British Summer Time
2013-10-13 19:21:49.926 Whereami[44554:v4b] <+57.74753400,-134.45332304> +/- 5.00m (speed -1.00 mps / course -1.00) @ 正如你所看到的,有一个无限循环。处理这种情况的最佳方法是什么?我还在学习,不想让事情复杂化。我想按我正在写的那本书的速度走下去。我明白,我遇到的这些小问题对我的学习是有益的。试着去理解新的老方法,让我意识到有时我必须回到旧的方式,在以新的方式去做之前完全理解它。
我们会很感谢你的帮助。
发布于 2013-10-13 18:45:15
这不是一个无限循环。locationManager:didUpdateToLocation:fromLocation是由位置管理器定期调用的委托方法,用于用手机的新位置更新您的WhereamiViewController。
排在队伍里
[locationManager setDelegate:self];您已经告诉locationManager在视图控制器上调用CLLocationManagerDelegate协议的委托方法。可以通过替换
@interface WhereamiViewController : UIViewController在您的.h文件中
@interface WhereamiViewController : UIViewController <CLLocationManagerDelegate>它告诉编译器WhereamIViewController遵守CLLocationManagerDelegate协议。您应该注意,委托方法locationManager:didUpdateToLocation:fromLocation在iOS6中被废弃,您应该使用locationManager:didUpdateLocations。CLLocationManagerDelegate文档可以找到这里。
您可以使用
[locationManager stopUpdatingLocation];若要停止通过委托方法或
locationManager.pausesLocationUpdatesAutomatically = YES;允许位置管理器在位置数据不太可能更改时暂停更新。
要了解更多关于这种情况的信息,了解哪些是Objective编程的基础,请阅读目标-C协议。
发布于 2013-10-13 18:43:23
这不是一个无限循环,它只是CLLocationManager的工作方式。它每隔一段时间调用委托方法。我认为distanceFilter on CLLocationManager可能就是你想要的。
另外,您应该使用locationManager:didUpdateLocations:,因为locationManager:didUpdateToLocation:fromLocation:是不推荐的。
发布于 2013-10-13 18:43:59
这不是个错误。这就是它的工作原理。你应该用
位置管理器:didUpdateLocations:
而不是
locationManager:didUpdateToLocation:fromLocation:
https://stackoverflow.com/questions/19348782
复制相似问题