Man James
Man James

Reputation: 89

My location is not displayed on the map in MapKit

When I was using let viewRegion = MKCoordinateRegionMakeWithDistance((userLocation?.coordinate)!, 600, 600) it was fine, however apparently Swift has now replaced this with

let viewRegion = MKCoordinateRegion(center: (userLocation?.coordinate)!, latitudinalMeters: 600, longitudinalMeters: 600)

My location no longer appears on the map anymore, the map shows the right area. Here is my code.

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 = MKCoordinateRegion(center: (userLocation?.coordinate)!, latitudinalMeters: 600, longitudinalMeters: 600)

    self.map.setRegion(viewRegion, animated: true)


}

Upvotes: 0

Views: 61

Answers (1)

SafoineMoncefAmine
SafoineMoncefAmine

Reputation: 165

first hope you didn't forget to extends your viewcontroller from CLLocationManagerDelegate and MKMapViewDelegate.
you can access the userlocation directly from locationManager variable private let locationManager: CLLocationManager = CLLocationManager() from anywhere in your class by calling
userLocation = self.locationManager.location

also don't forget to delegate your view controller to location manager and mapview :

locationManager.delegate = self
        mapView.delegate = self

Upvotes: 0

Related Questions