关于LBS和CoreLocation

最近在做关于地理位置统计相关业务,所以就全面了解了这块的知识,闲来无事就做一下笔记吧。

LBS Location Based Service 基站定位服务

关于LBS某度是这样解释的

LBS定义

首先关于蜂窝数据信息API都来自于这个框架CoreTelephony.framework, WIFI信息API是在这个系统框架SystemConfiguration.framework

cellular 运营商信息

//CTCarrier 对象包含运营商的信息
CTTelephonyNetworkInfo *networkInfo = [[CTTelephonyNetworkInfo alloc] init];
CTCarrier *carrier = [networkInfo subscriberCellularProvider];

NSString *carrierName = [JPUSHCTCarrierInfo() carrierName]; //运营商名称
NSString *mobileNetworkCode = [JPUSHCTCarrierInfo() mobileNetworkCode];  //MNC 移动设备网络代码
NSString *countryCode = [JPUSHCTCarrierInfo() mobileCountryCode]; //MCC 移动设备国家代码

移动网络代码

WIFI信息

 NSString *macIp = @"Not Found"; //mac地址
 NSString *ssid = @"Not Found";  //wifi名
  CFArrayRef myArray = CNCopySupportedInterfaces();
  if (myArray != nil) {
    CFDictionaryRef myDict =
            CNCopyCurrentNetworkInfo(CFArrayGetValueAtIndex(myArray, 0));
    if (myDict != nil) {
      NSDictionary *dict = (NSDictionary *)CFBridgingRelease(myDict);

      macIp = [dict valueForKey:@"BSSID"];
      ssid = [dict valueForKey:@"SSID"];
    }
    CFRelease(myArray);
  }

然后重点讲一下关于gps定位,这个就涉及到我们的CoreLocation.framework

CoreLocation

这里有一篇文章很不错Core Location in i​OS 8

当我们的app需要用到用户的位置时,我们就需要正确而又高效的使用CoreLocation.framework,这里主要记录一下使用这个框架的一些典型问题。

关于请求权限

首先如果是iOS8,或者iOS9,你需要在你的工程的info.plist添加下面的两个key

  • NSLocationWhenInUseUsageDescription
  • NSLocationAlwaysUsageDescription

NOTE: 在iOS10上又有一些新的变化,需要添加的是

  • Privacy - Location When In Use Usage Description
  • Privacy - Location Always Usage Description

不过记得要在上线的时候提供一个明确的原因(关于为什么需要获取位置),否则可能被拒。

其次你需要去调用requestAlwaysAuthorization 或者 requestWhenInUseAuthorization 去向用户请求获取位置信息权限。

之后就可以调用startUpdateLocation来开启定位啦,当然一定要记得获取定位之后及时停止定位,否则会很耗电量和流量。

1.获取定位权限
    self.locManager = [[CLLocationManager alloc] init];
    self.locManager.delegate = self;
    self.locManager.desiredAccuracy = kCLLocationAccuracyBest;

    if ([CLLocationManager locationServicesEnabled]) {
        if([self.locManager respondsToSelector:@selector(requestWhenInUseAuthorization)]){
            [self.locManager requestWhenInUseAuthorization];
        }else {
            [self.locManager startUpdatingLocation];
        }
    }
2.在代理方法中获取位置信息    
#pragma mark - CLLocationManagerDelegate
- (void)locationManager:(CLLocationManager *)manager didChangeAuthorizationStatus:(CLAuthorizationStatus)status {
    switch (status) {
        case kCLAuthorizationStatusNotDetermined:
            NSLog(@"用户还没选择***");
            break;
        case kCLAuthorizationStatusRestricted:
            NSLog(@"受限制***");
            break;
        case kCLAuthorizationStatusDenied:
            NSLog(@"用户拒绝或者定位服务没开***");
            break;
        case kCLAuthorizationStatusAuthorizedAlways:
            NSLog(@"iOS8以上 任何时候***");
            [self.locManager startUpdatingLocation];
            break;
        case kCLAuthorizationStatusAuthorizedWhenInUse:
            NSLog(@"iOS8以上 在使用应用期间***");
            [self.locManager startUpdatingLocation];
            break;
    }
}

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray<CLLocation *> *)locations {
    NSLog(@"locations: %@", locations);
    for (CLLocation *location in locations) {
        NSLog(@"longitude:%f latitude:%f, altitude: %f, horizontalAccuracy: %f, verticalAccuracy: %f", location.coordinate.longitude, location.coordinate.latitude, location.altitude, location.horizontalAccuracy, location.verticalAccuracy);
    }
    [self.locManager stopUpdatingLocation];
}

- (void)locationManager:(CLLocationManager *)manager didFailWithError:(NSError *)error {
    NSLog(@"error %@", error);
    [self.locManager stopUpdatingLocation];
}

处理回调方法的多次调用

LocationManager通过locationManager:didUpdateLocations:方法返回用户的位置,你可能会疑问为什么这个方法总是会被调用很多次?为什么这个locations数组的lastObject是一个很“老”的数据。

为了搞清楚这里面的原因,我们可以研究下CLLocationManager在获取位置数据的时候到底发生了什么。苹果官方文档这样描述:

Calculating a phone’s location using just GPS satellite data can take up to several minutes. iPhone can reduce this time to just a few seconds by using Wi-Fi hotspot and cell tower data to quickly find GPS satellites.

CLLocationManager 尝试尽量快的提供位置数据给你,方便你可以处理后续逻辑而不是为了获取位置而等待几秒。因此,在上一次你调用[self.locManager startUpdatingLocation]的时候系统帮你缓存了地理位置数据。

为了验证这个结论,你可以这样测试一下:先卸载你的应用->关闭定位服务->打开定位服务->重新安装应用->开启应用。(注意如果你的设备有其他开启定位服务的应用全部都要关闭,定位是系统服务,所以缓存数据也是系统完成的)你会看到两件事:

  1. 过了几秒才会调用这个回调方法locationManager:didUpdateLocations:
  2. 这个方法只会被执行一次。

然后,你关掉应用,稍微等一会再重新启动,你会发现两件不一样的事情:

  1. 这个回调方法立马就被调用(带着缓存数据)
  2. 这个回调方法会被调用两次或者更多次(这个取决于你关掉应用的时间和你的定位精度)

如果你不想获取之前的定位信息,可以通过location的时间戳来进行筛选。具体怎么处理这个依据你的app逻辑。

- (void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations{

    NSTimeInterval locationAge = [[(CLLocation *)[locations lastObject] timestamp] timeIntervalSinceNow];
    NSLog(@"how old the location is: %.5f", locationAge);
}

先写到这吧,以后有新的知识继续补充。。。

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值