gpt4 book ai didi

ios - 如何使用地理围栏和 CLCircularRegion 设置 "Check-In"警报?

转载 作者:行者123 更新时间:2023-11-30 13:14:46 25 4
gpt4 key购买 nike

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

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,以便在进入或退出该区域时收到通知。
  2. 实现locationManager:didEnterRegion:委托(delegate)方法来处理事件。

示例:

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

关于ios - 如何使用地理围栏和 CLCircularRegion 设置 "Check-In"警报?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38360567/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com