获取当前位置(Core Location)
2015-07-10 09:25
369 查看
Core Location框架提供了三种用于追踪设备当前位置的服务,Core Location框架从内置的蜂窝,Wi-Fi或者GPS来获取位置
The significant-change location
service 提供了低耗电的方法来获取当前位置,当前位置改变时会发出通知
The standard location service 提供了一种可设置的方法来获取当前位置
Region monitoring 监视特定地区的跨越
如果程序必须使用位置服务
在程序的info.plist中添加UIRequiredDeviceCapabilities键,它是一个包含多个字符串的数组,然后添加location-services,gps字符串
1.The Standard Location Service
[plain]
Listing 1-1 Starting the standard location service
- (void)startStandardUpdates
{
// 创建location manager
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[plain]
<span style="font-size:16px;"> // 设置获取位置的精确度,越精确越耗电</span>
[plain]
<span style="font-size:16px;"> locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// 设置距离过滤器,超过次距离就更新一次位置
locationManager.distanceFilter = 500;
[locationManager startUpdatingLocation];
}</span>
使用location manager之前一般要检查位置服务是否可用,
[plain]
<span style="font-size:16px;">+ (BOOL)locationServicesEnabled</span>
当位置信息更新时,会给location manager发送消息
<span style="font-size:16px;">- (void)startSignificantChangeUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}</span>
可以叫醒在后台的程序
3.Region monitoring Service
使用之前调用CLLocationManager的regionMonitoringAvailable and regionMonitoringEnabled
[plain]
<span style="font-size:16px;">- (BOOL)registerRegionWithCircularOverlay:(MyCircle*)overlay andIdentifier:(NSString*)identifier
{
// Do not create regions if support is unavailable or disabled.
if ( ![CLLocationManager regionMonitoringAvailable] ||
![CLLocationManager regionMonitoringEnabled] )
return NO;
// If the radius is too large, registration fails automatically,
// so clamp the radius to the max value.
CLLocationDegrees radius = overlay.radius;
if (radius > self.locationManager.maximumRegionMonitoringDistance)
radius = self.locationManager.maximumRegionMonitoringDistance;
// Create the region and start monitoring it.
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:overlay.coordinate
radius:radius identifier:identifier];
[self.locationManager startMonitoringForRegion:region
desiredAccuracy:kCLLocationAccuracyHundredMeters];
[region release];
return YES;
}</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
The significant-change location
service 提供了低耗电的方法来获取当前位置,当前位置改变时会发出通知
The standard location service 提供了一种可设置的方法来获取当前位置
Region monitoring 监视特定地区的跨越
如果程序必须使用位置服务
在程序的info.plist中添加UIRequiredDeviceCapabilities键,它是一个包含多个字符串的数组,然后添加location-services,gps字符串
1.The Standard Location Service
[plain]
Listing 1-1 Starting the standard location service
- (void)startStandardUpdates
{
// 创建location manager
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[plain]
<span style="font-size:16px;"> // 设置获取位置的精确度,越精确越耗电</span>
[plain]
<span style="font-size:16px;"> locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
// 设置距离过滤器,超过次距离就更新一次位置
locationManager.distanceFilter = 500;
[locationManager startUpdatingLocation];
}</span>
使用location manager之前一般要检查位置服务是否可用,
[plain]
<span style="font-size:16px;">+ (BOOL)locationServicesEnabled</span>
当位置信息更新时,会给location manager发送消息
2.Significant-Change Location Service
[plain]<span style="font-size:16px;">- (void)startSignificantChangeUpdates
{
// Create the location manager if this object does not
// already have one.
if (nil == locationManager)
locationManager = [[CLLocationManager alloc] init];
locationManager.delegate = self;
[locationManager startMonitoringSignificantLocationChanges];
}</span>
可以叫醒在后台的程序
3.Region monitoring Service
使用之前调用CLLocationManager的regionMonitoringAvailable and regionMonitoringEnabled
[plain]
<span style="font-size:16px;">- (BOOL)registerRegionWithCircularOverlay:(MyCircle*)overlay andIdentifier:(NSString*)identifier
{
// Do not create regions if support is unavailable or disabled.
if ( ![CLLocationManager regionMonitoringAvailable] ||
![CLLocationManager regionMonitoringEnabled] )
return NO;
// If the radius is too large, registration fails automatically,
// so clamp the radius to the max value.
CLLocationDegrees radius = overlay.radius;
if (radius > self.locationManager.maximumRegionMonitoringDistance)
radius = self.locationManager.maximumRegionMonitoringDistance;
// Create the region and start monitoring it.
CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:overlay.coordinate
radius:radius identifier:identifier];
[self.locationManager startMonitoringForRegion:region
desiredAccuracy:kCLLocationAccuracyHundredMeters];
[region release];
return YES;
}</span>
版权声明:本文为博主原创文章,未经博主允许不得转载。
相关文章推荐
- 锐浪报表设置指定量空白行的方法
- [视频]Win10 Build 10166串流Xbox One游戏演示:赞的没话说
- leveldb初识
- Python中的一些陷阱与技巧小结
- 深入分析由前序和中序重构二叉树问题
- 新入手的Atmel-ICE产品说明
- CodeForces 3B Lorry 贪心
- 几种方法的尾递归实现
- VS2010中的调试技巧
- sheepdog创建vdi流程
- 开源项目(各种有用的第三方库)
- codeforces 557c Arthur and Table
- Android 部分内容设置颜色、字体、超链接、图片
- 开源项目(各种有用的第三方库)
- [转载] Linux下查看内存使用情况方法总结
- VS2010如何调试IIS上的网站
- 如何让旧浏览器支持HTML5新标签
- MFC中发一些CDockablePane 关闭等编程问题。强烈推荐!!!!
- iOS 8 模拟器上设置中文输入
- svn在linux下的使用(svn命令)