ios - map View 未显示位置

标签 ios swift mapkit

我目前正在开发一款iOS应用程序,其功能之一包括定位夜生活名胜古迹。但是,当我的 MapView 加载时,即使已授予位置访问权限,也不会显示当前用户位置。

这是我的代码:

import UIKit
import MapKit
import CoreLocation

class BarMapVC: UIViewController {



@IBOutlet weak var searchBtn: UIBarButtonItem!

@IBOutlet weak var doneBtn: UIBarButtonItem!

@IBOutlet weak var searchBar: UISearchBar!

@IBOutlet weak var locationBtn: UIButton!

@IBOutlet weak var barMapView: MKMapView!

var locationManager = CLLocationManager()
let authorizationStatus = CLLocationManager.authorizationStatus()
let regionRadius: Double = 1000

override func viewDidLoad() {
    super.viewDidLoad()
    searchBar.isHidden = true
    barMapView.delegate = self
    locationManager.delegate = self
    configureLocationServices()
    centerMapOnUserLocation()
}

@IBAction func searchBtnWasPressed(_ sender: Any) {
    searchBar.isHidden = false
}

@IBAction func doneBtnWasPressed(_ sender: Any) {
    dismiss(animated: true)
}

@IBAction func locationBtnWasPressed(_ sender: Any) {
    if authorizationStatus == .authorizedAlways || authorizationStatus == .authorizedWhenInUse {
        centerMapOnUserLocation()
    }
}
}

extension BarMapVC: MKMapViewDelegate {
    func centerMapOnUserLocation() {
        guard let coordinate = locationManager.location?.coordinate else { return }
        let coordinateRegion = MKCoordinateRegionMakeWithDistance(coordinate, regionRadius * 2.0, regionRadius * 2.0)
        barMapView.setRegion(coordinateRegion, animated: true)
    }
}

extension BarMapVC: CLLocationManagerDelegate {
    func configureLocationServices() {
        if authorizationStatus == .notDetermined {
            locationManager.requestAlwaysAuthorization()
        } else {
            return
        }
    }

    func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
        centerMapOnUserLocation()
    }

}

我正在使用 CLLocation Manager,遵守 mapViewDelegate,并且拥有所有需要的导出。我做错了什么吗?

最佳答案

您可以在 map View 上进行设置:

barMapView.showsUserLocation = true

关于ios - map View 未显示位置,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/50781491/

相关文章:

ios - 在 iPad 上使用 Touch ID 登录多用户应用程序

ios - 使 TabView 在 SwiftUI 上不透明会在顶部产生一个新 View

ios - 如何将百分比值精确舍入到小数点后两位双倍?

ios - UIDocumentPickerViewController - 未调用委托(delegate)方法

swift - 由于自定义 CNSocialProfile 服务名称,imessage 中的 VCF 崩溃?

ios - 如何快速将 map 置于用户位置的中心?

ios - 自定义 MKAnnotation 按钮

objective-c - UIKeyboardTypeNumberPad 关闭键盘

ios - Segue in swift 2 问题

iPhone MapView 仅限于密苏里州