在UiLabel中看不到MapKit中的中心位置

时间:2018-11-02 07:53:46

标签: swift mapkit userlocation

无法在标签中显示地图的中心位置!

导入UIKit 导入MapKit 导入CoreLocation

ViewController类:UIViewController,CLLocationManagerDelegate,MKMapViewDelegate {

@IBOutlet weak var myMapView: MKMapView!

@IBOutlet weak var addressLabel: UILabel!

let locationManager = CLLocationManager()
let regionInMeters: Double = 10000
var previousLocation: CLLocation?


override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.

    checkLocationServices()

}

func setupLocationManager() {
    locationManager.delegate = self
    locationManager.desiredAccuracy = kCLLocationAccuracyBest
}

func centerViewOnUserLocation() {
    if let location = locationManager.location?.coordinate {
        let region = MKCoordinateRegion.init(center: location, latitudinalMeters: regionInMeters, longitudinalMeters: regionInMeters)
        myMapView.setRegion(region, animated: true)
    }

}

// the Location service
func checkLocationServices() {
    if CLLocationManager.locationServicesEnabled() {
        setupLocationManager()
        checkLocationAuthorisation()

    } else {
        //show user to turn ON the services
    }
}

func checkLocationAuthorisation() {
    switch CLLocationManager.authorizationStatus() {
    case .authorizedWhenInUse:
        // do the stuff
        startTrackingLocation()

    case .denied:
        //Alert to turn ON the permissions
        break
    case .notDetermined:
        locationManager.requestWhenInUseAuthorization()
    case .restricted:
        // Alert to show what's up
        break
    case .authorizedAlways:

        break

    }
}

func startTrackingLocation() {
    myMapView.showsUserLocation = true
    centerViewOnUserLocation()
    locationManager.startUpdatingLocation()
    previousLocation = getCenterLocation(for: myMapView)
}

func getCenterLocation(for mapView: MKMapView) -> CLLocation {
    let latitude = mapView.centerCoordinate.latitude
    let longitude = mapView.centerCoordinate.longitude

    return CLLocation(latitude: latitude, longitude: longitude)
}


func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
    // We'll be back
    checkLocationAuthorisation()
}

func mapView(_ mapView: MKMapView, regionDidChangeAnimated animated: Bool) {

    let center = getCenterLocation(for: myMapView)
    let geoCoder = CLGeocoder()

    guard let previousLocation = self.previousLocation else { return }

    guard center.distance(from: previousLocation) > 50 else { return }
    self.previousLocation = center

    geoCoder.reverseGeocodeLocation(center) { [weak self] (placemarks, error) in
        guard let self = self else { return }

        if let _ = error {
            //TODO: Show alert informing the user
            return
        }
        guard let placemark = placemarks?.first else {
            //TODO: Show alert informing the user
            return
        }

        let streetNumber = placemark.subThoroughfare  ?? ""
        let streetName = placemark.subThoroughfare ?? ""

        DispatchQueue.main.async {
            self.addressLabel.text = "\(streetNumber) \(streetName)"
        }
    }
}


@IBAction func satelliteView(_ sender: Any) {
    myMapView.mapType = MKMapType.satellite
}


@IBAction func hybridView(_ sender: Any) {
    myMapView.mapType = MKMapType.hybrid
}


@IBAction func standardView(_ sender: Any) {
    myMapView.mapType = MKMapType.standard
}


@IBAction func findDirections(_ sender: Any) {
}

}

enter image description here

应用中的标签应该显示地图的中心位置(大头针)。但是不知道哪里出了问题!

1 个答案:

答案 0 :(得分:0)

更改了getCenterlocation函数并使其正常工作! `func getCenterLocation(for mapView:MKMapView)-> CLLocation {

 let latitude=mapView.centerCoordinate.latitude
 let longitude=mapView.centerCoordinate.longitude
 return CLLocation(latitude:latitude, longitude:longitude)

} func getCenterLocation(对于myMapView:MKMapView)-> CLLocation {

 let latitude=myMapView.centerCoordinate.latitude
 let longitude=myMapView.centerCoordinate.longitude
 return CLLocation(latitude:latitude, longitude:longitude)

}`