【发布时间】:2018-08-22 13:31:40
【问题描述】:
如果我的位置在标记附近,我想检查我的位置何时发生变化。我有独特位置的标记。例如,如果我离标记近 10m,我想显示警报。我该怎么做?
【问题讨论】:
如果我的位置在标记附近,我想检查我的位置何时发生变化。我有独特位置的标记。例如,如果我离标记近 10m,我想显示警报。我该怎么做?
【问题讨论】:
例如
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 }
}
}
}
【讨论】:
您可以从 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 }
}
【讨论】: