我正在尝试制作一个使用设备当前位置的iOS7应用。我在Mac上使用iPhone模拟器,但我遇到了一些问题。每当我看到位置管理器所在的视图时,即使在我设置了自定义位置(来自模拟器> debug>位置)之后,它也会打印出纬度和经度0.000000。
此外,模拟器在打开应用程序时没有要求使用当前位置的许可似乎很奇怪。有谁知道这里发生了什么?
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view, typically from a nib.
[super viewDidLoad];
CLLocationManager *locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
locationManager.distanceFilter = kCLDistanceFilterNone; // whenever we move
locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters; // 100 m
[locationManager startUpdatingLocation];
_location = [locationManager location];
_coord.longitude = _location.coordinate.longitude;
_coord.latitude = _location.coordinate.latitude;
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
_coord.longitude = _location.coordinate.longitude;
_coord.latitude = _location.coordinate.latitude;
printf("%f\n",self.coord.longitude);
printf("%f\n",self.coord.latitude);
}
答案 0 :(得分:0)
您需要从委托方法didUpdateLocationToLocation:fromLocation:获取newLocation。还实现了didFailWithError委托方法。在开始获取更新位置之前需要一些时间,因此委托调用。
最后一个位置通常是缓存的,因此检查位置的时间戳并过滤旧位置可能是明智的。
修改强>
这是我能提供的最干净的例子。在Xcode中启动新项目,选择单一视图应用程序模板,iPhone。不要触摸故事板,只需用此替换ViewController.m的内容,然后在模拟器或设备中运行。如果在Simulator上,转到Debug并设置一些位置,您将在控制台中获得坐标。我也在视图开启或关闭时启动和停止位置更新。
#import "ViewController.h"
#import <CoreLocation/CoreLocation.h>
@interface ViewController () <CLLocationManagerDelegate>
@property (strong, nonatomic) CLLocationManager *locationManager;
@end
@implementation ViewController
#pragma mark - Location Manager delegate methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation {
if ([newLocation.timestamp timeIntervalSinceNow] >= -300.0) {
NSLog(@"updated location with latitude %f longitude %f", newLocation.coordinate.longitude, newLocation.coordinate.latitude);
}
}
- (void)viewWillAppear:(BOOL)animated
{
[super viewWillAppear:animated];
[self.locationManager startUpdatingLocation];
}
- (void)viewWillDisappear:(BOOL)animated
{
[super viewWillDisappear:animated];
[self.locationManager stopUpdatingLocation];
}
- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
if(error.code == kCLErrorDenied) {
// alert user
UIAlertView *alertView = [[UIAlertView alloc] initWithTitle:@"Access to location services is disabled"
message:@"You can turn Location Services on in Settings -> Privacy -> Location Services"
delegate:nil
cancelButtonTitle:@"OK"
otherButtonTitles:nil];
[alertView show];
} else if(error.code == kCLErrorLocationUnknown) {
NSLog(@"Error: location unknown");
} else {
NSLog(@"Error retrieving location");
}
}
#pragma mark - Location Manager getter
- (CLLocationManager *)locationManager
{
if (!_locationManager) {
_locationManager = [[CLLocationManager alloc] init];
_locationManager.delegate = self;
_locationManager.desiredAccuracy = kCLLocationAccuracyHundredMeters;
_locationManager.distanceFilter = 60.0;
}
return _locationManager;
}
@end