I would like to be-able to see if the user is in a 5 km radius of the other location in Swift 4.
For example:
User Location: 37.785834, -122.406417
Other Location: -117.564011, 48.302353
Thanks for anyone who helps!
I would like to be-able to see if the user is in a 5 km radius of the other location in Swift 4.
For example:
User Location: 37.785834, -122.406417
Other Location: -117.564011, 48.302353
Thanks for anyone who helps!
You can detect when the user enters or leaves a geographic region automatically by using Region Monitoring feature provided by CLLocationManager.
Start monitoring the circle region around the specified coordinate:
let center = CLLocationCoordinate2D(latitude: 37.785834, longitude: -122.406417)
// Make sure the app is authorized.
if CLLocationManager.authorizationStatus() == .authorizedAlways {
// Make sure region monitoring is supported.
if CLLocationManager.isMonitoringAvailable(for: CLCircularRegion.self) {
// Register the region.
let maxDistance = locationManager.maximumRegionMonitoringDistance
let region = CLCircularRegion(center: center,
radius: 5000.0, identifier: "YourRegionID")
region.notifyOnEntry = true
region.notifyOnExit = false
locationManager.startMonitoring(for: region)
}
}
Handle a region-related notification (enter notification in this example) by implementing suitable method from CLLocationManagerDelegate:
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if let region = region as? CLCircularRegion {
// your logic to handle region-entered notification
}
}