定位
2015-12-16 09:14
204 查看
IOS 支持三种检测当前位置的方式:手机基站、Wi-Fi、和GPS,其中GPS是经度最高的,同时也是最耗费手机电量的。一般情况下在室内是无法通过GPS获 取位置信息的,通过Wi-Fi获取位置的原理是通过网络提供商的IP地址信息来获取位置,经度不是很高,最后是通过手机基站获取位置,手机开机后会连接附 近的基站塔获取信号,通过基站可以得到手机所在的位置信息,基站越密集,所获取的位置信息经度就越高。
IOS
SDK提供的Core
Location能比较好的提供获取位置信息的功能,获取位置信息涉及如下几个类,CLLocationManager(位置管理
器), CLLocation, CLLocationManagerdelegate(协议、提供委托方
法),CLLocationCoodinate2D(存储坐标位置)
另外CLLocationManager还有几个属性;
desiredAccuracy:位置的精度属性
取值有如下几种:
<table "="" cellpadding="0" cellspacing="0">
kCLLocationAccuracyBest
精确度最佳
kCLLocationAccuracynearestTenMeters
精确度10m以内
kCLLocationAccuracyHundredMeters
精确度100m以内
kCLLocationAccuracyKilometer
精确度1000m以内
kCLLocationAccuracyThreeKilometers
精确度3000m以内
distanceFilter:横向移动多少距离后更新位置信息
delegate:响应CLLocationManagerdelegate的对象
下面来构建一个获取位置的例子:
首先建立一个Single View Application工程,然后引入CoreLocation.framework,并在ViewController.h中修改如下:
#import <UIKit/UIKit.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
.m文件的实现如下,具体解释在代码注释中说明
#import "ViewController.h"
@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
SDK提供的Core
Location能比较好的提供获取位置信息的功能,获取位置信息涉及如下几个类,CLLocationManager(位置管理
器), CLLocation, CLLocationManagerdelegate(协议、提供委托方
法),CLLocationCoodinate2D(存储坐标位置)
另外CLLocationManager还有几个属性;
desiredAccuracy:位置的精度属性
取值有如下几种:
<table "="" cellpadding="0" cellspacing="0">
kCLLocationAccuracyBest
精确度最佳
kCLLocationAccuracynearestTenMeters
精确度10m以内
kCLLocationAccuracyHundredMeters
精确度100m以内
kCLLocationAccuracyKilometer
精确度1000m以内
kCLLocationAccuracyThreeKilometers
精确度3000m以内
distanceFilter:横向移动多少距离后更新位置信息
delegate:响应CLLocationManagerdelegate的对象
下面来构建一个获取位置的例子:
首先建立一个Single View Application工程,然后引入CoreLocation.framework,并在ViewController.h中修改如下:
#import <UIKit/UIKit.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
.m文件的实现如下,具体解释在代码注释中说明
#import "ViewController.h"
@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
最后编译并运行结果如下(在模拟器上得到的经纬度):
相关文章推荐
- 创业常见病自诊
- shm_open 与 open
- 领域驱动设计案例之业务实现2
- 袁氏家族情系桑梓其心可鉴――浙江大学图书馆藏宋刻本《资治通鉴纲目》影印出版
- Guava之前置条件判断
- 导游 解题报告(Floyd-Warshall 算法)
- ViewController的关键流程
- Jquery easyui从零单排之datagrid多行数据删除
- js获取页面中新弹出的窗口
- Chrome浏览器允许跨域请求配置
- 深入浅析JavaScript字符串操作方法 slice、substr、substring及其IE兼容性
- HTML5 LocalStorage 本地存储
- HTML5 LocalStorage 本地存储
- Android studio svn 使用报错,解决办法
- hiho刷题日记——第三十天小Hi小Ho的惊天大作战:扫雷·一
- 领域驱动设计案例之业务实现1
- daemon虚拟光驱
- Android自定义控件
- iframe的内容增高或缩减时设置其iframe的高度的处理方案
- 软件工程设计之四则运算