Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/121.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 如何检查用户';的当前位置在位置圆内_Ios_Swift_Core Location_Cllocationmanager - Fatal编程技术网

Ios 如何检查用户';的当前位置在位置圆内

Ios 如何检查用户';的当前位置在位置圆内,ios,swift,core-location,cllocationmanager,Ios,Swift,Core Location,Cllocationmanager,我想检查用户是否在附近。例如,我指定了用户当前位置周围50米的半径。假设用户正在移动,现在我想检查用户是否在50米半径内。这是我的密码 override func viewDidLoad() { super.viewDidLoad() locationManager.startMonitoringVisits() locationManager.delegate = self locationManager.dist

我想检查用户是否在附近。例如,我指定了用户当前位置周围50米的半径。假设用户正在移动,现在我想检查用户是否在50米半径内。这是我的密码

 override func viewDidLoad() {
        super.viewDidLoad()

          locationManager.startMonitoringVisits()
          locationManager.delegate = self
          locationManager.distanceFilter = 1
          locationManager.allowsBackgroundLocationUpdates = true
          locationManager.startUpdatingLocation() 
    }


下面是检查距离的代码


    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {

        guard let location = locations.first else {
            return
        }
        let officeLocation = CLLocationCoordinate2D.init(latitude: 31.471303736482234, longitude: 74.27275174139386)

        let circle = MKCircle(center: officeLocation, radius: 50 as CLLocationDistance)
        if location.distance(from: officeLocation) > circle.radius {
            self.newVisitReceived(des: "YOU ARE OUT OF OFFICE")
        }
        else{ 
            self.newVisitReceived(des: "YOU ARE IN OFFICE")
         }

    }

即使我不移动,此代码也会发送“您外出”的通知。

定位服务的一般问题是,根据许多因素,测量的准确性会有所不同。如果用户正站在50米的边界上,您希望您的代码表现如何?如果准确性不好,您当前的代码将在“办公室内”和“办公室外”之间随机来回翻转

我认为,在最佳条件下,GPS的精度实际上超过4米,因此距离过滤器1可能不合适


我想你的应用程序中需要一些状态来跟踪用户最后一次出现在50米半径范围内的时间,以及在再次更新该变量之前的一些宽限期,以避免“闪烁”。

我会用Geofenses解决这个问题。。。 您必须指定一个坐标中心和半径,当用户从您的地理围栏进入/离开时,您希望在该坐标中心和半径处聆听用户的声音

override func viewDidLoad() {
    super.viewDidLoad()

    let locationManager = CLLocationManager()
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBestForNavigation
    locationManager.allowsBackgroundLocationUpdates = true
    locationManager.requestAlwaysAuthorization()
}

func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    if status == .authorizedAlways || status == .authorizedWhenInUse {

        // CLLocationCoordinate2D; You have to put the coordinate that you want to listen
        let region = CLCircularRegion(center: CLLocationCoordinate2D(latitude: 324234, longitude: 23423), radius: 50, identifier: "Ur ID")
        region.notifyOnExit = true
        region.notifyOnEntry = true
        manager.startMonitoring(for: region)
    }
}

func locationManager(_ manager: CLLocationManager, didExitRegion region: CLRegion) {
    // User has exited from ur regiom
}

func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
    // User has exited from ur region
}

我希望这将是有用的

可能有许多因素导致此问题。你可以使用一个模拟器,它的位置被硬编码到几个不同的位置中的一个,所有这些位置都在你的半径之外。你可能正在一台尚未收到GPS或AGPS位置更新的设备上运行,因此你的应用程序不知道(尚未)它已被移出其位置。你的意思是这段代码没有问题?你的代码看起来很好,但我认为你可能对设备(或模拟器)的来源做出了一些假设的位置数据。您的代码非常适合我的用例。谢谢你的发帖。