swift 地图定位(六)区域监听

import UIKit
import CoreLocation

class ViewController: UIViewController {

    lazy var locationM: CLLocationManager = {
        let locationM = CLLocationManager()
        locationM.delegate = self
        if #available(iOS 8.0, *) {//info.plist add: Privacy - Location Always Usage Description
            locationM.requestAlwaysAuthorization()
        }
        return locationM
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        // 1. 创建区域
        let center = CLLocationCoordinate2DMake(31.785834, 121.406417)
        let distance: CLLocationDistance = 1000
        let region  = CLCircularRegion(center: center, radius: distance, identifier: "targetcloud")
        // 2. 监听区域
        locationM.startMonitoring(for: region)
    }
}

extension ViewController: CLLocationManagerDelegate {
    // 进入区域时调用
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print(">进入区域" + region.identifier)
    }
    
    // 离开区域时调用
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
         print("离开区域<" + region.identifier)
    }
}


加入判断

import UIKit
import CoreLocation

class ViewController: UIViewController {

    @IBOutlet weak var noticeLabel: UILabel!
    
    lazy var locationM: CLLocationManager = {
        let locationM = CLLocationManager()
        locationM.delegate = self
        if #available(iOS 8.0, *) {
            locationM.requestAlwaysAuthorization()
        }
        return locationM
    }()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self){//加判断
            let center = CLLocationCoordinate2DMake(31.785834, 121.406417)
            var distance: CLLocationDistance = 1000
            if distance > locationM.maximumRegionMonitoringDistance {
                distance = locationM.maximumRegionMonitoringDistance
            }
            let region  = CLCircularRegion(center: center, radius: distance, identifier: "targetcloud")
            locationM.startMonitoring(for: region)
            locationM.requestState(for: region)
        }
    }
}

extension ViewController: CLLocationManagerDelegate {
    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print(">进入区域" + region.identifier)
    }
    
    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("离开区域<" + region.identifier)
    }
    
    func locationManager(_ manager: CLLocationManager, didDetermineState state: CLRegionState, for region: CLRegion) {
        if region.identifier == "targetcloud" {
            if state == .inside {
              noticeLabel.text = "welcome!"
            }else if state == .outside {
              noticeLabel.text = "欢迎下次再来!"
            }
        }
    }
}



评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值