I need to make a button that centers the map on user's location and another one that centers the map on specific coordinates (centroCU). When I run the app, the button shows, but the action doesn't work. What am I doing wrong? I got the code from Button to center view to user location using MapKit
class ViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {
let map = MKMapView()
let centroCU = CLLocationCoordinate2D(latitude: 25.72714,
longitude: -100.31190)
...
override func viewDidLoad() {
super.viewDidLoad()
view.addSubview(map)
map.frame = view.bounds
map.setRegion(MKCoordinateRegion(center: centroCU,
span: MKCoordinateSpan(latitudeDelta: 0.013,
longitudeDelta: 0.013))
, animated: true)
map.delegate = self
addMapTrackingButton()
...
}
func addMapTrackingButton(){
let image = UIImage(named: "Recenter") as UIImage?
let button = UIButton(type: UIButton.ButtonType.custom) as UIButton
button.frame = CGRect(origin: CGPoint(x: 315 , y: 50), size: CGSize(width: 45, height: 45))
button.setImage(image, for: .normal)
button.backgroundColor = .clear
button.addTarget(self, action: #selector(ViewController.centerMapOnCentroCU), for:.touchUpInside)
map.addSubview(button)
}
@objc func centerMapOnCentroCU() {
map.setRegion(MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 25.72714,
longitude: -100.31190),
span: MKCoordinateSpan(latitudeDelta: 0.013,
longitudeDelta: 0.013)) }
...
}