蓝点位置地图套件

时间:2020-05-29 03:04:18

标签: swift mapkit

我正在尝试使用Mapkit制作地图应用程序。但是当我在模拟器上运行应用程序时,我似乎无法访问蓝点。我对Mapkit还是很陌生,任何提示都将不胜感激。这是我的代码。 ps:我正在使用Swift 4.2和Xcode 11进行编码

import UIKit
import MapKit
import CoreLocation

class mapViewController: UIViewController {

    let locationManager = CLLocationManager()

    @IBOutlet weak var mapView: MKMapView!

    override func viewDidLoad() {
        super.viewDidLoad()
        locationManager.requestWhenInUseAuthorization()
        checkLocationServices()

    }

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

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

    func checkLocationServices() {
        if CLLocationManager.locationServicesEnabled() {
            setupLocationManager()
            checkLocationAuthorization()
        } else {

        }

    }

    func checkLocationAuthorization() {
        let pin = MKPointAnnotation()

        switch CLLocationManager.authorizationStatus() {
        case .authorizedWhenInUse:
            mapView.showsUserLocation = true
            mapView.addAnnotation(pin)
            break
        case .denied:
            break
        case .notDetermined:
            locationManager.requestWhenInUseAuthorization()
            break
        case .restricted:
            break
        case .authorizedAlways:
            break
        }
    }

}

extension mapViewController: CLLocationManagerDelegate {

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        // we'll be back
    }

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

}

enter image description here

1 个答案:

答案 0 :(得分:0)

检查模拟器菜单:功能>位置>自定义位置

enter image description here