IOS Core Location 定位功能

Core Location可以利用三种技术来实现该功能:

  1. 蜂窝基站三角网定位根据手机所属范围内的手机基站的位置进行计算来确定当前位置。蜂窝基站三角网定位在城市和其他手机基站密度较高的区域非常准确,而在基站较为稀疏的区域则不太精确。
  2. GPS是三种技术中最为精确的,任何具有3G数据链接的设备还包含一个GPS单元,GPS读取来自多个卫星的微波信号来确定当前位置。
  3. Wi-Fi定位服务(WPS)使用Wi-Fi连接的MAC地址,通过参考已知服务提供商及其服务区域的大型数据库来猜测你的位置。WPS是不精确的,并且有时会有数英里的误差。
根据程序的需要,挑选合适的方法。

  • 位置管理器
Core Location API实际上非常易于使用。我们将使用的主类是CLLocationManager,通常称为位置管理器。
 创建一个位置管理器实例
CLLocationManager *locationManager = [[CLLocationManager alloc] init];

要求的Core Location的精度越高,消耗的电量就会越多。
  • 设置所需的精度
下面设置委托和请求指定精度级别的实例:
locationManager.delegate = self;
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.desiredAccuracy该值的单位为米,如果你指定它为10,
则希望尝试确定当前位置10米范围之内的区域。
  • 设置距离筛选器
默认情况下,位置管理器将通知委托任何检测到的在位置方面的更改。
距离筛选器也是以米为单位进行设置的。
locationManager.distanceFilter = 1000.0f;
  • 启动位置管理器
[locationManager startUpdatingLocation];
  • 更明智地使用位置管理器
如果只需要确定当前位置而不需要连续轮询位置,则当它获取应用程序所需的信息之后,
你应该让位置委托停止位置管理器。
只要你从位置管理器获得更新,就会消耗拥用户的电池。
位置管理器停止向其委托发送更新:
[locationManager stopUpdatingLocation];
  • 位置管理器委托
位置管理器委托必须符合CLLocationManagerDelegate协议,该协议定义了两种方法, 
当位置管理器已经确定位置或者 当它检测到位置的更改时将调用其中一个方法,
当位置管理器遇到错误时将调用另一个方法。
  • 获取位置更新
当位置管理器希望通知其委托当前位置时,它将调用locationManager:didUpdateToLocation:fromLocation:方法。
该方法接受3个参数。
第一个参数是调用该方法的位置管理器。
第二个参数是定义设备的当前位置的一个CLLocation对象。
第三个参数是上次更新定义之前的位置的一个CLLocation对象。第一次调用该方法时,以前的位置对象将为nil;
  • 使用CLLocation获取纬度和经度
纬度和经度存储在一个名为coordinate的属性中。
CLLocationDegrees latitude = theLocation.coordinate.latitude;
CLLocationDegrees longitude = theLocation.coordinate.longitude;

horizontalAccuracy属性描述以coordinate作为其圆的半径,值越大,Core Location所确定的位置就越不确定。

altitude属性是海平面以上或以下多少米:
CLLocationDegreesaltitude = theLocation.altitude;

除了这些属性之外,CLLocation还有一个非常有用的实例方法,该方法将允许你确定两个CLLocation对象之间的距离。
CLLocationDegrees distance = [fromLocation distanceFromLocation : toLocation];
返回的distance值将是大圆计算的结果,该计算忽略了海波属性,并且假设这两个点处于同一海平面来计算该距离。
  • 错误通知
如果Core Location无法确定你的当前位置,它会调用另一个名为locationManager:didFailwithError:的委托方法。
最有可能的错误原因是用户拒绝访问。 位置管理器的使用必须由用户进行授权。


在xcode工程Frameworks文件夹种添加CoreLocation.framework


上代码。。。

先在BIDViewController_iPhone.xib中创建12个label

BIDViewController.h

//
//  BIDViewController.h
//  CoreLocationTest
//
//  Created by ex next on 13-8-8.
//  Copyright (c) 2013年 ex next. All rights reserved.
//

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

@interface BIDViewController : UIViewController<CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
@property (strong, nonatomic) CLLocation *startingPoiont;
@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

BIDViewController.m

//
//  BIDViewController.m
//  CoreLocationTest
//
//  Created by ex next on 13-8-8.
//  Copyright (c) 2013年 ex next. All rights reserved.
//

#import "BIDViewController.h"

@interface BIDViewController ()

@end

@implementation BIDViewController
@synthesize locationManager;
@synthesize startingPoiont;
@synthesize latitudeLabel;
@synthesize longitudeLabel;
@synthesize horizontalAccuracyLabel;
@synthesize altitudeLabel;
@synthesize verticalAccuracyLabel;
@synthesize distanceTraveledLabel;

- (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
{
    //清除输出口
    [self viewDidUnload];
    self.locationManager = nil;
    self.latitudeLabel = nil;
    self.longitudeLabel = nil;
    self.horizontalAccuracyLabel = nil;
    self.altitudeLabel = nil;
    self.verticalAccuracyLabel = nil;
    self.distanceTraveledLabel = nil;
}

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

#pragma mark -
#pragma mark CLLocationManagerDelegate Methods
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
    if (startingPoiont == nil) {
        self.startingPoiont = newLocation;
    }
    
    NSString *latitudeString = [NSString stringWithFormat:@"%g\u00B0",newLocation.coordinate.latitude];
    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:startingPoiont];
    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 Locatin"
                                                    message:errorType
                                                   delegate:nil
                                          cancelButtonTitle:@"Okay" otherButtonTitles: nil];
    [alert show];
}

@end



  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值