蓝点定位地图包

问题描述 投票:0回答:1

我试图使用Mapkit制作一个地图应用程序,但当我在模拟器上运行应用程序时,我似乎无法访问蓝点。我对Mapkit还是个新手,如果有任何提示,我将非常感激。

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

swift mapkit
1个回答
0
投票

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

enter image description here

© www.soinside.com 2019 - 2024. All rights reserved.