如何使用地理围栏和 CLCircularRegion 设置“签到”警报?

Posted

技术标签:

【中文标题】如何使用地理围栏和 CLCircularRegion 设置“签到”警报?【英文标题】:How to setup a "Check-In" alert using Geofencing and CLCircularRegion? 【发布时间】:2016-07-13 19:53:54 【问题描述】:

我正在尝试设置警报,一旦用户输入特定位置,警报就会弹出并允许用户“签到”。用户签入应用程序后,通知 api 端点用户已成功签入。这是我第一次使用地理围栏和核心定位。我了解如何设置它的基本概念,但不完全确定签到警报和地理围栏如何结合在一起。这是我的代码:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate


  var manager = CLLocationManager()


  override func viewDidLoad() 
    super.viewDidLoad()


    // Core Location
    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()


    var latitude: CLLocationDegrees = 43.039278
    var longitude: CLLocationDegrees = -87.932479
    var center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var radius: CLLocationDistance = CLLocationDistance(10.0)
    var identifier: String = "storeID"

    var geoRegion: CLCircularRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)

  


  func showSimpleAlertWithTitle(title: String!, message: String!, viewController: UIViewController) 
    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let action = UIAlertAction(title: "Check-In", style: .Cancel , handler: nil)
    alert.addAction(action)
    viewController.presentViewController(alert, animated: true, completion: nil)
  



【问题讨论】:

【参考方案1】:
    CLCircularRegion 上设置notifyOnEntry = true 以在进入或离开该区域时得到通知。 实现 locationManager:didEnterRegion: 委托方法来处理事件。

例子:

override func viewDidLoad() 
    super.viewDidLoad()


    manager.delegate = self
    manager.desiredAccuracy = kCLLocationAccuracyBest
    manager.requestWhenInUseAuthorization()
    manager.startUpdatingLocation()

    var latitude: CLLocationDegrees = 43.039278
    var longitude: CLLocationDegrees = -87.932479
    var center: CLLocationCoordinate2D = CLLocationCoordinate2DMake(latitude, longitude)
    var radius: CLLocationDistance = CLLocationDistance(10.0)
    var identifier: String = "storeID"

    var geoRegion: CLCircularRegion = CLCircularRegion(center: center, radius: radius, identifier: identifier)
    geoRegion.notifyOnEntry = true

    manager.startMonitoringForRegion(geoRegion)



func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) 

    showSimpleAlertWithTitle("Entered region \(region.identifier)", message: nil)


func showSimpleAlertWithTitle(title: String!, message: String!) 
    let alert = UIAlertController(title: title, message: message, preferredStyle: .Alert)
    let action = UIAlertAction(title: "Check-In", style: .Cancel , handler: nil)
    alert.addAction(action)
    presentViewController(alert, animated: true, completion: nil)
  

见:CLRegion.notifyOnEntry

【讨论】:

以上是关于如何使用地理围栏和 CLCircularRegion 设置“签到”警报?的主要内容,如果未能解决你的问题,请参考以下文章

如何在 Android 中创建地理围栏?

如何使用 Android 地理围栏 API?

即使应用程序未运行,如何使用有效的地理围栏? [复制]

ibeacons 和地理围栏结合 swift

如何使地理围栏准确?

如何使用一系列信息创建地理围栏? [关闭]