I am learning iOS development. I have create UILable which display the Gps coordinates of user. as below
func locationManager(_ _manager:CLLocationManager, didUpdateLocations Location:[CLLocation]){
if let location = Location.first{
coordinate.text = "Your location \(location.coordinate.latitude.description) \(location.coordinate.longitude.description) - \(location.timestamp.description)"
speed.text = "Your moving at speed of \(location.speed.description) course \(location.courseAccuracy.description)"
altitude.text = "Your at altitude of \(location.altitude.description)"
I have Label which I name coordinate that display the current Gps coordinate and it's clickable as below.
@IBOutlet weak var coordinate: UILabel!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
coordinate.adjustsFontSizeToFitWidth = true
let tap = UITapGestureRecognizer(target: self, action: #selector(self.labelAction(_:)))
coordinate.addGestureRecognizer(tap)
coordinate.isUserInteractionEnabled = true
}
@objc func labelAction(_ sender: UIGestureRecognizer) {
let alert = UIAlertController(title: "Coordinate", message: "I have been clicked...", preferredStyle: .alert)
self.present(alert, animated: true, completion: nil)
debugPrint("clicked")
}
Now whats happening is when you click coordinate:UILabel its show the alert message. But what I Wan't is to open the map app and point to current Gps coordinate. I have been able to achieve this but not with click of label but when the app launch. Below is the sample Example of what I have achieved.
func locationManager(_ _manager:CLLocationManager, didUpdateLocations Location:[CLLocation]){
if let location = Location.first{
coordinate.text = "Your location \(location.coordinate.latitude.description) \(location.coordinate.longitude.description) - \(location.timestamp.description)"
speed.text = "Your moving at speed of \(location.speed.description) course \(location.courseAccuracy.description)"
altitude.text = "Your at altitude of \(location.altitude.description)"
// let object = CLLocation(latitude: location.coordinate.latitude, longitude: location.coordinate.longitude)
// print(object.description)
let regionDistance:CLLocationDistance = 1000
let coordinates = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let regionSpant = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
let option = [
MKLaunchOptionsMapCenterKey:NSValue(mkCoordinate: regionSpant.center),
MKLaunchOptionsMapSpanKey:NSValue(mkCoordinateSpan: regionSpant.span)
]
let placeMark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placeMark)
mapItem.name = "You are Here"
mapItem.openInMaps(launchOptions: option)
}
}
what I want is to move the part below
let regionDistance:CLLocationDistance = 1000
let coordinates = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
let regionSpant = MKCoordinateRegion(center: coordinates, latitudinalMeters: regionDistance, longitudinalMeters: regionDistance)
let option = [
MKLaunchOptionsMapCenterKey:NSValue(mkCoordinate: regionSpant.center),
MKLaunchOptionsMapSpanKey:NSValue(mkCoordinateSpan: regionSpant.span)
]
let placeMark = MKPlacemark(coordinate: coordinates, addressDictionary: nil)
let mapItem = MKMapItem(placemark: placeMark)
mapItem.name = "You are Here"
mapItem.openInMaps(launchOptions: option)
To move it to @objc func labelAction(_ sender: UIGestureRecognizer)
so I will be able to open a map only when I click the label.But what am struggling with is to get CLLocationCoordinate
out of locationManger
function and pass it to labelAction
function any suggestion or help I would appreciate. I apologies for my English I am using google translator
CodePudding user response:
You need to create an instance variable
var lastCoordinate:CLLocationCoordinate?
And assign it here
func locationManager(_ _manager:CLLocationManager, didUpdateLocations locations:[CLLocation]){
if let location = locations.first {
lastCoordinate = location.coordinate
....
Then use it anywhere
@objc func labelAction(_ sender: UIGestureRecognizer) {
guard let coor = lastCoordinate else { return }
// use coor