IOS开发之Core Location
来源:互联网 发布:酒店网络怎么布置 编辑:程序博客网 时间:2024/04/30 10:59
IOS 支持三种检测当前位置的方式:手机基站、Wi-Fi、和GPS,其中GPS是经度最高的,同时也是最耗费手机电量的。一般情况下在室内是无法通过GPS获 取位置信息的,通过Wi-Fi获取位置的原理是通过网络提供商的IP地址信息来获取位置,经度不是很高,最后是通过手机基站获取位置,手机开机后会连接附 近的基站塔获取信号,通过基站可以得到手机所在的位置信息,基站越密集,所获取的位置信息经度就越高。
IOS SDK提供的Core Location能比较好的提供获取位置信息的功能,获取位置信息涉及如下几个类,CLLocationManager(位置管理器), CLLocation, CLLocationManagerdelegate(协议、提供委托方法),CLLocationCoodinate2D(存储坐标位置)
另外CLLocationManager还有几个属性;
desiredAccuracy:位置的精度属性
取值有如下几种:
<table "="" cellpadding="0" cellspacing="0" style="margin-top: 0px; margin-right: auto; margin-bottom: 0px; margin-left: auto; padding-top: 0px; padding-right: 0px; padding-bottom: 0px; padding-left: 0px; border-collapse: collapse; -webkit-border-horizontal-spacing: 0px; -webkit-border-vertical-spacing: 0px; border-top-width: 1px; border-right-width: 1px; border-bottom-width: 1px; border-left-width: 1px; border-top-style: solid; border-right-style: solid; border-bottom-style: solid; border-left-style: solid; border-top-color: rgb(192, 192, 192); border-right-color: rgb(192, 192, 192); border-bottom-color: rgb(192, 192, 192); border-left-color: rgb(192, 192, 192); border-image: initial; ">kCLLocationAccuracyBest
精确度最佳
kCLLocationAccuracynearestTenMeters
精确度10m以内
kCLLocationAccuracyHundredMeters
精确度100m以内
kCLLocationAccuracyKilometer
精确度1000m以内
kCLLocationAccuracyThreeKilometers
精确度3000m以内
distanceFilter:横向移动多少距离后更新位置信息
delegate:响应CLLocationManagerdelegate的对象
下面来构建一个获取位置的例子:
首先建立一个Single View Application工程,然后引入CoreLocation.framework,并在ViewController.h中修改如下:
#import <CoreLocation/CoreLocation.h>
@interface ViewController : UIViewController<CLLocationManagerDelegate>
{
CLLocationManager *locManager;
}
@property (retain, nonatomic) IBOutletUILabel *lonLabel;
@property (retain, nonatomic) IBOutletUILabel *latLabel;
@property (retain, nonatomic) CLLocationManager *locManager;
@end
@interfaceViewController ()
@end
@implementation ViewController
@synthesize lonLabel;
@synthesize latLabel;
@synthesize locManager;
- (void)viewDidLoad
{
[superviewDidLoad];
//初始化位置管理器
locManager = [[CLLocationManager alloc]init];
//设置代理
locManager.delegate = self;
//设置位置经度
locManager.desiredAccuracy = kCLLocationAccuracyBest;
//设置每隔100米更新位置
locManager.distanceFilter = 100;
//开始定位服务
[locManagerstartUpdatingLocation];
}
- (void)viewDidUnload
{
[selfsetLonLabel:nil];
[selfsetLatLabel:nil];
[superviewDidUnload];
// Release any retained subviews of the main view.
}
- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
}
- (void)dealloc {
//停止定位服务
[locManagerstopUpdatingLocation];
[lonLabelrelease];
[latLabelrelease];
[superdealloc];
}
//协议中的方法,作用是每当位置发生更新时会调用的委托方法
-(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
//结构体,存储位置坐标
CLLocationCoordinate2D loc = [newLocation coordinate];
float longitude = loc.longitude;
float latitude = loc.latitude;
self.lonLabel.text = [NSStringstringWithFormat:@"%f",longitude];
self.latLabel.text = [NSStringstringWithFormat:@"%f",latitude];
}
//当位置获取或更新失败会调用的方法
-(void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error
{
NSString *errorMsg = nil;
if ([error code] == kCLErrorDenied) {
errorMsg = @"访问被拒绝";
}
if ([error code] == kCLErrorLocationUnknown) {
errorMsg = @"获取位置信息失败";
}
UIAlertView *alertView = [[UIAlertViewalloc]initWithTitle:@"Location"
message:errorMsg delegate:self cancelButtonTitle:@"Ok"otherButtonTitles:nil, nil];
[alertView show];
[alertView release];
}
@end
- IOS开发之Core Location
- IOS开发之Core Location
- iOS开发之core location
- iOS开发之-Core Location
- ios之Core Location定位功能
- iOS大典之地图Core Location
- IOS Core Location
- ios-Core Location框架
- Core Location之指南针
- IOS学习之Core Location定位和MapKit视图使用
- IOS Core Location 定位功能
- ios-使用Core Location定位
- iOS Core Location 实现定位
- IOS Core Location-ios定位
- IOS开发(96)之Core Data
- iOS开发之Core Data Demo (一)
- iOS开发之Core Data数据存储
- iOS 动画开发之Core Animation
- Android图像处理——ColorMatrix
- 坎坷道路-程序员必须先让自己的心灵强大起来
- 一个比较简单的COM
- 队列Rollback的设计
- ctrl + z 和 fg命令的妙用
- IOS开发之Core Location
- CF 111A
- JDBC(未完 别看呢)
- 奥运得金
- 将GridView控件数据导出到Excel中。
- Oracle Flashback技术总结
- Linux安装飞信
- PHP手册-自动加载文件
- Oracle创建实例