Posts

Post not yet marked as solved
0 Replies
343 Views
So, i am making an app in which you enter coordinates, confirm them, and then an arrow pops up that shows the direction to the place which coordinates user entered. However when i click confirm, the arrow shows up but doesnt rotate. When i tried some troubleshooting, i found out that arrowImageView is nil. Can anyone please help? ` import UIKit import CoreLocation extension CGFloat { func toRadians() -> CGFloat { return self * CGFloat.pi / 180.0 } } class ViewController: UIViewController { @IBOutlet weak var Latitude_text: UITextField! @IBOutlet weak var Longitude_text: UITextField! @IBOutlet var arrowImageView: UIImageView! let locationManager = CLLocationManager() var destinationCoordinate: CLLocationCoordinate2D? override func viewDidLoad() { super.viewDidLoad() locationManager.delegate = self locationManager.requestWhenInUseAuthorization() locationManager.startUpdatingLocation() locationManager.startUpdatingHeading() } @IBAction func Latitude_import(_ sender: Any) { validateCoordinates() } @IBAction func Longitude_input(_ sender: Any) { validateCoordinates() } @IBAction func confirmButtonTapped(_ sender: UIButton) { validateCoordinates() performSegue(withIdentifier: "ShowArrowScreen", sender: self) } func validateCoordinates() { guard var latitudeText = Latitude_text?.text, !latitudeText.isEmpty, var longitudeText = Longitude_text?.text, !longitudeText.isEmpty else { // Handle empty fields print("Invalid coordinates. Please enter valid latitude and longitude.") return } // Replace commas with dots latitudeText = latitudeText.replacingOccurrences(of: ",", with: ".") longitudeText = longitudeText.replacingOccurrences(of: ",", with: ".") guard let latitude = Double(latitudeText), let longitude = Double(longitudeText) else { // Handle invalid text print("Invalid coordinates. Please enter valid latitude and longitude.") return } destinationCoordinate = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) updateUIWithBearing() } func calculateBearing(from source: CLLocationCoordinate2D, to destination: CLLocationCoordinate2D) -> Double { let theta = atan2(destination.longitude - source.longitude, destination.latitude - source.latitude) return (theta * (180.0 / .pi) + 360.0).truncatingRemainder(dividingBy: 360.0) } func updateUIWithBearing() { guard let currentLocation = locationManager.location, let destination = destinationCoordinate else { print("Error: Could not retrieve location or destination coordinates.") return } let bearing = calculateBearing(from: currentLocation.coordinate, to: destination) print("Bearing to destination: \(bearing) degrees") rotateArrow(bearing: CGFloat(bearing)) } func rotateArrow(bearing: CGFloat) { print("Rotate Arrow function called.") guard let arrowImageView = arrowImageView else { print("Error: Arrow image view is nil.") return } guard let frameworkBundle = Bundle(identifier: "com.myframework.name") else { print("Error: Unable to load framework bundle.") return } guard let arrowImage = UIImage(named: "myImage", in: frameworkBundle, compatibleWith: nil) else { print("Error: Arrow image is nil.") return } arrowImageView.image = arrowImage let radians = bearing.toRadians() UIView.animate(withDuration: 0.5, animations: { arrowImageView.transform = CGAffineTransform.identity.rotated(by: radians) }) { _ in print("Rotation animation completed.") } } } extension ViewController: CLLocationManagerDelegate { func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { let heading = newHeading.trueHeading print("Current heading: \(heading) degrees") } func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { if let location = locations.last { print("Updated location: \(location.coordinate.latitude), \(location.coordinate.longitude)") } } }
Posted
by VojTek.
Last updated
.