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

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

2.Significant-Change Location Service

[plain]
  1. <span style="font-size:16px;">- (void)startSignificantChangeUpdates  
  2. {  
  3.     // Create the location manager if this object does not  
  4.     // already have one.  
  5.     if (nil == locationManager)  
  6.         locationManager = [[CLLocationManager alloc] init];  
  7.    
  8.     locationManager.delegate = self;  
  9.     [locationManager startMonitoringSignificantLocationChanges];  
  10. }</span>  

可以叫醒在后台的程序


3.Region monitoring Service 使用之前调用CLLocationManager的regionMonitoringAvailable and regionMonitoringEnabled
[plain]
  1. <span style="font-size:16px;">- (BOOL)registerRegionWithCircularOverlay:(MyCircle*)overlay andIdentifier:(NSString*)identifier  
  2. {  
  3.    // Do not create regions if support is unavailable or disabled.  
  4.    if ( ![CLLocationManager regionMonitoringAvailable] ||  
  5.         ![CLLocationManager regionMonitoringEnabled] )  
  6.       return NO;  
  7.   
  8.    // If the radius is too large, registration fails automatically,  
  9.    // so clamp the radius to the max value.  
  10.    CLLocationDegrees radius = overlay.radius;  
  11.    if (radius > self.locationManager.maximumRegionMonitoringDistance)  
  12.       radius = self.locationManager.maximumRegionMonitoringDistance;  
  13.   
  14.    // Create the region and start monitoring it.  
  15.    CLRegion* region = [[CLRegion alloc] initCircularRegionWithCenter:overlay.coordinate  
  16.                         radius:radius identifier:identifier];  
  17.    [self.locationManager startMonitoringForRegion:region  
  18.                     desiredAccuracy:kCLLocationAccuracyHundredMeters];  
  19.   
  20.    [region release];  
  21.    return YES;  
  22. }</span>  
  • 1
  • 2
  • 下一页

相关内容