Swift 4 MapKit 检查标记是否靠近我的位置
Swift 4 MapKit check if mark is near to my location
如果我的位置靠近标记,我想检查我的位置何时发生变化。我有独特位置的标记。例如,如果我距离标记 10 米,我想显示警报。我该怎么做?
您可以从 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 }
}
- 获取可见注释。
- 计算你的位置和标记之间的距离。
例如
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 }
}
}
}
如果我的位置靠近标记,我想检查我的位置何时发生变化。我有独特位置的标记。例如,如果我距离标记 10 米,我想显示警报。我该怎么做?
您可以从 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 }
}
- 获取可见注释。
- 计算你的位置和标记之间的距离。
例如
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 }
}
}
}