ios - 我正在尝试在 xcode 中设置默认放大
问题描述
下面是我的代码。我试图通过在 xcode 中放大到某个数字来开始我的地图。它似乎不是那样工作的。现在每次打开地图,地图都会完全缩小到可以看到整个大陆的程度。任何帮助都感激不尽。
import UIKit
import MapKit
import CoreLocation
class YogaMap: UIViewController{
@IBOutlet weak var Yoga: MKMapView!
let locationManager = CLLocationManager()
let regionInMeters: Double = 100
override func viewDidLoad(){
super.viewDidLoad()
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)
Yoga.setRegion(region, animated: true)
}
}
func checkLocationServices(){
if CLLocationManager.locationServicesEnabled(){
setupLocationManager()
checkLocationAuthorization()
} else {
}
}
func checkLocationAuthorization(){
switch CLLocationManager.authorizationStatus(){
case .authorizedWhenInUse:
centerViewOnUserLocation()
break
case .denied:
break
case .notDetermined:
locationManager.requestWhenInUseAuthorization()
break
case .restricted:
break
case .authorizedAlways:
break
}
}
}
extension YogaMap: CLLocationManagerDelegate {
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
}
func locationManager(_ manager: CLLocationManager, didChangeAuthorization status: CLAuthorizationStatus) {
}
}
谢谢
解决方案
您应该等待位置管理器返回位置,然后再尝试使用centerViewOnUserLocation
. 尝试将你的函数调用移到里面locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
推荐阅读
- javascript - 从输出中删除字符串“未定义”
- android - 在android中将基于iso国家代码本地时间的时间转换为UTC时间
- r - 为什么在这里使用 do.call ?
- c# - C# LINQ - 从查询结果中排除空对象 [EF - 无引用 Int]
- python - 运行 PyInstaller 创建的可执行文件时出错
- mysql - 将多列内部连接到一个 ID
- konvajs - konvajs中的`destroy`和`remove`有什么区别
- kotlin - 如何在@ParametrizedTest 上应用条件参数?
- apache-spark - Apache Spark 中的多类分类错误指标
- mfc - 在 CDialogEX 项目中使用 CMFCStatusBar