查看用户是否在某个位置附近? Swift 4
See if the user is near a location? Swift 4
我想知道用户是否在 Swift 中其他位置的 5 公里半径范围内 4.
例如:
User Location: 37.785834, -122.406417
Other Location: -117.564011, 48.302353
感谢所有帮助过的人!
您可以使用Region Monitoring feature provided by CLLocationManager检测用户何时自动进入或离开地理区域。
开始监控指定坐标周围的圆圈区域:
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)
}
}
通过实施来自 CLLocationManagerDelegate:
的适当方法来处理与区域相关的通知(在此示例中输入通知)
func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if let region = region as? CLCircularRegion {
// your logic to handle region-entered notification
}
}
我想知道用户是否在 Swift 中其他位置的 5 公里半径范围内 4.
例如:
User Location: 37.785834, -122.406417
Other Location: -117.564011, 48.302353
感谢所有帮助过的人!
您可以使用Region Monitoring feature provided by CLLocationManager检测用户何时自动进入或离开地理区域。
开始监控指定坐标周围的圆圈区域:
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)
}
}
通过实施来自 CLLocationManagerDelegate:
的适当方法来处理与区域相关的通知(在此示例中输入通知)func locationManager(_ manager: CLLocationManager, didEnterRegion region: CLRegion) {
if let region = region as? CLCircularRegion {
// your logic to handle region-entered notification
}
}