本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正。


本文相关目录:

================== 所属文集:【iOS】07 设备工具 ==================

7.4 定位服务->1.0 简介

7.4 定位服务->2.1.1 定位 - 官方框架CoreLocation: 请求用户授权

7.4 定位服务->2.1.2 定位 - 官方框架CoreLocation: CLLocationManager位置管理器

7.4 定位服务->2.1.3.1 定位 - 官方框架CoreLocation 功能1:地理定位

7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码

7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听

7.4 定位服务->2.1.4 定位 - 官方框架CoreLocation 案例:指南针效果

7.4 定位服务->2.2 定位 - locationManager框架

7.4 定位服务->3.1 地图框架MapKit 功能1:地图展示

7.4 定位服务->3.2 地图框架MapKit 功能2:路线规划(导航)

7.4 定位服务->3.3 地图框架MapKit 功能3:3D视图

7.4 定位服务->3.4 地图框架MapKit 功能4:地图截图

7.4 定位服务->3.5 地图框架MapKit 功能5:POI检索

================== 所属文集:【iOS】07 设备工具 ==================


定位目录:

官方框架CoreLocation目录:

定位的功能实现:

本文目录:


1.0 概念解释


2.0 监听思路


3.0 问题:区域监听, 测试没有效果?


代码7:区域监听 Demo

编译环境:Xcode 8.0

模拟器版本:iOS 10

Swift版本:3.0

【OC 语言】

#import "ViewController.h"
#import <CoreLocation/CoreLocation.h> @interface ViewController () <CLLocationManagerDelegate>
@property(nonatomic, strong) CLLocationManager *locationM;
@end @implementation ViewController #pragma mark - 懒加载
- (CLLocationManager *)locationM {
    if (!_locationM) {
        // 创建CLLocationManager对象并设置代理
        _locationM = [[CLLocationManager alloc] init];
        _locationM.delegate = self;
        
        // 请求前后台定位, 或前台定位授权, 并在Info.Plist文件中配置相应的Key
        if ([_locationM respondsToSelector:@selector(requestAlwaysAuthorization)]) {
            [_locationM requestAlwaysAuthorization];
        }
    }
    return _locationM;
} - (void)viewDidLoad {
    [super viewDidLoad];
    
    if([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]])
    {
    
    // 0.判断区域监听服务是否可用(定位服务是否关闭, 定位是否授权,是否开启飞行模式)
        if ([CLLocationManager isMonitoringAvailableForClass:[CLCircularRegion class]]) {
      
        // 1.创建区域中心
        CLLocationCoordinate2D center = CLLocationCoordinate2DMake(21.123, 124.345);
        // 指定区域半径
        CLLocationDistance radius = 100;
        
        // 区域半径如果大于最大区域监听半径,则无法监听成功
        if (radius > self.locationM.maximumRegionMonitoringDistance) {
            radius = self.locationM.maximumRegionMonitoringDistance;
        }
        
        // 根据区域中心和区域半径创建一个区域
        CLCircularRegion *region = [[CLCircularRegion alloc] initWithCenter:center
                                                                     radius:radius
                                                                 identifier:@"TD"];
        
        // 2. 开始监听指定区域 (这个方法, 只会当进入或者离开区域这个动作触发时, 才会调用对应的代理方法)
        [self.locationM startMonitoringForRegion:region];
        
        // 请求获取某个区域的当前状态
        [self.locationM requestStateForRegion:region];
    } else {
        NSLog(@"区域监听不可用");
    }
 }
} #pragma mark - CLLocationManagerDelegate
#pragma mark - 进入监听区域后调用(调用一次)
- (void)locationManager:(nonnull CLLocationManager *)manager didEnterRegion:(nonnull CLRegion *)region {
    
    NSLog(@"进入区域---%@", region.identifier);
} #pragma mark - 进入监听区域后调用(调用一次)
- (void)locationManager:(nonnull CLLocationManager *)manager didExitRegion:(nonnull CLRegion *)region {
    
    NSLog(@"离开区域---%@", region.identifier);
} #pragma mark - 当监听区域失败时调用
// 监听区域个数是有上限的,如果大于上限,再注册区域就会失败,就会执行此方法)
- (void)locationManager:(nonnull CLLocationManager *)manager
monitoringDidFailForRegion:(nullable CLRegion *)region
              withError:(nonnull NSError *)error {
    
    // 经验: 一般都是在此处把比较远的区域给移除
//    [manager stopMonitoringForRegion:region];
} #pragma mark - 请求某个区域状态时, 回调的代理方法
- (void)locationManager:(CLLocationManager *)manager
      didDetermineState:(CLRegionState)state
              forRegion:(CLRegion *)region {
    
    switch (state) {
        case CLRegionStateUnknown:
            NSLog(@"未知状态");
            break;
        case CLRegionStateInside:
            NSLog(@"在区域内部");
            break;
        case CLRegionStateOutside:
            NSLog(@"在区域外部");
            break;
        default:
            break;
    }
} - (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
} @end

打印结果:

OC - 区域监听[12621:389819] 在区域内部

OC - 区域监听[12621:389819] 离开区域---TD
OC - 区域监听[12621:389819] 在区域外部 OC - 区域监听[12621:389819] 进入区域---TD
OC - 区域监听[12621:389819] 在区域内部

【Swift语言】


import UIKit
import CoreLocation class ViewController: UIViewController {
    
    @IBOutlet weak var Label: UILabel!
    
    lazy var locationM: CLLocationManager = {
        
        let locationM = CLLocationManager()
        locationM.delegate = self
        
        // 请求授权 配置key
        if #available(iOS 8.0, *) {
            locationM.requestAlwaysAuthorization()
        }
        return locationM
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        // 如果想要进行区域监听, 在ios8.0之后, 必须要请求用户的位置授权
        
        // 0. 先判断区域监听是否可用
        if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self){
        
        // 1. 创建区域中心
        let center = CLLocationCoordinate2DMake(21.123, 124.345)
        var distance: CLLocationDistance = 10
        
        // 区域半径如果大于最大区域监听半径,则无法监听成功
        if distance > locationM.maximumRegionMonitoringDistance {
            distance = locationM.maximumRegionMonitoringDistance
        }
        
        // 根据区域中心和区域半径创建一个区域
        let region  = CLCircularRegion(center: center, radius: distance, identifier: "TD")
        
        // 2. 开始监听指定区域 (这个方法, 只会当进入或者离开区域这个动作触发时, 才会调用对应的代理方法)
        locationM.startMonitoring(for: region)
        
        // 请求获取某个区域的当前状态
        locationM.requestState(for: region)
        
        }else{
            print("区域监听不可用")
        }
    }
    
    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
} extension ViewController: CLLocationManagerDelegate {
    
    // 进入区域时调用
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("进入区域--" + region.identifier)
        Label.text = "进入区域"
    }
    
    // 离开区域时调用
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("离开区域--" + region.identifier)
        Label.text = "离开区域"
    }
    
    // 当请求某个区域状态时, 如果获取到对应状态就会调用这个方法
    func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
        
        if region.identifier == "TD" {
            if state == CLRegionState.inside{
                Label.text = "您已进入---" + region.identifier + "区域"
            }else if state == CLRegionState.outside {
                Label.text = "您已离开---" + region.identifier + " 区域"
            }else {
                Label.text = "其他"
            }
        }
    }    
}

打印结果:

进入区域--TD
离开区域--TD

本文源码 Demo 详见 Github

https://github.com/shorfng/iOS_7.0_Device-Tools


作者:蓝田(Loto)
【作品发布平台】

简书

博客园

Gitbook(如果觉得文章太长,请阅读此平台发布的文章)

【代码托管平台】

Github

【如有疑问,请通过以下方式交流】

评论区回复

发送邮件shorfng@126.com

本文版权归作者和本网站共有,欢迎转载,但未经作者同意必须保留此段声明,且在文章页面明显位置给出原文连接,谢谢合作。


如果觉得我的文章对您有用,请随意打赏。您的支持将鼓励我继续创作!
  • 支付宝扫一扫 向我打赏

  • 你也可以微信 向我打赏

【iOS】7.4 定位服务->2.1.3.3 定位 - 官方框架CoreLocation 功能3:区域监听的更多相关文章

  1. 【iOS】7.4 定位服务->2.1.3.1 定位 - 官方框架CoreLocation 功能1:地理定位

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  2. 【iOS】7.4 定位服务->2.1.3.2 定位 - 官方框架CoreLocation 功能2:地理编码和反地理编码

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  3. 【iOS】7.4 定位服务->2.1.1 定位 - 官方框架CoreLocation: 请求用户授权

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  4. 【iOS】7.4 定位服务->2.1.2 定位 - 官方框架CoreLocation: CLLocationManager(位置管理器)

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  5. 【iOS】7.4 定位服务->2.1.4 定位 - 官方框架CoreLocation 案例:指南针效果

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  6. iOS地图 -- 区域监听的实现和小练习

    区域监听用到的方法 [self.mgr startMonitoringForRegion:region]; --> 开启区域监听,没有返回值,在代理方法中得到信息并且处理信息 注:该方法只有用户 ...

  7. 【iOS】7.4 定位服务->3.1 地图框架MapKit 功能1:地图展示

    > 本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. --- > 本文相关目录: ================== 所属文集:[[ ...

  8. 【iOS】7.4 定位服务->3.2 地图框架MapKit 功能2:路线规划(导航)

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

  9. 【iOS】7.4 定位服务->3.3 地图框架MapKit 功能3:3D视图

    本文并非最终版本,如果想要关注更新或更正的内容请关注文集,联系方式详见文末,如有疏忽和遗漏,欢迎指正. 本文相关目录: ================== 所属文集:[iOS]07 设备工具 === ...

随机推荐

  1. SpringMVC通过实体类返回json格式的字符串,并在前端显示

    一.除了搭建springmvc框架需要的jar包外,还需要这两个jar包 jackson-core-asl-1.9.2.jar和jackson-mapper-asl-1.9.2.jar 二.web,. ...

  2. spring 注解配置

    要在spring mvc中使用注解需要在*-servlet.xml文件中添加 <mvc:annotation-driver />配置 这个配置会创建DefaultAnnotationHan ...

  3. W3Cschool学习笔记——XHTML基础教程

    XHTML 是更严格更纯净的 HTML 代码. XHTML 是什么? XHTML 指可扩展超文本标签语言(EXtensible HyperText Markup Language). XHTML 的目 ...

  4. 关于BOM的理解

    BOM提供了很多对象,用于访问浏览器的功能,这些功能与网页内容无关 BOM的核心对象时window,她表示浏览器的一个实例 window的双重角色   1.JS访问浏览器窗口的一个接口  2.ECMA ...

  5. JavaScript中国象棋程序(2) - 校验棋子走法

    "JavaScript中国象棋程序" 这一系列教程将带你从头使用JavaScript编写一个中国象棋程序.这是教程的第2节. 这一系列共有9个部分: 0.JavaScript中国象 ...

  6. groovy学习(一)列表

    numbers = [11, 12, 13, 14]println(numbers[0])println(numbers[3])println(numbers[-1])//最左边的元素println( ...

  7. 【转】jqGrid学习之参数

    jqGrid参数 名称 类型 描述 默认值 可修改 url string 获取数据的地址 datatype string 从服务器端返回的数据类型,默认xml.可选类型:xml,local,json, ...

  8. SimpleDateFormat的线程安全问题与解决方案

    SimpleDateFormat 是 Java 中一个常用的类,该类用来对日期字符串进行解析和格式化输出,但如果使用不小心会导致非常微妙和难以调试的问题. 因为 DateFormat 和 Simple ...

  9. 2017-2-28 C#基础 数组

    1.什么是数组? 数组就是具有相同数据类型变量的集合. 2.数组的作用:操作大量数据. 3.数组的定义要求:(1)数组里面的内容必须是同一类型.(2)数组必须有长度限制. 4.数组分为一维数组,二维数 ...

  10. 一个简单的php站点配置

    一个简单的php站点配置   现在我们来看在一个典型的,简单的PHP站点中,nginx怎样为一个请求选择location来处理:   server {     listen      80;     ...