+ (BOOL)coordinate:(CLLocationCoordinate2D)coord inRegion:(MKCoordinateRegion)region
{
CLLocationCoordinate2D center = region.center;
MKCoordinateSpan span = region.span;
BOOL result = YES;
result &= cos((center.latitude - coord.latitude)*M_PI/180.0) > cos(span.latitudeDelta/2.0*M_PI/180.0);
result &= cos((center.longitude - coord.longitude)*M_PI/180.0) > cos(span.longitudeDelta/2.0*M_PI/180.0);
return result;
}
Swiftで書くとこのようになると思います。
func checkIfCoordinateInRegion(coordinate: CLLocationCoordinate2D, region: MKCoordinateRegion) -> Bool {
let center = region.center
let span = region.span
return cos((center.latitude - coordinate.latitude)*Double.pi/180.0) > cos(span.latitudeDelta/2.0*Double.pi/180.0) && cos((center.longitude - coordinate.longitude)*Double.pi/180.0) > cos(span.longitudeDelta/2.0*Double.pi/180.0)
}
なぜcosで計算すると良いのかがパッと見分からなかったので考えてみました。
まず、地球を描きます。
The EarthDraw imaginary latitude and longtitude.Draw an imaginary span areaViewing the earth from the side.Here is cosθ of your span area.If a point is inside the span area, its cosθ’ will always be greater than cosθ.
func displayFrame(_ frame: CVPixelBuffer?, withAffineTransform transform: CGAffineTransform) {
DispatchQueue.main.async { // updating the UI needs to happen on the main thread
if let frame = frame {
let ciImage = CIImage(cvPixelBuffer: frame)
.transformed(by: transform) // applying affineTransform here, otherwise a portrait image will be displayed in landscape.
let ciContext = CIContext(options: nil)
guard let cgImage = ciContext.createCGImage(ciImage, from: ciImage.extent) else { return }
let uiImage = UIImage(cgImage: cgImage)
self.imageView.image = uiImage
}
}
}
// this method happens after all the layouts has been done. このメソッドは全てのレイアウト後に呼ばれる
override func layoutSubviews() {
super.layoutSubviews()
let layer = sampleImageView.layer
layer.cornerRadius = 30
}