我正在构建一个应用程序来检测您是否在地图上绘制的区域中。看来,当我测试它时,实际地理围栏区域的半径大于我绘制的圆。我的代码如下:
func setupGeoFence() {
let radius = 150
let location = CLLocation(latitude: 44, longitude: -79)
geofenceRegion = CLCircularRegion(center: location.coordinate, radius: CLLocationDistance(radius), identifier: "location")
removeRadiusCircle(mapView: mapView)
addRadiusCircle(location: location, radius: CLLocationDistance(Float(radius)), mapView: mapView)
geofenceRegion?.notifyOnExit = true
geofenceRegion?.notifyOnEntry = true
self.locationManager.startMonitoring(for: geofenceRegion!)
self.locationManager.requestState(for: geofenceRegion!)
}
func addRadiusCircle(location: CLLocation,radius: CLLocationDistance, mapView: MKMapView){
let circle = MKCircle(center: location.coordinate, radius: radius)
let pin = MKPointAnnotation()
pin.coordinate = location.coordinate
mapView.addAnnotation(pin)
mapView.add(circle)
}