I am currently stuck while attempting to figure out how to add a button on my map that will redisplay a user's current location if they stray away from it on the map. At the moment I have the code written below that displays the user's current location.
import UIKit
import MapKit
import CoreLocation
class GameViewController: UIViewController,CLLocationManagerDelegate
{
var lastUserLocation: MKUserLocation?
@IBOutlet weak var Map: MKMapView!
let manager = CLLocationManager()
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let location = locations[0]
let span:MKCoordinateSpan = MKCoordinateSpanMake(0.00775, 0.00775)
let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
let region: MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
Map.setRegion(region, animated: true)
self.Map.showsUserLocation = true
manager.stopUpdatingLocation()
}
override func viewDidLoad() {
super.viewDidLoad()
manager.delegate = self
manager.desiredAccuracy = kCLLocationAccuracyBest
manager.requestAlwaysAuthorization()
manager.startUpdatingLocation()
}
@IBAction func refLocation(_ sender: Any) {
print("click")
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
}
What I am unsure of, is what code to put inside of the @IBAction function that will allow the map to re-center to the user's current location if they stray from it while looking elsewhere.