I am trying to display the current location of the user and have the region set to their current location.
Here is my code.
@IBOutlet weak var mapView: MKMapView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
self.locationManager.requestWhenInUseAuthorization()
if CLLocationManager.locationServicesEnabled() {
locationManager.delegate = self
locationManager.desiredAccuracy = kCLLocationAccuracyNearestTenMeters
locationManager.startUpdatingLocation()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
let locValue:CLLocationCoordinate2D = manager.location!.coordinate
print("locations = \(locValue.latitude) \(locValue.longitude)")
let userLocation = locations.last
let viewRegion = MKCoordinateRegionMakeWithDistance((userLocation?.coordinate)!, 600, 600)
self.mapView.setRegion(viewRegion, animated: true)
}
What happens:
- Map loads
- Map region is set to current location
- Map region will keep updating to the current location
- Current location gets printed to the console
What doesn't work:
- "Blue" current location indicator does not appear.
If you know what I need to do to make the "Blue" current location indicator appear it will be much appreciated.
You need to set
In
viewDidLoad