我正在尝试在 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])内移动函数调用

您忘记在此函数中调用startUpdatingLocationfunc setupLocationManager

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

位置更新时的中心地图视图

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
centerViewOnUserLocation()
}

最新更新