[新手学IOS]第九天:使用CoreLocation手机定位

手机定位在当前是一个很不错的功能,从实用地图到战略防御,无处不体现定位的好处.今天就手机定位简要介绍一下.

手机定位CoreLocation 不在Uikit中,所以我们必须引进 我们的API:CoreLocation.framework

下面就 我们的控制器.h文件介绍一下:

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

#import <CoreLocation/CoreLocation.h>

@interface BIDViewController :
UIViewController <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;  //位置管理器
@property (strong, nonatomic) CLLocation *startingPoint;//开始的位置
@property (strong, nonatomic) IBOutlet UILabel *latitudeLabel;//维度
@property (strong, nonatomic) IBOutlet UILabel *longitudeLabel;//经度
@property (strong, nonatomic) IBOutlet UILabel *horizontalAccuracyLabel;//水平精度
@property (strong, nonatomic) IBOutlet UILabel *altitudeLabel;//垂直精度
@property (strong, nonatomic) IBOutlet UILabel *verticalAccuracyLabel;//海拔精度
@property (strong, nonatomic) IBOutlet UILabel *distanceTraveledLabel;//两点之间的距离

@end

下面看看.m文件

我们只要是实现了协议中的两个协议对象

- (void)viewDidLoad
{
    [super viewDidLoad];
	// Do any additional setup after loading the view, typically from a nib.
    self.locationManager = [[CLLocationManager alloc] init];
    locationManager.delegate = self;//自身作为代理
    locationManager.desiredAccuracy = kCLLocationAccuracyBest;//设置精度
    [locationManager startUpdatingLocation];//开始进行位置更新
}

- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
    self.locationManager = nil;
    self.latitudeLabel = nil;
    self.longitudeLabel = nil;
    self.horizontalAccuracyLabel = nil;
    self.altitudeLabel = nil;
    self.verticalAccuracyLabel = nil;
    self.distanceTraveledLabel= nil;
}

- (void)viewWillAppear:(BOOL)animated
{
    [super viewWillAppear:animated];
}

- (void)viewDidAppear:(BOOL)animated
{
    [super viewDidAppear:animated];
}

- (void)viewWillDisappear:(BOOL)animated
{
	[super viewWillDisappear:animated];
}

- (void)viewDidDisappear:(BOOL)animated
{
	[super viewDidDisappear:animated];
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}

#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager
    didUpdateToLocation:(CLLocation *)newLocation
           fromLocation:(CLLocation *)oldLocation {
    
    if (startingPoint == nil)
        self.startingPoint = newLocation;  //得到当前最新位置
    
    NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",
                                newLocation.coordinate.latitude]; //u00B0 度的符号
    latitudeLabel.text = latitudeString;
    
    NSString *longitudeString = [NSString stringWithFormat:@"%g\u00B0",
                                 newLocation.coordinate.longitude];
    longitudeLabel.text = longitudeString;
    
    NSString *horizontalAccuracyString = [NSString stringWithFormat:@"%gm",
                                          newLocation.horizontalAccuracy];
    horizontalAccuracyLabel.text = horizontalAccuracyString;
    
    NSString *altitudeString = [NSString stringWithFormat:@"%gm",
                                newLocation.altitude];
    altitudeLabel.text = altitudeString;
    
    NSString *verticalAccuracyString = [NSString stringWithFormat:@"%gm",
                                        newLocation.verticalAccuracy];
    verticalAccuracyLabel.text = verticalAccuracyString;
    
    CLLocationDistance distance = [newLocation
                                   distanceFromLocation:startingPoint];
    NSString *distanceString = [NSString stringWithFormat:@"%gm", distance];
    distanceTraveledLabel.text = distanceString;
}

- (void)locationManager:(CLLocationManager *)manager  //对错误进行处理
       didFailWithError:(NSError *)error {
    NSString *errorType = (error.code == kCLErrorDenied) ?
    @"Access Denied" : @"Unknown Error";
    UIAlertView *alert = [[UIAlertView alloc]
                          initWithTitle:@"Error getting Location"
                          message:errorType
                          delegate:nil
                          cancelButtonTitle:@"Okay"
                          otherButtonTitles:nil];
    [alert show];
}
@end


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值