As the title says.
Small info, i have used xcode for a week now and this project i am working on i havent made 80% of it, so there is still a lot i dont understand. But on this particular problem i have been stuck for a day now.
I have my file with a view and a lot of code. But in it i create a new GPSInfoView and GPSInfoViewController.
These come from another file and look like so
import UIKit
import CoreLocation
class GPSInfoView: UIView {
private var horizontalAccuracyLabel = UILabel()
private var verticalAccuracyLabel = UILabel()
private var latitudeLabel = UILabel()
private var longitudeLabel = UILabel()
private var updateButton = UIButton()
// func updateInfo(location: CLLocation) {
// setHorizontalAccuracy(location.horizontalAccuracy)
// setVerticalAccuracy(location.verticalAccuracy)
// setLatitude(location.coordinate.latitude)
// setLongitude(location.coordinate.longitude)
// }
override init(frame: CGRect) {
super.init(frame: frame)
commonInit()
}
required init?(coder: NSCoder) {
super.init(coder: coder)
commonInit()
}
private func commonInit() {
backgroundColor = .white // or .lightGray
addSubview(horizontalAccuracyLabel)
addSubview(verticalAccuracyLabel)
addSubview(latitudeLabel)
addSubview(longitudeLabel)
horizontalAccuracyLabel.frame = CGRect(x: 16.0, y: 100.0, width: 200, height: 20)
verticalAccuracyLabel.frame = CGRect(x: 16.0, y: 200.0, width: 200, height: 20)
latitudeLabel.frame = CGRect(x: 16.0, y: 300.0, width: 200, height: 20)
longitudeLabel.frame = CGRect(x: 16.0, y: 400.0, width: 200, height: 20)
// Set font and color properties
for label in [horizontalAccuracyLabel, verticalAccuracyLabel, latitudeLabel, longitudeLabel] {
label.font = UIFont.systemFont(ofSize: 14)
label.textColor = .black
}
}
@objc private func updateButtonTapped() {
}
func setHorizontalAccuracy(_ accuracy: Double) {
// horizontalAccuracyLabel.isHidden = false
print(horizontalAccuracyLabel.text)
DispatchQueue.main.async {
self.horizontalAccuracyLabel.text = "Horizontal Accuracy: \(accuracy) meters"
self.layoutIfNeeded()
}
}
func setVerticalAccuracy(_ accuracy: Double) {
DispatchQueue.main.async {
self.verticalAccuracyLabel.text = "Vertical Accuracy: \(accuracy) meters"
self.layoutIfNeeded()
}
}
func setLatitude(_ latitude: Double) {
DispatchQueue.main.async {
self.latitudeLabel.text = "Latitude: \(latitude)"
self.layoutIfNeeded()
}
}
func setLongitude(_ longitude: Double) {
DispatchQueue.main.async {
self.longitudeLabel.text = "Longitude: \(longitude)"
self.layoutIfNeeded()
}
}
}
class GPSInfoViewController: UIViewController {
private let gpsInfoView = GPSInfoView()
var location: CLLocation?
private var updateTimer: Timer?
override func viewDidLoad() {
super.viewDidLoad()
view = gpsInfoView
if let location = location {
updateGPSInfoView(location: location) // Call it initially
}
}
func updateGPSInfoView(location: CLLocation) {
DispatchQueue.main.async {
self.gpsInfoView.setHorizontalAccuracy(location.horizontalAccuracy)
self.gpsInfoView.setVerticalAccuracy(location.verticalAccuracy)
self.gpsInfoView.setLatitude(location.coordinate.latitude)
self.gpsInfoView.setLongitude(location.coordinate.longitude)
self.gpsInfoView.layoutIfNeeded()
}
}
deinit {
updateTimer?.invalidate()
}
}
i supply the view with new location data in this location manager
internal func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
guard let location = locations.last else { return }
updateGPSInfoView(location: location)
locationUser = location
updateGPSIconSignalStatus(manager: manager)
}
private func updateGPSInfoView(location: CLLocation) {
gpsInfoViewController.updateGPSInfoView(location: location)
}
I have debugged and tested a lot of things, but some i have found is that both the set functions and the location manager is getting and sending accurate location information.
Pressing the icon that shows my view also correctly updates it to whatever new location i have. But while on the view i cannot get the labels to refresh automatically once it has that new location information.
i have even tried with a timer as well, which is why there is an unused timer in the controller right now, which i intend to reuse when it works to only update it every 5-10secs instead of every second.