【问题标题】:How to setup a "Check-In" alert using Geofencing and CLCircularRegion?如何使用地理围栏和 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)
  }


}

【问题讨论】:

    标签: ios swift geolocation core-location


    【解决方案1】:
    1. CLCircularRegion 上设置notifyOnEntry = true 以在进入或离开该区域时得到通知。
    2. 实现 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

    【讨论】:

      猜你喜欢
      • 2019-02-01
      • 2014-07-05
      • 1970-01-01
      • 2015-11-10
      • 2014-08-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多