I'm starting an iOS project, and when I try to use the user's location, the map seems to reset every time I set the center coordinate. I have a function mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!
where the only thing is mapView.centerCoordinate = userLocation.location.coordinate
. When I comment out this code, the map seems to use the same region I had set in a button that zooms in. When I uncomment the code, the map zooms back out to the original setting when the location is updated. The memory use continues to increase each time, which leads me to believe a new map instance is being created over the current one for some reason. I didn't have this problem when I used a single view application, but now I'm using the tabbed view application default. The secondViewController is set to the default when it is created. Pretty much all of the code:
class FirstViewController: UIViewController, MKMapViewDelegate {
@IBOutlet var mapView: MKMapView!
@IBOutlet var startButton: UIButton!
override func viewDidLoad() {
super.viewDidLoad()
mapView.delegate=self
mapView.showsUserLocation = true
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
@IBAction func startEnd(sender: AnyObject) {
if (startButton.currentTitle == "Start") {
startButton.setTitle("Running", forState: .Normal)
let userLocation=mapView.userLocation
let region=MKCoordinateRegionMakeWithDistance(userLocation.location.coordinate,2000,2000)
mapView.setRegion(region,animated:true)
} else {
startButton.setTitle("Start", forState: .Normal)
}
}
func mapView(mapView: MKMapView!, didUpdateUserLocation userLocation: MKUserLocation!) {
mapView.centerCoordinate = userLocation.location.coordinate
}
How do I stop the map from resetting the map view region to the default (or, possibly, creating a new map on top of the one I already have)