Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Zoom in on User Location- Swift

Tags:

ios

swift

mapkit

I can't figure out how to get the map to zoom in on the user location from viewDidLoad. I tried setting a region, and that didn't work. This is the code I have, any tips?

@IBOutlet weak var mapView: MKMapView! var MapViewLocationManager:CLLocationManager! = CLLocationManager() var currentLocation: PFGeoPoint! = PFGeoPoint()

override func viewDidLoad() {
    super.viewDidLoad()
    self.mapView.showsUserLocation = true
    mapView.delegate = self
   MapViewLocationManager.delegate = self
    mapView.setUserTrackingMode(MKUserTrackingMode.Follow, animated: true)
}

I have looked up the answers for this question but haven't found the answer in Swift or that actually works. Thanks!!

like image 586
Caitlin Avatar asked Jun 25 '15 03:06

Caitlin


2 Answers

You have to try this. It will zooming map.

let span = MKCoordinateSpanMake(0.050, 0.050)
let region = MKCoordinateRegion(center: CLLocationCoordinate2D(latitude: 23.0225, longitude: 72.5714), span: span)
mapView.setRegion(region, animated: true)`
like image 81
Jatin Patel Avatar answered Sep 22 '22 06:09

Jatin Patel


On most apps, a 'current location' button is implemented. A simple way to do it is like this:

@IBAction func myLocationButtonTapped(_ sender: Any) {

    mapView.showsUserLocation = true
    mapView.setUserTrackingMode(.follow, animated: true)

}

Once the user pans or zooms the map, the tracking will stop. So it's good enough for me.

like image 20
AmitP Avatar answered Sep 26 '22 06:09

AmitP