iosswiftmkmapviewswift4mkuserlocation

Location Marker Not Displaying Swift 4


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:

  1. Map loads
  2. Map region is set to current location
  3. Map region will keep updating to the current location
  4. Current location gets printed to the console

What doesn't work:

  1. "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.


Solution

  • You need to set

    mapView.showsUserLocation = true
    

    In viewDidLoad