首页 > 解决方案 > 如何编写用于检查用户位置的 Swift 代码并仅在用户位于地理围栏半径内时才执行该功能?

问题描述

目标是仅在当前用户位置在预定义的地理围栏内时才允许通过点击“我在这里”发布数据。如果他们超出界限,他们将收到警报错误,并且数据不会发布到 Firestore。

我已经阅读了有关 didDetermineState 的信息,但无法理解。


import UIKit
import Firebase
import FirebaseFirestore
import MapKit
import CoreLocation

class checkInViewController: UIViewController {

    @IBOutlet weak var mapView: MKMapView!

    @IBOutlet weak var imHereButton: UIButton!
    @IBOutlet weak var callUsButton: UIButton!

    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()
        // Do any additional setup after loading the view.
        setUpElements()

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

        let geoFenceRegion:CLCircularRegion = CLCircularRegion(center: CLLocationCoordinate2DMake(xxxx, xxxx), radius: 5, identifier: "xxxx")

        locationManager.startMonitoring(for: geoFenceRegion)

    }

    func setUpElements (){
        Utilities.styleFilledButton(imHereButton)
        Utilities.styleFilledButton(callUsButton)
    }

    @IBAction func backTapped(_ sender: Any) {
        dismiss(animated: true, completion: nil)
    }


    func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
        print("Check-In Enabled")
    }

    func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
        print("Check-In Disabled")
    }

    @IBAction func imHereTapped(_ sender: Any) {

        guard let uid = Auth.auth().currentUser?.uid else { return }

        guard let memberNumber = UserDefaults.standard.string(forKey: "memberNumber"), !memberNumber.isEmpty else
        { return }
        guard let firstName = UserDefaults.standard.string(forKey: "firstName"), !firstName.isEmpty else
        { return }
        guard let lastName = UserDefaults.standard.string(forKey: "lastName"), !lastName.isEmpty else
        { return }

        let checkInData = ["name":"\(firstName) \(lastName)", "memberNumber":memberNumber, "userId": uid]

        FirestoreReferenceManager.myReferenceForCheckIn().addDocument(data: checkInData) { (error) in
            if error != nil {
                    print("Error, Data not saved")
                } else {
                self.showResAlert(title: "Welcome Back", message: "You are now CHECKED-IN please continue to the rental counter. Thank You.")
                }
        }
    }

    @IBAction func callUsTapped(_ sender: Any) {
        if let url = URL(string: "tel://\(xxxx)"),
            UIApplication.shared.canOpenURL(url) {
            UIApplication.shared.open(url, options: [:], completionHandler: nil)
        }
    }
}

extension checkInViewController: CLLocationManagerDelegate{

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        locationManager.stopUpdatingLocation()
        mapView.showsUserLocation = true
    }

}

标签: swift

解决方案


推荐阅读