swift - 无法在 map View 中快速移动

标签 swift dictionary view location

我无法将 map 移动或缩放到其他位置。我使用了本教程:https://www.youtube.com/watch?v=qrdIL44T6FQ在 map 上显示我的位置。出于某种原因,打开应用程序后, map 首先非常缓慢地(+- 30 秒)放大到我的位置。然后,当我想在 map 中四处移动时,我做不到。它停留在我当前的位置。

这是我的代码:

import UIKit
import MapKit
import CoreLocation

class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    
    let locationManager = CLLocationManager()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        self.locationManager.delegate = self
        
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        
        self.locationManager.requestWhenInUseAuthorization()
        
        self.locationManager.startUpdatingLocation()
        
        self.mapView.showsUserLocation = true
        
        


    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        
        let location = locations.last
        
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.5, longitudeDelta: 0.5 ))
        
        self.mapView.setRegion(region, animated: true)
        
        self.locationManager.startUpdatingLocation()
    
    }
    
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError) {
        print("Errors: " + error.localizedDescription)
    }
        

}

有人知道这个问题吗?谢谢!

最佳答案

没关系,我在某处犯了错误。此代码工作正常:

import UIKit
import MapKit
import CoreLocation

class FirstViewController: UIViewController, MKMapViewDelegate, CLLocationManagerDelegate {

    @IBOutlet weak var mapView: MKMapView!
    
    let locationManager = CLLocationManager()
    
    override func viewDidLoad()
    {
        super.viewDidLoad()
        
        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
        self.locationManager.requestWhenInUseAuthorization()
        self.locationManager.startUpdatingLocation()
        
        self.mapView.showsUserLocation = true
        
    }
    
    override func didReceiveMemoryWarning()
    {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }
    
    // MARK: - Location Delegate Methods
    
    func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations.last
        
        let center = CLLocationCoordinate2D(latitude: location!.coordinate.latitude, longitude: location!.coordinate.longitude)
        let region = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 1, longitudeDelta: 1))
        
        self.mapView.setRegion(region, animated: true)
        
        self.locationManager.stopUpdatingLocation()
    }
    
    func locationManager(manager: CLLocationManager, didFailWithError error: NSError)
    {
        print("Error: " + error.localizedDescription)
    }
    
}

关于swift - 无法在 map View 中快速移动,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38854120/

相关文章:

iOS - 自定义 View frame.size.height 给出 0 值

Python - 在列表中选择一个字典,哪个键更接近全局值

java - map 上的流不保存 .map 更改

ios - 注销时创建 toast (iOS)

swift - SwiftUI View 协议(protocol)中的 EnvironmentObject

ios - 如何在准备中的 viewController 之间通过引用传递快速数组(对于 segue :?

swift - lldb 如何处理特殊用途的 _swift_runtime_on_report 函数?

swift - 如何在 UIAlertController 中添加 UIPickerView?

python - 在 Python 中将值添加到列表的字典中?

android - 如何将 View 作为背景添加到 surfaceView?