iOS定位机制说明

2016-12-20  本文已影响242人  Cyyyyyyyy

最近看了一下iOS系统提供的定位机制。我们现在产品里用的是高德的定位SDK,我们不必接触Core Location的细节,但是高德的SDK毕竟是对Core Location的封装,它势必继承了Core Location的特点和局限,多一些对Core Location机制的了解,能让我们在后续理解处理定位相关功能的时候多一些把握。

Core Location framework提供了两种定位机制,它们各有利弊,我们要根据实际的业务场景,考虑电量消耗和精度等情况,选择合适的实现方案。

下面是对两种机制的简要说明和代码示例:

Standard Location Service

Standard Location Service是苹果提供的最常用的定位获取和位置变化追踪的机制。大部分情况下,我们要获取用户的位置信息都是基于此。它的特点如下:

代码示例

使用Standard Location Service需要创建一个CLLocationManager对象,根据需要初始化参数desiredAccuracydistanceFilter,同时把要接收位置变化的对象设置为CLLocationManagerdelegate

以下是一段开启定位的代码示例:

- (void)startStandardUpdates {
    // Create the location manager if this object does not
    // already have one.
    if (nil == locationManager)
        locationManager = [[CLLocationManager alloc] init];
 
    locationManager.delegate = self;
    locationManager.desiredAccuracy = kCLLocationAccuracyKilometer;
 
    // Set a movement threshold for new events.
    locationManager.distanceFilter = 500; // meters
 
    [locationManager startUpdatingLocation];
}

Significant-Change Location Service

Significant-Change Location Service是苹果提供的在位置发生较大变化(500m或者更多)的时候通知应用的机制。该机制的特点如下:

- (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];
}

参考文档

Location and Maps Programming Guide
后台定位上传的代码实践
iOS后台持续定位并定时上传

上一篇 下一篇

猜你喜欢

热点阅读