Search code examples
iosswiftmkmapview

What's the correct way to zoom in on a users location using swift with xcode


I have a map that I'm trying to use to zoom in on a users location and I can seem to get setUserTrackingMode to work correctly. I have showsUserLocation working fine but I can't get it to zoom in. I'm using xCode 6 with iOS 8 and swift. Here's how I'm trying to call the method:

@IBOutlet var mapView : MKMapView
override func viewDidLoad() {
    super.viewDidLoad()
    self.mapView.showsUserLocation = true
    self.mapView.delegate = self;
    self.mapView.setUserTrackingMode(MKUserTrackingModeFollow, animated: true);

I'm getting an error for self.mapView.setUserTrackingMode(MKUserTrackingModeFollow, animated: true);

The error says, "Use of unresolved identifier 'MKUserTrackingModeFollow'"

How can I get it to zoom in on the users location?


Solution

  • From the pre-release documentation the swift tracking modes are:

    enum MKUserTrackingMode : Int {
        case None
        case Follow
        case FollowWithHeading
    }
    

    You should use -

    self.mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true);
    

    In Swift, as enums are treated as a type, "Follow" is interpreted within the scope of an MKUserTrackingMode enum.