iPhone获取当前位置(CoreLocation的一些简单使用)

获取用户位置

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 
Listing 1-1 Starting the standard location service- (void)startStandardUpdates{ // 创建location manager if (nil == locationManager) locationManager = [[CLLocationManager alloc] init]; locationManager.delegate = self;  // 设置获取位置的精确度,越精确越耗电 locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; // 设置距离过滤器,超过次距离就更新一次位置 locationManager.distanceFilter = 500; [locationManager startUpdatingLocation];}
使用location manager之前一般要检查位置服务是否可用,+ (BOOL)locationServicesEnabled

当位置信息更新时,会给location manager发送消息

2.Significant-Change Location Service- (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];}可以叫醒在后台的程序
3.Region monitoring Service使用之前调用CLLocationManager的regionMonitoringAvailable and regionMonitoringEnabled
- (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;}
接受位置更新消息
当位置更新时,location manager向其代理发送 locationManager:didUpdateToLocation:fromLocation:

如果无法获取新位置,则发送locationManager:didFailWithError:

// Delegate method from the CLLocationManagerDelegate protocol.- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation{ // If it's a relatively recent event, turn off updates to save power NSDate* eventDate = newLocation.timestamp; NSTimeInterval howRecent = [eventDate timeIntervalSinceNow]; if (abs(howRecent) < 15.0) { NSLog(@"latitude %+.6f, longitude %+.6f\n", newLocation.coordinate.latitude, newLocation.coordinate.longitude); } // else skip the event and process the next one.}
获取移动方向信息

- (void)startHeadingEvents { if (!self.locationManager) { CLLocationManager* theManager = [[[CLLocationManager alloc] init] autorelease]; // Retain the object in a property. self.locationManager = theManager; self.locationManager.delegate = self; } // Start location services to get the true heading. locationManager.distanceFilter = 1000; locationManager.desiredAccuracy = kCLLocationAccuracyKilometer; [locManager startUpdatingLocation]; // 开始更新移动方向 if ([CLLocationManager headingAvailable]) { locManager.headingFilter = 5; [locManager startUpdatingHeading]; } } 

- (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { if (newHeading.headingAccuracy < 0) return; // Use the true heading if it is valid. CLLocationDirection theHeading = ((newHeading.trueHeading > 0) ? newHeading.trueHeading : newHeading.magneticHeading); self.currentHeading = theHeading; [self updateHeadingDisplays]; } 

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值