0

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)

user3476534
  • 220
  • 1
  • 4
  • 15
  • 1
    FYI http://stackoverflow.com/questions/12641658/ios6-mkmapview-using-a-ton-of-memory-to-the-point-of-crashing-the-app-anyone-e/32422435#32422435 – DogCoffee Sep 09 '15 at 01:43
  • Thanks for the info on the memory use. Will this stop the map from resetting the region everytime the centercoordinate is updated? I can't try this until tonight. Thanks! – user3476534 Sep 09 '15 at 13:09
  • Worked dogcoffee. Also used "setCenterCoordinate" instead of "centerCoordinate". Thanks! – user3476534 Sep 09 '15 at 23:48

0 Answers0