IOS CoreLocation

来源:互联网 发布:淘宝客的原理 编辑:程序博客网 时间:2024/05/16 14:13
环境:
I5、4G
Win7旗舰版 64
Vm 8.0
Mac OS X Lion 10.7.2 VMware Image
Xcode 4.3
IOS SDK 5.0

Ipad2 5.1.1

要利用CoreLocation,必须在frameworks里面加入“CoreLocation.framework”。在最新版本的Xcode(4.x)中加入新的framework步骤如下:
单击项目的target =>在出来的xcodeproj面板中点击“Link Binary With Libraries” =>点击“+”,然后选择需要的framework即可。
首先建立一个Single View Application工程,然后引入CoreLocation.framework,并在ViewController.h中修改如下:

[plain] view plaincopy
  1. #import <UIKit/UIKit.h>  
  2. #import <CoreLocation/CoreLocation.h>  
  3.   
  4. @interface XViewController : UIViewController  
  5. {  
  6.     CLLocationManager *locManager;  
  7.       
  8. }  
  9.   
  10. @property (retain, nonatomic) IBOutlet UILabel *lonLabel;  
  11. @property (retain, nonatomic) IBOutlet UILabel *latLabel;  
  12. @property (retain, nonatomic) CLLocationManager *locManager;  
  13.   
  14. - (IBAction)StartLocation:(id)sender;  
  15. @end  

.m文件的实现如下,具体解释在代码注释中说明

[plain] view plaincopy
  1. - (void)dealloc {  
  2.     [lonLabel release];  
  3.     [latLabel release];  
  4.     [locManager release];  
  5.     [super dealloc];  
  6. }  
  7.   
  8. //协议中的方法,作用是每当位置发生更新时会调用的委托方法    
  9. -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation    
  10. {    
  11.     //结构体,存储位置坐标    
  12.     CLLocationCoordinate2D loc = [newLocation coordinate];    
  13.     float longitude = loc.longitude;    
  14.     float latitude = loc.latitude;    
  15.     self.lonLabel.text = [NSString stringWithFormat:@"%f",longitude];    
  16.     self.latLabel.text = [NSString stringWithFormat:@"%f",latitude];    
  17.       
  18. }    
  19.   
  20. //当位置获取或更新失败会调用的方法    
  21. -(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error    
  22. {    
  23.     NSString *errorMsg = nil;    
  24.     if ([error code] == kCLErrorDenied) {    
  25.         errorMsg = @"访问被拒绝";    
  26.     }    
  27.     if ([error code] == kCLErrorLocationUnknown) {    
  28.         errorMsg = @"获取位置信息失败";    
  29.     }    
  30.       
  31.     UIAlertView *alert = [[UIAlertView alloc]initWithTitle:@"Location"     
  32.                                                    message:errorMsg     
  33.                                                   delegate:self     
  34.                                          cancelButtonTitle:@"Cancel"     
  35.                                          otherButtonTitles:@"OtherBtn",nil];    
  36.     [alert show];    
  37.     [alert release];    
  38. }    
  39.   
  40. - (IBAction)StartLocation:(id)sender {  
  41.     //初始化位置管理器    
  42.     locManager = [[CLLocationManager alloc]init];    
  43.     //设置代理    
  44.     locManager.delegate = self;    
  45.     //设置位置经度    
  46.     locManager.desiredAccuracy = kCLLocationAccuracyBest;    
  47.     //设置每隔100米更新位置    
  48.     locManager.distanceFilter = 100;    
  49.     //开始定位服务    
  50.     [locManager startUpdatingLocation];    
  51.       
  52. }  
转自:http://blog.csdn.net/ikmb/article/details/7822492
原创粉丝点击