简体   繁体   中英

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)


}

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM