创建地理围栏的最有效和最迟滞的方法是什么?

时间:2016-06-04 05:22:50

标签: ios swift mapkit

我正在尝试创建一个地理围栏,用户可以单击一个按钮,它将警告用户是否在该区域内。我有办法使这项工作,但由于我是iOS编程和swift的新手,我不确定这是否是最有效的方法。此外,用户位置点有时会滞后于我的应用程序(它表示用户位于其他位置而不是显示正确的位置)。我对地理围栏的实施是否正确?或者我错过了什么?

我使用的代码是:

显示用户位置:

func checkLocationAuthorizationStatus() {
    if CLLocationManager.authorizationStatus() == .AuthorizedWhenInUse {
        Map.showsUserLocation = true
    } else {
        locationManager.requestWhenInUseAuthorization()
    }
}

设置位置管理器

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    checkLocationAuthorizationStatus()

    if (CLLocationManager.locationServicesEnabled()) {
        locationManager.delegate = self
        locationManager.desiredAccuracy = kCLLocationAccuracyBest
        locationManager.requestAlwaysAuthorization()
        locationManager.startUpdatingLocation()
    }     
}

用于创建地理围栏

@IBAction func checkLocation(sender: UIButton) {
    let geoFence = CLCircularRegion(center: defaultLocation, radius: 15, identifier: "fence")
    if geoFence.containsCoordinate(userLocation.coordinate) {
        let title = "You are in the zone"
        let okText = "OK"

        let alert = UIAlertController(title: title, message: nil, preferredStyle: UIAlertControllerStyle.Alert)
        let okayButton = UIAlertAction(title: okText, style: UIAlertActionStyle.Cancel, handler: nil)
        alert.addAction(okayButton)

        presentViewController(alert, animated: true, completion: nil)
    } else {
        print("no")
    }
}

0 个答案:

没有答案