【问题标题】:Swift 4 MapKit check if mark is near to my locationSwift 4 MapKit 检查标记是否靠近我的位置
【发布时间】:2018-08-22 13:31:40
【问题描述】:

如果我的位置在标记附近,我想检查我的位置何时发生变化。我有独特位置的标记。例如,如果我离标记近 10m,我想显示警报。我该怎么做?

【问题讨论】:

    标签: swift location mapkit


    【解决方案1】:
    1. 获取可见注释。
    2. 计算您的位置与标记之间的距离。

    例如

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        guard let myLocation = locations.first else { return }
    
        let annotationSet = mapView.annotations(in: mapView.visibleMapRect)
        for annotation in annotationSet {
            guard let annotation = annotation as? MKPointAnnotation else { continue }
            let loc = CLLocation(latitude: annotation.coordinate.latitude,
                                 longitude: annotation.coordinate.longitude)
            let distance = myLocation.distance(from: loc)
            if distance < 10.0 { Show Alert }
            }
        }
    }
    

    【讨论】:

      【解决方案2】:

      您可以从 CLLocationManager 实现此委托:

      func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          let myLoc:CLLocationCoordinate2D = manager.location!.coordinate
          let distance : CLLocationDistance = mark1.distanceFromLocation(myLoc)
      
          if distance < 10.0 { //Show Alert }
      }
      

      【讨论】:

      • 在for循环中计算上面的两行怎么样?
      • 即使你有 900 分,你不会只显示一个警报吗?您可以在 for 循环中设置一个 bool 或 count 变量,如果 count 变量大于 1 或 bool 设置为 true,则显示警报或执行代码的下一部分。
      猜你喜欢
      • 1970-01-01
      • 2017-10-10
      • 2014-06-26
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多