首页 > 解决方案 > 我正在尝试在 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) {

    }

}

谢谢

标签: iosswiftxcode

解决方案


您应该等待位置管理器返回位置,然后再尝试使用centerViewOnUserLocation. 尝试将你的函数调用移到里面locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])


推荐阅读