Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/106.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 检测用户是否走出MKCircle | MapKit_Ios_Swift_Mapkit_Core Location_Mkcircle - Fatal编程技术网

Ios 检测用户是否走出MKCircle | MapKit

Ios 检测用户是否走出MKCircle | MapKit,ios,swift,mapkit,core-location,mkcircle,Ios,Swift,Mapkit,Core Location,Mkcircle,我需要的东西(更简单的解释): [Done]获取用户位置并在UIMapView中显示 [Done]在UIMapView中添加环绕用户的圆圈 检测用户是否离开该圆圈 这是我的1和2代码: import UIKit import MapKit import CoreLocation class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { @IBOutlet weak

我需要的东西(更简单的解释):

  • [Done]获取用户位置并在UIMapView中显示
  • [Done]在UIMapView中添加环绕用户的圆圈
  • 检测用户是否离开该圆圈
  • 这是我的1和2代码:

    import UIKit 
    import MapKit 
    import CoreLocation
    
    class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate {
    
        @IBOutlet weak var myMap: MKMapView!
    
        let manager = CLLocationManager()
        var addedCircle = false
    
    
        func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
            let location = locations[0]
    
            let span: MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
            let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,
                                                                            location.coordinate.longitude)
            let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
    
            myMap.setRegion(region, animated: true)
    
    
    
            self.myMap.showsUserLocation = true
    
            if !addedCircle {
                self.addRadiusCircle(location: location)
                putted = true
            }
    
        }
    
    
    
        func addRadiusCircle(location: CLLocation){
            self.myMap.delegate = self
            let circle = MKCircle(center: location.coordinate, radius: 100)
            self.myMap.add(circle)
        }
    
        func mapView(_ mapView: MKMapView, rendererFor overlay: MKOverlay) -> MKOverlayRenderer {
            if overlay is MKCircle {
                let circle = MKCircleRenderer(overlay: overlay)
                circle.strokeColor = UIColor.red
                circle.fillColor = UIColor(red: 255, green: 0, blue: 0, alpha: 0.1)
                circle.lineWidth = 1
                return circle
            } else {
                return MKPolylineRenderer()
            }
        }
    
    
        override func viewDidLoad() {
            super.viewDidLoad()
    
            manager.delegate = self
            manager.desiredAccuracy = kCLLocationAccuracyBest
            manager.requestWhenInUseAuthorization()
            manager.startUpdatingLocation()
    
        }
    
    
    }
    
    这是这个代码的结果:


    该圆圈不随用户移动,我想知道如何检测用户是否从该红色圆圈中走出来,提前感谢您的回答。您可以使用
    CLLocation的
    distance(from:)
    方法来确定用户当前位置与圆圈中心之间的距离

    let location = CLLocation()
    let circleCenter = CLLocation()
    
    if location.distance(from: circleCenter) > circleRadius {
        // User is outside of circle.
    }
    

    您可以使用
    CLLocation
    distance(from:)
    方法来确定用户当前位置与圆中心之间的距离

    let location = CLLocation()
    let circleCenter = CLLocation()
    
    if location.distance(from: circleCenter) > circleRadius {
        // User is outside of circle.
    }
    

    你是说区域监控吗?链接到文档:但是Stackoverflow上有许多区域监控问题。您需要更具体地说明您的问题。您是指区域监控吗?链接到文档:但是Stackoverflow上有许多区域监控问题。你需要更具体地说明你的问题。