2016-01-30 38 views
-1

我一直在试图制作一个应用程序,允许用户使用核心位置框架检索当前位置。我希望能够在应用程序启动时获取使用该应用程序的设备的坐标,并能够使用UILabels显示它们。有人可以告诉我我做错了什么,或者我应该对代码做些什么调整?核心位置不会工作

头文件

#import <UIKit/UIKit.h> 
#import <CoreLocation/CoreLocation.h> 


@interface ViewController : UIViewController 

@property (strong, nonatomic) IBOutlet UILabel *longitude; 
@property (strong, nonatomic) IBOutlet UILabel *latitude; 

@property (strong, nonatomic) IBOutlet UILabel *altitude; 
@property (strong, nonatomic) IBOutlet UILabel *speed; 

@end 

实现文件:

#import "ViewController.h" 

@interface ViewController() 

@end 

@implementation ViewController 


- (void)viewDidLoad 
{ 

CLLocationManager *locationManager; 

[super viewDidLoad]; 
locationManager = [[CLLocationManager alloc]init]; // initializing locationManager 
locationManager.delegate = self; // I set the delegate of locationManager to self. 
locationManager.desiredAccuracy = kCLLocationAccuracyBest; // setting the accuracy 

[locationManager startUpdatingLocation]; //requesting location updates 
} 

- (void)didReceiveMemoryWarning { 
[super didReceiveMemoryWarning]; 
// Dispose of any resources that can be recreated. 
} 



-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error{ 
UIAlertView *errorAlert = [[UIAlertView alloc]initWithTitle:@"Error" message:@"There was an error retrieving your location" delegate:nil cancelButtonTitle:@"OK" otherButtonTitles: nil]; 
[errorAlert show]; 
NSLog(@"Error: %@",error.description); 
} 
-(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations 
{ 
    CLLocation *crnLoc = [locations lastObject]; 
_latitude.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.latitude]; 
_longitude.text = [NSString stringWithFormat:@"%.8f",crnLoc.coordinate.longitude]; 
_altitude.text = [NSString stringWithFormat:@"%.0f m",crnLoc.altitude]; 
_speed.text = [NSString stringWithFormat:@"%.1f m/s", crnLoc.speed]; 
} 



@end 

我得到这些2个错误。

Assigning to 'id<CLLocationManagerDelegate> _Nullable' from incompatible type 'ViewController *const __strong' 


'UIAlertView' is deprecated: first deprecated in iOS 9.0 - UIAlertView is deprecated. Use UIAlertController with a preferredStyle of UIAlertControllerStyleAlert instead 

回答

2

你不执行CLLocationManagerDelegate协议,这就是为什么你得到的第一个错误。您可以实现这样的:

@interface ViewController() <CLLocationManagerDelegate> 

@end 

您也应该检查documentation的,你需要,以实现符合该协议,你必须在你的@implementation写的方法。只有在符合该协议后才能执行此分配:

locationManager.delegate = self; // I set the delegate of locationManager to self. 

第二个问题似乎是警告。 Apple在iOS 9.0中确实弃用UIAlertView,它已被替换为UIAlertController,因此您应该使用它。 This NSHipster文章解释了如何使用它,并将其与旧的,弃用的版本进行比较,这很有趣,但它在Swift中(我将它留给它们进行讨论)。你也可以在Objective-C中找到一些例子。下面是最后一个链接上显示的示例:

UIAlertController * view= [UIAlertController 
           alertControllerWithTitle:@"My Title" 
               message:@"Select you Choice" 
             preferredStyle:UIAlertControllerStyleActionSheet]; 

UIAlertAction* ok = [UIAlertAction 
         actionWithTitle:@"OK" 
            style:UIAlertActionStyleDefault 
           handler:^(UIAlertAction * action) { 
            //Do some thing here 
            [view dismissViewControllerAnimated:YES  
                  completion:nil]; 
}]; 

UIAlertAction* cancel = [UIAlertAction 
          actionWithTitle:@"Cancel" 
             style:UIAlertActionStyleDefault 
            handler:^(UIAlertAction * action) { 
             [view dismissViewControllerAnimated:YES completion:nil]; 
}]; 


[view addAction:ok]; 
[view addAction:cancel]; 
[self presentViewController:view animated:YES completion:nil];